@@ -145,7 +145,7 @@ func waitUntilCompleteShutdown() (bool, string) {
145
145
timeout := 30 * time .Second
146
146
startTime := time .Now ()
147
147
148
- c := exec .Command ("sh" , "-c" , "ps ax | grep '[o]cis server' | grep -v grep | awk '{print $1}'" )
148
+ c := exec .Command ("sh" , "-c" , "ps ax | grep 'ocis server' | grep -v grep | awk '{print $1}'" )
149
149
output , err := c .CombinedOutput ()
150
150
if err != nil {
151
151
log .Println (err .Error ())
@@ -223,6 +223,7 @@ func StartService(service string, envMap []string) {
223
223
} else {
224
224
cmd .Env = append (os .Environ (), envMap ... )
225
225
}
226
+ log .Println (fmt .Sprintf ("%s" , cmd .Env ))
226
227
227
228
logs , err := cmd .StderrPipe ()
228
229
if err != nil {
@@ -251,7 +252,6 @@ func StartService(service string, envMap []string) {
251
252
252
253
for listService , pid := range runningServices {
253
254
log .Println (fmt .Sprintf ("%s service started with process id %v" , listService , pid ))
254
- WaitUntilPortListens (listService )
255
255
}
256
256
257
257
// Read the logs when the 'ocis server' command is running
@@ -307,65 +307,25 @@ func StartService(service string, envMap []string) {
307
307
308
308
// Stop oCIS service or a specific service by its unique identifier
309
309
func StopService (service string ) (bool , string ) {
310
- stopWg := new (sync.WaitGroup )
311
- stopWg .Add (1 )
312
-
313
- resultChan := make (chan struct {
314
- success bool
315
- message string
316
- }, 1 )
317
-
318
- go func () {
319
- defer stopWg .Done ()
320
-
321
- pid , exists := runningServices [service ]
322
- if ! exists {
323
- resultChan <- struct {
324
- success bool
325
- message string
326
- }{false , fmt .Sprintf ("Service %s is not running" , service )}
327
- return
328
- }
329
-
330
- process , err := os .FindProcess (pid )
331
- if err != nil {
332
- resultChan <- struct {
333
- success bool
334
- message string
335
- }{false , fmt .Sprintf ("Failed to find service %s process running with ID %d" , service , pid )}
336
- return
337
- }
338
-
339
- pKillError := process .Signal (syscall .SIGINT )
340
- if pKillError != nil {
341
- resultChan <- struct {
342
- success bool
343
- message string
344
- }{false , fmt .Sprintf ("Failed to stop service with process id %d" , pid )}
345
- return
346
- }
347
-
348
- delete (runningServices , service )
349
- log .Println (fmt .Sprintf ("oCIS service %s has been stopped successfully" , service ))
310
+ pid , exists := runningServices [service ]
311
+ if ! exists {
312
+ return false , fmt .Sprintf ("Service %s is not running" , service )
313
+ }
350
314
351
- resultChan <- struct {
352
- success bool
353
- message string
354
- }{true , fmt .Sprintf ("Service %s stopped successfully" , service )}
355
- }()
315
+ process , err := os .FindProcess (pid )
316
+ if err != nil {
317
+ return false , fmt .Sprintf ("Failed to find service %s process running with ID %d" , service , pid )
318
+ }
356
319
357
- // Wait for the goroutine to finish and retrieve the result.
358
- stopWg . Wait ()
359
- result := <- resultChan
360
- close ( resultChan )
320
+ pKillError := process . Signal ( syscall . SIGINT )
321
+ if pKillError != nil {
322
+ return false , fmt . Sprintf ( "Failed to stop service with process id %d" , pid )
323
+ }
361
324
362
- return result . success , result . message
363
- }
325
+ delete ( runningServices , service )
326
+ log . Println ( fmt . Sprintf ( "oCIS service %s has been stopped successfully" , service ))
364
327
365
- var servicePortMap = map [string ]int {
366
- "ocis" : 9250 ,
367
- "audit" : 9229 ,
368
- "activitylog" : 9197 ,
328
+ return true , fmt .Sprintf ("Service %s stopped successfully" , service )
369
329
}
370
330
371
331
// WaitUntilPortListens waits until the port for a given service is listening
@@ -374,11 +334,7 @@ func WaitUntilPortListens(service string) bool {
374
334
ticker := time .NewTicker (2 * time .Second )
375
335
defer ticker .Stop ()
376
336
377
- port , exists := servicePortMap [service ]
378
- if ! exists {
379
- log .Println (fmt .Sprintf ("%s service port not mapped..." , service ))
380
- return false
381
- }
337
+ port := config .GetService (service )
382
338
383
339
for {
384
340
select {
@@ -387,15 +343,22 @@ func WaitUntilPortListens(service string) bool {
387
343
log .Println (fmt .Errorf ("timeout: %s service did not become available within 30 seconds" , service ).Error ())
388
344
return false
389
345
case <- ticker .C :
346
+ // Retry if the service is not in `runningServices`
347
+ if _ , exists := runningServices [service ]; ! exists {
348
+ log .Println (fmt .Sprintf ("Service %s not found in running services. Retrying...\n " , service ))
349
+ continue
350
+ }
351
+
352
+ // Check if the port is listening
390
353
address := fmt .Sprintf (":%d" , port )
391
- log .Println (fmt .Sprintf ("Address %s\n " , address ))
392
354
// Try to connect to the port
393
355
conn , err := net .DialTimeout ("tcp" , address , 1 * time .Second )
394
356
if err == nil {
395
357
_ = conn .Close ()
396
- log .Println (fmt .Sprintf ("%s service is ready" , service ))
358
+ log .Println (fmt .Sprintf ("%s service is ready to listen port %d " , service , port ))
397
359
return true
398
360
}
361
+ log .Println (fmt .Sprintf ("%v port is not ready %v\n " , conn , err ))
399
362
}
400
363
}
401
364
}
0 commit comments