7
7
"fmt"
8
8
"io"
9
9
"net/http"
10
- "ociswrapper/common"
11
- "ociswrapper/log"
12
- "ociswrapper/ocis/config"
13
10
"os"
14
11
"os/exec"
15
12
"strconv"
@@ -18,48 +15,56 @@ import (
18
15
"syscall"
19
16
"time"
20
17
18
+ "ociswrapper/common"
19
+ "ociswrapper/log"
20
+ "ociswrapper/ocis/config"
21
+
21
22
"github.com/creack/pty"
22
23
)
23
24
24
25
var cmd * exec.Cmd
25
26
var retryCount = 0
26
27
var stopSignal = false
27
28
var EnvConfigs = []string {}
28
- var runningCommands = make (map [string ]int ) // Maps unique IDs to PIDs
29
+ var runningServices = make (map [string ]int )
29
30
30
31
func Start (envMap []string ) {
31
- StartService ("" , envMap )
32
+ StartService ("" , envMap )
32
33
}
33
34
34
35
func Stop () (bool , string ) {
35
- log .Println (fmt .Sprintf ("Stop ocis check cmd %s\n " , cmd ))
36
36
log .Println ("Stopping oCIS server..." )
37
37
stopSignal = true
38
38
39
- for listservice , pid := range runningCommands {
40
- log .Println (fmt .Sprintf ("Services running before terminating: %s with process and id: %v\n " , listservice , pid ))
41
- process , err := os .FindProcess (pid )
42
- err = process .Signal (syscall .SIGINT )
43
- if err != nil {
44
- if ! strings .HasSuffix (err .Error (), "process already finished" ) {
45
- log .Fatalln (err )
46
- } else {
47
- return true , "oCIS server is already stopped"
48
- }
49
- }
50
- process .Wait ()
39
+ for listservice , pid := range runningServices {
40
+ process , err := os .FindProcess (pid )
41
+ if err != nil {
42
+ log .Println (fmt .Sprintf ("Error finding process for service %s: %v" , listservice , err ))
43
+ continue
44
+ }
45
+ pKillError := process .Signal (syscall .SIGINT )
46
+ if pKillError != nil {
47
+ if ! strings .HasSuffix (pKillError .Error (), "process already finished" ) {
48
+ log .Fatalln (pKillError )
49
+ } else {
50
+ return true , "oCIS server is already stopped"
51
+ }
52
+ }
53
+ _ , waitErr := process .Wait ()
54
+ if waitErr != nil {
55
+ log .Println (fmt .Sprintf ("Error waiting for process to exit: %v\n " , waitErr ))
56
+ }
57
+ delete (runningServices , "ocis" )
51
58
}
52
59
53
- cmd = nil
54
60
success , message := waitUntilCompleteShutdown ()
61
+
62
+ cmd = nil
55
63
return success , message
56
64
}
57
65
58
66
func Restart (envMap []string ) (bool , string ) {
59
- log .Println (fmt .Sprintf ("Restarting ocis check cmd %s\n " , cmd ))
60
- log .Println (fmt .Sprintf ("Restaring ocis with rollback os environ %s\n " , envMap ))
61
- log .Println (fmt .Sprintf ("OS environment: %s\n " , os .Environ ()))
62
- go Stop ()
67
+ Stop ()
63
68
64
69
log .Println ("Restarting oCIS server..." )
65
70
common .Wg .Add (1 )
@@ -69,8 +74,8 @@ func Restart(envMap []string) (bool, string) {
69
74
}
70
75
71
76
func IsOcisRunning () bool {
72
- if runningCommands ["ocis" ] > 0 {
73
- return true
77
+ if runningServices ["ocis" ] > 0 {
78
+ return true
74
79
}
75
80
return false
76
81
}
@@ -136,7 +141,6 @@ func WaitForConnection() (bool, string) {
136
141
}
137
142
138
143
func waitUntilCompleteShutdown () (bool , string ) {
139
- log .Println ("Process found. Waiting... waitUntilCompleteShutdown" )
140
144
timeout := 30 * time .Second
141
145
startTime := time .Now ()
142
146
@@ -207,11 +211,9 @@ func StartService(service string, envMap []string) {
207
211
cmdArgs := []string {"server" } // Default command args
208
212
209
213
if service != "" {
210
- // Directly append service if provided
211
214
cmdArgs = append ([]string {service }, cmdArgs ... )
212
215
}
213
216
214
- // wait for the log scanner to finish
215
217
var wg sync.WaitGroup
216
218
wg .Add (2 )
217
219
@@ -220,18 +222,12 @@ func StartService(service string, envMap []string) {
220
222
defer common .Wg .Done ()
221
223
}
222
224
223
- // Initialize the command
224
225
cmd = exec .Command (config .Get ("bin" ), cmdArgs ... )
225
226
226
- // Use the provided envMap if not empty, otherwise use EnvConfigs
227
227
if len (envMap ) == 0 {
228
228
cmd .Env = append (os .Environ (), EnvConfigs ... )
229
- log .Println (fmt .Sprintf ("OS environment variables while running ocis service: %s\n " , cmd .Env ))
230
- log .Println (fmt .Sprintf ("OS environment: %s\n " , os .Environ ()))
231
229
} else {
232
230
cmd .Env = append (os .Environ (), envMap ... )
233
- log .Println (fmt .Sprintf ("OS environment variables while running ocis service: %s\n " , cmd .Env ))
234
- log .Println (fmt .Sprintf ("OS environment: %s\n " , os .Environ ()))
235
231
}
236
232
237
233
logs , err := cmd .StderrPipe ()
@@ -243,9 +239,6 @@ func StartService(service string, envMap []string) {
243
239
log .Panic (err )
244
240
}
245
241
246
- // log.Println(fmt.Sprintf("command env used to start service %s\n", cmd.Env))
247
- // log.Println(fmt.Sprintf("command used to start service %s\n", cmd))
248
-
249
242
err = cmd .Start ()
250
243
251
244
if err != nil {
@@ -256,17 +249,14 @@ func StartService(service string, envMap []string) {
256
249
outputScanner := bufio .NewScanner (output )
257
250
outChan := make (chan string )
258
251
259
- // If service is an empty string, set the PID for "ocis"
260
252
if service == "" {
261
- runningCommands ["ocis" ] = cmd .Process .Pid
253
+ runningServices ["ocis" ] = cmd .Process .Pid
262
254
} else {
263
- runningCommands [service ] = cmd .Process .Pid
255
+ runningServices [service ] = cmd .Process .Pid
264
256
}
265
257
266
- log .Println ("Started oCIS processes:" )
267
- for listservice , pid := range runningCommands {
258
+ for listservice , pid := range runningServices {
268
259
log .Println (fmt .Sprintf ("Service started: %s with process and id: %v\n " , listservice , pid ))
269
-
270
260
}
271
261
272
262
// Read the logs when the 'ocis server' command is running
@@ -316,45 +306,36 @@ func StartService(service string, envMap []string) {
316
306
}
317
307
}
318
308
}
319
-
320
- log .Println (fmt .Sprintf (" ---- ocis start service ending line---- %s\n " , cmd ))
321
309
wg .Wait ()
322
310
close (outChan )
323
311
}
324
312
325
313
// Stop oCIS service or a specific service by its unique identifier
326
314
func StopService (service string ) (bool , string ) {
327
- for listservice , pid := range runningCommands {
328
- log .Println (fmt .Sprintf ("Services running before terminating: %s with process and id: %v\n " , listservice , pid ))
329
- }
330
-
331
- pid , exists := runningCommands [service ]
332
- log .Println (fmt .Sprintf ("Services process id to terminate: %s\n " , pid ))
315
+ pid , exists := runningServices [service ]
333
316
if ! exists {
334
317
return false , fmt .Sprintf ("Service %s is not running" , service )
335
318
}
336
319
337
320
// Find the process by PID and send SIGINT to stop it
338
321
process , err := os .FindProcess (pid )
339
- log .Println (fmt .Sprintf ("Found process to terminate in os: %s\n " , pid ))
340
-
341
322
if err != nil {
342
- log .Println (fmt .Sprintf ("Failed to find process: %v" , err ))
343
323
return false , fmt .Sprintf ("Failed to find process with ID %d" , pid )
344
324
}
345
325
346
- err = process .Signal (syscall .SIGINT )
347
- log .Println ("Process terminated using signal" )
348
- if err != nil {
349
- log .Println (fmt .Sprintf ("Failed to send signal: %v" , err ))
326
+ pterr := process .Signal (syscall .SIGINT )
327
+ if pterr != nil {
350
328
return false , fmt .Sprintf ("Failed to stop service with PID %d" , pid )
351
329
}
330
+
352
331
time .Sleep (30 * time .Second )
353
- process .Wait ()
354
- log .Println ("Process terminating process.wait" )
355
- delete (runningCommands , service )
356
- for listservice , pid := range runningCommands {
357
- log .Println (fmt .Sprintf ("Service list after deleteing %s service. list contain service: %s with process and id: %v\n " , service , listservice , pid ))
358
- }
332
+ _ , waitErr := process .Wait ()
333
+ if waitErr != nil {
334
+ log .Println (fmt .Sprintf ("Error waiting for process to exit: %v\n " , waitErr ))
335
+ }
336
+
337
+ log .Println (fmt .Sprintf ("Service %s process %v terminated" , service , pid ))
338
+ delete (runningServices , service )
339
+
359
340
return true , fmt .Sprintf ("Service %s stopped successfully" , service )
360
341
}
0 commit comments