Skip to content

Commit 448d37d

Browse files
check for port when stoping service
1 parent 8a4ee6a commit 448d37d

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

tests/ociswrapper/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Also, see `./bin/ociswrapper help` for more information.
127127

128128
6. `POST /services/{service-name}`
129129

130-
Restart oCIS with service excluded and start excluded oCIS service individually, not covered by the oCIS supervisor.
130+
Restart oCIS instances without specified service and start that service independently (not covered by the oCIS supervisor).
131131

132132
Body of the request should be a JSON object with the following structure:
133133

@@ -143,20 +143,20 @@ Also, see `./bin/ociswrapper help` for more information.
143143
> You need to set the proper addresses to access the service from other steps in the CI pipeline.
144144
>
145145
> `{SERVICE-NAME}_DEBUG_ADDR=0.0.0.0:{DEBUG_PORT}`
146+
>
146147
> `{SERVICE-NAME}_HTTP_ADDR=0.0.0.0:{HTTP_PORT}`
147148

148-
149149
Returns:
150150

151-
- `200 OK` - oCIS server is stopped
151+
- `200 OK` - oCIS service started successfully
152152
- `400 Bad Request` - request body is not a valid JSON object
153-
- `500 Internal Server Error` - Unable to start oCIS service
153+
- `500 Internal Server Error` - Failed to start oCIS service audit
154154

155155
7. `DELETE /services/{service-name}`
156156

157157
Stop individually running oCIS service
158158

159159
Returns:
160160

161-
- `200 OK` - command is successfully executed
162-
- `500 Internal Server Error` - Unable to start oCIS service
161+
- `200 OK` - oCIS service stopped successfully
162+
- `500 Internal Server Error` - Unable to stop oCIS service

tests/ociswrapper/ocis/ocis.go

+41-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ func StartService(service string, envMap []string) {
223223
} else {
224224
cmd.Env = append(os.Environ(), envMap...)
225225
}
226-
log.Println(fmt.Sprintf("%s", cmd.Env))
227226

228227
logs, err := cmd.StderrPipe()
229228
if err != nil {
@@ -322,6 +321,12 @@ func StopService(service string) (bool, string) {
322321
return false, fmt.Sprintf("Failed to stop service with process id %d", pid)
323322
}
324323

324+
// service takes 30sec time to completely shutdown
325+
success := WaitForServiceDown(service)
326+
if !success {
327+
StopService(service)
328+
}
329+
325330
delete(runningServices, service)
326331
log.Println(fmt.Sprintf("oCIS service %s has been stopped successfully", service))
327332

@@ -361,3 +366,38 @@ func WaitForService(service string) bool {
361366
}
362367
}
363368
}
369+
370+
// wwait until service completely down
371+
func WaitForServiceDown(service string) bool {
372+
overallTimeout := time.After(30 * time.Second)
373+
ticker := time.NewTicker(2 * time.Second)
374+
defer ticker.Stop()
375+
376+
port := config.GetServiceDebugPort(service)
377+
378+
for {
379+
select {
380+
case <-overallTimeout:
381+
// Timeout occurred while waiting for the service to be available
382+
log.Println(fmt.Errorf("timeout: %s service did not shut down within 30 seconds", service).Error())
383+
return false
384+
case <-ticker.C:
385+
// Retry if the service is not in `runningServices`
386+
if _, exists := runningServices[service]; !exists {
387+
log.Println(fmt.Sprintf("Service %s not found in running services. Retrying...\n", service))
388+
continue
389+
}
390+
391+
address := fmt.Sprintf(":%d", port)
392+
// Try to connect to the port
393+
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
394+
if err != nil {
395+
log.Println(fmt.Sprintf("%s service port %d is no longer reachable", service, port))
396+
return true
397+
}
398+
_ = conn.Close()
399+
log.Println(fmt.Sprintf("%s service port %d is still active. Retrying...", service, port))
400+
}
401+
}
402+
}
403+

0 commit comments

Comments
 (0)