/
watchdog.go
110 lines (87 loc) · 2.22 KB
/
watchdog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package common
import (
"bytes"
"fmt"
"os"
"os/exec"
"syscall"
"time"
)
// https://medium.com/@vCabbage/go-timeout-commands-with-os-exec-commandcontext-ba0c861ed738
type ErrWatchdog struct {
msg string
}
func (e *ErrWatchdog) Error() string {
return e.msg
}
func WatchdogCmd(cmd *exec.Cmd, timeout time.Duration) error {
doneCh := make(chan error)
start := time.Now()
err := cmd.Start()
if Error(err) {
return err
}
Debug("Watchdog process started pid: %d timeout: %v cmd: %s ...", cmd.Process.Pid, timeout, CmdToString(cmd))
go func() {
doneCh <- cmd.Wait()
}()
select {
case <-time.After(timeout):
Debug("Watchdog process will be killed pid: %d timeout: %v cmd: %s time: %v", cmd.Process.Pid, timeout, CmdToString(cmd), time.Since(start))
Error(cmd.Process.Kill())
return &ErrWatchdog{msg: fmt.Sprintf("Watchdog killed process pid: %d cmd: %s after: %v", cmd.Process.Pid, CmdToString(cmd), time.Since(start))}
case err = <-doneCh:
exitcode := 0
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
exitcode = exitError.ExitCode()
} else {
exitcode = -1
}
}
exitstate := ""
switch exitcode {
case 0:
exitstate = "successfull"
default:
exitstate = "failed"
}
output := "<na>"
bu, ok := cmd.Stdout.(*bytes.Buffer)
if ok {
output = "\n" + string(bu.Bytes())
}
Debug("Watchdog process %s! pid: %d exitcode: %d timeout: %v cmd: %s time: %s output: %s", exitstate, cmd.Process.Pid, exitcode, timeout, CmdToString(cmd), time.Since(start), output)
return err
}
}
func WatchdogFunc(msg string, fn func() error, timeout time.Duration) error {
doneCh := make(chan error)
start := time.Now()
var err error
go func() {
doneCh <- fn()
}()
select {
case <-time.After(timeout):
Debug("Watchdog function killed! time: %v", time.Since(start))
return &ErrWatchdog{msg: msg}
case err = <-doneCh:
exitstate := ""
if err != nil {
exitstate = "failed"
} else {
exitstate = "successfull"
}
Debug("Watchdog function %s! time: %s", exitstate, time.Since(start))
return err
}
}
func StillAlive(pid int) bool {
process, err := os.FindProcess(pid)
if err != nil {
return false
} else {
return process.Signal(syscall.Signal(0)) != nil
}
}