|
| 1 | +package grsync |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "log" |
| 6 | + "os/exec" |
| 7 | + "syscall" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +var ErrTimeout = errors.New("command timed out") |
| 12 | + |
| 13 | +// KillGrace is the amount of time we allow a process to shutdown before |
| 14 | +// sending a SIGKILL. |
| 15 | +const KillGrace = 5 * time.Second |
| 16 | + |
| 17 | +// WaitTimeout waits for the given command to finish with a timeout. |
| 18 | +// It assumes the command has already been started. |
| 19 | +// If the command times out, it attempts to kill the process and returns |
| 20 | +// a ErrTimeout error. |
| 21 | +func WaitTimeout(c *exec.Cmd, timeout time.Duration) error { |
| 22 | + var kill *time.Timer |
| 23 | + |
| 24 | + term := time.AfterFunc(timeout, func() { |
| 25 | + err := c.Process.Signal(syscall.SIGTERM) |
| 26 | + if err != nil { |
| 27 | + log.Printf("Error terminating process: %s", err) |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + kill = time.AfterFunc(KillGrace, func() { |
| 32 | + err := c.Process.Kill() |
| 33 | + if err != nil { |
| 34 | + log.Printf("Error killing process: %s", err) |
| 35 | + return |
| 36 | + } |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + err := c.Wait() |
| 41 | + |
| 42 | + // Shutdown all timers (the kill timer and the term timer) before checking cmd err, |
| 43 | + // otherwise there is no chance to turn off these timers that have not expired. |
| 44 | + if kill != nil { |
| 45 | + kill.Stop() |
| 46 | + } |
| 47 | + termSent := !term.Stop() |
| 48 | + // For a timer created with AfterFunc(d, f), if t.Stop returns false, then |
| 49 | + // the timer has already expired and the function f has been started in its own goroutine. |
| 50 | + // So if termSent is true, it means the cmd does not finished before the term timer expired. |
| 51 | + |
| 52 | + // Now, we can check cmd err. |
| 53 | + // If the process exited without error treat it as success. |
| 54 | + // This allows a process to do a clean shutdown on signal. |
| 55 | + if err == nil { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + // If SIGTERM was sent then treat any process error as a timeout. |
| 60 | + if termSent { |
| 61 | + return ErrTimeout |
| 62 | + } |
| 63 | + |
| 64 | + // Otherwise there was an cmd error unrelated to termination. |
| 65 | + return err |
| 66 | +} |
0 commit comments