@@ -54,6 +54,8 @@ There are command line options.
54
54
-graceful-timeout - Duration (in seconds) to wait for graceful kill to complete
55
55
-verbose - Print information about watched directories.
56
56
-manual-restart - Manual restart by typing "r"
57
+ -restart-on-error - Restart command if it was finished with error
58
+ -restart-timeout - Duration (in seconds) to wait before restart on error
57
59
58
60
ACTIONS
59
61
-build=CCC – Execute CCC to rebuild when a file changes
@@ -149,6 +151,8 @@ var (
149
151
flag_gracefultimeout = flag .Uint ("graceful-timeout" , 3 , "Duration (in seconds) to wait for graceful kill to complete" )
150
152
flag_verbose = flag .Bool ("verbose" , false , "Be verbose about which directories are watched." )
151
153
flag_manual_restart = flag .Bool ("manual-restart" , false , `Manual restart by typing "r"` )
154
+ flag_restartOnError = flag .Bool ("restart-on-error" , false , "Restart command if it was finished with error" )
155
+ flag_restartTimeout = flag .Uint ("restart-timeout" , 5 , "Duration (in seconds) to wait before restart on error" )
152
156
153
157
// initialized in main() due to custom type.
154
158
flag_directories dirList
@@ -318,32 +322,40 @@ func runner(commandTemplate string, buildStarted <-chan string, buildSuccess <-c
318
322
os .Exit (0 )
319
323
}()
320
324
321
- for {
322
- eventPath := <- buildStarted
325
+ command := ""
326
+ done := make ( chan error , 1 )
323
327
324
- // append %0.s to use format specifier even if not supplied by user
325
- // to suppress warning in returned string.
326
- command := fmt .Sprintf ("%0.s" + commandTemplate , eventPath )
328
+ for {
329
+ select {
330
+ case eventPath := <- buildStarted :
331
+ // append %0.s to use format specifier even if not supplied by user
332
+ // to suppress warning in returned string.
333
+ command = fmt .Sprintf ("%0.s" + commandTemplate , eventPath )
327
334
328
- if ! * flag_command_stop {
329
- if ! <- buildSuccess {
330
- continue
335
+ if ! * flag_command_stop {
336
+ if ! <- buildSuccess {
337
+ continue
338
+ }
331
339
}
332
- }
333
340
334
- if currentProcess != nil {
335
- killProcess (currentProcess )
336
- }
337
- if * flag_command_stop {
338
- log .Println (okColor ("Command stopped. Waiting for build to complete." ))
339
- if ! <- buildSuccess {
341
+ if currentProcess != nil {
342
+ killProcess (currentProcess )
343
+ }
344
+ if * flag_command_stop {
345
+ log .Println (okColor ("Command stopped. Waiting for build to complete." ))
346
+ if ! <- buildSuccess {
347
+ continue
348
+ }
349
+ }
350
+ case err := <- done :
351
+ if err == nil {
340
352
continue
341
353
}
342
354
}
343
355
344
356
log .Println (okColor ("Restarting the given command." ))
345
- cmd , stdoutPipe , stderrPipe , err := startCommand (command )
346
357
358
+ cmd , stdoutPipe , stderrPipe , err := startCommand (command )
347
359
if err != nil {
348
360
log .Fatal (failColor ("Could not start command: %s" , err ))
349
361
}
@@ -352,6 +364,15 @@ func runner(commandTemplate string, buildStarted <-chan string, buildSuccess <-c
352
364
pipeChan <- stderrPipe
353
365
354
366
currentProcess = cmd .Process
367
+
368
+ if * flag_restartOnError {
369
+ go func () {
370
+ err := cmd .Wait ()
371
+ time .Sleep (time .Duration (* flag_restartTimeout ) * time .Second )
372
+
373
+ done <- err
374
+ }()
375
+ }
355
376
}
356
377
}
357
378
0 commit comments