This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
supervisor.go
180 lines (146 loc) · 3.65 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package supervisor
import (
"log"
"os"
"os/exec"
"time"
)
var emptyFunc = func() {}
const (
minRestartDelay = time.Duration(0)
maxRestartDelay = 5 * time.Minute
durationToResetRestartDelay = 10 * time.Minute
)
// Supervisor provides functionality to start and supervise a background process
type Supervisor struct {
options *Options
cmd *exec.Cmd
keepRunning bool
}
// CreateSupervisors creates a new process supervisor
func CreateSupervisors(options *Options) []*Supervisor {
var supervisors []*Supervisor
for i := 0; i < options.Replicas; i++ {
templateData := &TemplateData{
Replica: i,
}
replicaOptions := options.processTemplates(templateData)
supervisor := &Supervisor{
options: replicaOptions,
}
supervisors = append(supervisors, supervisor)
}
return supervisors
}
// Run a process and supervise
func (s *Supervisor) Run() {
s.keepRunning = true
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
}
var afterRun []func()
if outputFile, closeFile, err := getOutputFile(s.options.RedirectStdout); err == nil {
s.cmd.Stdout = outputFile
afterRun = append(afterRun, closeFile)
} else {
log.Printf("RedirectStdout error: %v\n", err)
}
if outputFile, closeFile, err := getOutputFile(s.options.RedirectStderr); err == nil {
s.cmd.Stderr = outputFile
afterRun = append(afterRun, closeFile)
} else {
log.Printf("RedirectStderr error: %v\n", err)
}
start := time.Now()
err := s.cmd.Run()
duration := time.Now().Sub(start)
for _, fn := range afterRun {
fn()
}
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 getOutputFile(value string) (*os.File, func(), error) {
if value == "" {
return nil, emptyFunc, nil
}
switch value {
case "stdout":
return os.Stdout, emptyFunc, nil
case "stderr":
return os.Stderr, emptyFunc, nil
default:
outFile, err := os.OpenFile(value, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return nil, nil, err
}
return outFile, func() {
outFile.Close()
}, nil
}
}
func increaseRestartDelay(restartDelay time.Duration) time.Duration {
if restartDelay == 0 {
return 1 * time.Second
}
restartDelay = restartDelay * 2
if restartDelay > maxRestartDelay {
restartDelay = maxRestartDelay
}
return restartDelay
}