forked from lucaslorentz/caddy-supervisor
/
supervisor.go
151 lines (126 loc) · 2.99 KB
/
supervisor.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package supervisor
import (
"log"
"os"
"os/exec"
"time"
)
const (
maxRestartDelay = 5 * time.Minute
minRestartDelay = 10 * time.Second
durationToResetRestartDelay = 10 * time.Minute
)
// Supervisor provides functionality to start and supervise a background process
type Supervisor struct {
options *Options
cmd *exec.Cmd
keepRunning bool
}
// CreateSupervisor creates a new process supervisor
func CreateSupervisor(options *Options) *Supervisor {
return &Supervisor{
options: options,
}
}
// Start a process and supervise
func (s *Supervisor) Start() {
s.keepRunning = true
go s.supervise()
}
func (s *Supervisor) supervise() {
restartDelay := minRestartDelay
for s.keepRunning {
s.cmd = exec.Command(s.options.Command, s.options.Args...)
s.cmd.Env = append(os.Environ(), s.options.Env...)
if s.options.Dir != "" {
s.cmd.Dir = s.options.Dir
}
if stdoutFile := getFile(s.options.RedirectStdout); stdoutFile != nil {
s.cmd.Stdout = stdoutFile
defer stdoutFile.Close()
}
if stderrFile := getFile(s.options.RedirectStderr); stderrFile != nil {
s.cmd.Stderr = stderrFile
defer stderrFile.Close()
}
start := time.Now()
err := s.cmd.Run()
duration := time.Now().Sub(start)
if err != nil {
log.Printf("Process error: %v\n", err)
} else {
log.Printf("Process exited after: %v\n", duration)
}
if !s.keepRunning {
break
}
switch s.options.RestartPolicy {
case RestartAlways:
break
case RestartOnFailure:
if err == nil {
s.keepRunning = false
}
break
case RestartNever:
s.keepRunning = false
break
}
if s.keepRunning {
if restartDelay > minRestartDelay && (err == nil || duration > durationToResetRestartDelay) {
log.Printf("Resetting restart delay to %v\n", minRestartDelay)
restartDelay = minRestartDelay
}
if err != nil {
log.Printf("Restarting in %v\n", restartDelay)
time.Sleep(restartDelay)
restartDelay = increaseRestartDelay(restartDelay)
}
}
}
}
// Stop the supervised process
func (s *Supervisor) Stop() {
s.keepRunning = false
if cmdIsRunning(s.cmd) {
err := s.cmd.Process.Signal(os.Interrupt)
if err == nil {
go func() {
time.Sleep(s.options.TerminationGracePeriod)
if cmdIsRunning(s.cmd) {
s.cmd.Process.Kill()
}
}()
s.cmd.Process.Wait()
} else {
s.cmd.Process.Kill()
}
}
}
func cmdIsRunning(cmd *exec.Cmd) bool {
return cmd != nil && cmd.Process != nil && (cmd.ProcessState == nil || !cmd.ProcessState.Exited())
}
func getFile(value string) *os.File {
if value == "" {
return nil
}
switch value {
case "stdout":
return os.Stdout
case "stderr":
return os.Stderr
default:
outFile, err := os.OpenFile(value, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return nil
}
return outFile
}
}
func increaseRestartDelay(restartDelay time.Duration) time.Duration {
restartDelay = restartDelay * 2
if restartDelay > maxRestartDelay {
restartDelay = maxRestartDelay
}
return restartDelay
}