Skip to content

Commit d29e7f9

Browse files
author
Lukiyanchuk Dmitriy
committed
Implement auto restarting of command if it was finished with error
1 parent 7c01722 commit d29e7f9

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Usage:
2929
|`-graceful-kill=_`| false | On supported platforms, send the child process a SIGTERM to allow it to exit gracefully if possible. |
3030
|`-graceful-timeout=_`| 3 | Duration (in seconds) to wait for graceful kill to complete. |
3131
|`-manual-restart=_`| false | Manual restart by typing "r". |
32+
|`-restart-on-error=_`| false | Restart command if it was finished with error. |
33+
|`-restart-timeout=_`| 5 | Duration (in seconds) to wait before restart on error. |
3234

3335
## Examples
3436

daemon.go

+37-16
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ There are command line options.
5454
-graceful-timeout - Duration (in seconds) to wait for graceful kill to complete
5555
-verbose - Print information about watched directories.
5656
-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
5759
5860
ACTIONS
5961
-build=CCC – Execute CCC to rebuild when a file changes
@@ -149,6 +151,8 @@ var (
149151
flag_gracefultimeout = flag.Uint("graceful-timeout", 3, "Duration (in seconds) to wait for graceful kill to complete")
150152
flag_verbose = flag.Bool("verbose", false, "Be verbose about which directories are watched.")
151153
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")
152156

153157
// initialized in main() due to custom type.
154158
flag_directories dirList
@@ -318,32 +322,40 @@ func runner(commandTemplate string, buildStarted <-chan string, buildSuccess <-c
318322
os.Exit(0)
319323
}()
320324

321-
for {
322-
eventPath := <-buildStarted
325+
command := ""
326+
done := make(chan error, 1)
323327

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)
327334

328-
if !*flag_command_stop {
329-
if !<-buildSuccess {
330-
continue
335+
if !*flag_command_stop {
336+
if !<-buildSuccess {
337+
continue
338+
}
331339
}
332-
}
333340

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 {
340352
continue
341353
}
342354
}
343355

344356
log.Println(okColor("Restarting the given command."))
345-
cmd, stdoutPipe, stderrPipe, err := startCommand(command)
346357

358+
cmd, stdoutPipe, stderrPipe, err := startCommand(command)
347359
if err != nil {
348360
log.Fatal(failColor("Could not start command: %s", err))
349361
}
@@ -352,6 +364,15 @@ func runner(commandTemplate string, buildStarted <-chan string, buildSuccess <-c
352364
pipeChan <- stderrPipe
353365

354366
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+
}
355376
}
356377
}
357378

0 commit comments

Comments
 (0)