-
Notifications
You must be signed in to change notification settings - Fork 56
/
exec_runner.go
332 lines (282 loc) · 7.79 KB
/
exec_runner.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright (C) 2020 Graylog, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the Server Side Public License, version 1,
// as published by MongoDB, Inc.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Server Side Public License for more details.
//
// You should have received a copy of the Server Side Public License
// along with this program. If not, see
// <http://www.mongodb.com/licensing/server-side-public-license>.
package daemon
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"sync/atomic"
"syscall"
"time"
"github.com/flynn-archive/go-shlex"
"github.com/Graylog2/collector-sidecar/backends"
"github.com/Graylog2/collector-sidecar/common"
"github.com/Graylog2/collector-sidecar/context"
"github.com/Graylog2/collector-sidecar/logger"
)
type ExecRunner struct {
RunnerCommon
exec string
args string
stderr, stdout string
isRunning atomic.Value
isSupervised atomic.Value
restartCount int
startTime time.Time
cmd *exec.Cmd
signals chan string
}
func init() {
if err := RegisterBackendRunner("exec", NewExecRunner); err != nil {
log.Fatal(err)
}
}
func NewExecRunner(backend backends.Backend, context *context.Ctx) Runner {
r := &ExecRunner{
RunnerCommon: RunnerCommon{
name: backend.Name,
context: context,
backend: backend,
},
exec: backend.ExecutablePath,
args: backend.ExecuteParameters,
restartCount: 1,
signals: make(chan string),
stderr: filepath.Join(context.UserConfig.LogPath, backend.Name+"_stderr.log"),
stdout: filepath.Join(context.UserConfig.LogPath, backend.Name+"_stdout.log"),
}
// set default state
r.setRunning(false)
r.setSupervised(false)
r.signalProcessor()
r.startSupervisor()
return r
}
func (r *ExecRunner) Name() string {
return r.name
}
func (r *ExecRunner) Running() bool {
return r.isRunning.Load().(bool)
}
func (r *ExecRunner) setRunning(state bool) {
r.isRunning.Store(state)
}
func (r *ExecRunner) Supervised() bool {
return r.isSupervised.Load().(bool)
}
func (r *ExecRunner) setSupervised(state bool) {
r.isSupervised.Store(state)
}
func (r *ExecRunner) SetDaemon(d *DaemonConfig) {
r.daemon = d
}
func (r *ExecRunner) GetBackend() *backends.Backend {
return &r.backend
}
func (r *ExecRunner) SetBackend(b backends.Backend) {
r.backend = b
r.name = b.Name
r.stderr = filepath.Join(r.context.UserConfig.LogPath, b.Name+"_stderr.log")
r.stdout = filepath.Join(r.context.UserConfig.LogPath, b.Name+"_stdout.log")
r.exec = b.ExecutablePath
r.args = b.ExecuteParameters
r.restartCount = 1
}
func (r *ExecRunner) ResetRestartCounter() {
r.restartCount = 1
}
func (r *ExecRunner) ValidateBeforeStart() error {
err := r.backend.CheckExecutableAgainstAccesslist(r.context)
if err != nil {
r.backend.SetStatusLogErrorf(err.Error())
return err
}
_, err = exec.LookPath(r.exec)
if err != nil {
return r.backend.SetStatusLogErrorf("Failed to find collector executable %s: %s", r.exec, err)
}
if r.Running() {
return errors.New("Failed to start collector, it's already running")
}
return nil
}
func (r *ExecRunner) startSupervisor() {
r.restartCount = 1
go func() {
for {
// prevent cpu lock
time.Sleep(1 * time.Second)
// ignore regular shutdown
if !r.Supervised() {
continue
}
// check if process exited
if r.Running() {
continue
}
// after 60 seconds we can reset the restart counter
if time.Since(r.startTime) > 60*time.Second {
r.restartCount = 1
}
// don't continue to restart after 3 tries, stop the supervisor and wait for a configuration update
// or manual restart
if r.restartCount > 3 {
r.backend.SetStatusLogErrorf("Unable to start collector after 3 tries, giving up!")
if output := r.readCollectorOutput(); output != "" {
log.Errorf("[%s] Collector output: %s", r.name, output)
r.backend.SetVerboseStatus(output)
}
r.setSupervised(false)
continue
}
log.Errorf("[%s] Backend finished unexpectedly, trying to restart %d/3.", r.name, r.restartCount)
r.restartCount += 1
r.Restart()
}
}()
}
func (r *ExecRunner) readCollectorOutput() string {
output := ""
for _, file := range []string{r.stderr, r.stdout} {
out, err := ioutil.ReadFile(file)
if err == nil {
output += string(out)
}
}
return output
}
func (r *ExecRunner) start() error {
if err := r.ValidateBeforeStart(); err != nil {
return err
}
// setup process environment
var err error
var quotedArgs []string
if runtime.GOOS == "windows" {
quotedArgs = common.CommandLineToArgv(r.args)
} else {
quotedArgs, err = shlex.Split(r.args)
}
if err != nil {
return err
}
r.cmd = exec.Command(r.exec, quotedArgs...)
r.cmd.Dir = r.daemon.Dir
r.cmd.Env = append(os.Environ(), r.daemon.Env...)
// start the actual process and don't block
r.startTime = time.Now()
r.run()
r.setSupervised(true)
return nil
}
func (r *ExecRunner) Shutdown() error {
r.signals <- "shutdown"
return nil
}
func (r *ExecRunner) stop() error {
// deactivate supervisor
r.setSupervised(false)
// if the command hasn't been started yet or doesn't run anymore, just return
if r.cmd == nil || r.cmd.Process == nil {
return nil
}
log.Infof("[%s] Stopping", r.name)
// give the chance to cleanup resources
if r.cmd.Process != nil && runtime.GOOS != "windows" {
r.cmd.Process.Signal(syscall.SIGHUP)
time.Sleep(2 * time.Second)
}
// in doubt kill the process
if r.Running() {
log.Debugf("[%s] SIGHUP ignored, killing process", r.Name())
err := r.cmd.Process.Kill()
if err != nil {
log.Debugf("[%s] Failed to kill process %s", r.Name(), err)
}
}
r.backend.SetStatus(backends.StatusStopped, "Stopped", "")
return nil
}
func (r *ExecRunner) Restart() error {
r.signals <- "restart"
return nil
}
func (r *ExecRunner) restart() error {
if r.Running() {
r.stop()
for timeout := 0; r.Running() || timeout >= 5; timeout++ {
log.Debugf("[%s] waiting for process to finish...", r.Name())
time.Sleep(1 * time.Second)
}
}
// wipe collector log files after each try
os.Truncate(r.stderr, 0)
os.Truncate(r.stdout, 0)
r.start()
return nil
}
func (r *ExecRunner) run() {
log.Infof("[%s] Starting (%s driver)", r.name, r.backend.ServiceType)
if r.stderr != "" {
err := common.CreatePathToFile(r.stderr)
if err != nil {
r.backend.SetStatusLogErrorf("Failed to create path to collector's stderr log: %s", r.stderr)
}
f := logger.GetRotatedLog(r.stderr, r.context.UserConfig.LogRotateMaxFileSize, r.context.UserConfig.LogRotateKeepFiles)
defer f.Close()
r.cmd.Stderr = f
}
if r.stdout != "" {
err := common.CreatePathToFile(r.stdout)
if err != nil {
r.backend.SetStatusLogErrorf("Failed to create path to collector's stdout log: %s", r.stdout)
}
f := logger.GetRotatedLog(r.stdout, r.context.UserConfig.LogRotateMaxFileSize, r.context.UserConfig.LogRotateKeepFiles)
defer f.Close()
r.cmd.Stdout = f
}
r.backend.SetStatus(backends.StatusRunning, "Running", "")
err := r.cmd.Start()
if err != nil {
r.backend.SetStatusLogErrorf("Failed to start collector: %s", err)
}
// wait for process exit in the background. Ensure single cmd.Wait() call
go func() {
r.setRunning(true)
r.cmd.Wait()
r.setRunning(false)
}()
}
// process signals sequentially to prevent race conditions with the supervisor
func (r *ExecRunner) signalProcessor() {
go func() {
seq := 0
for {
cmd := <-r.signals
seq++
log.Debugf("[signal-processor] (seq=%d) handling cmd: %v", seq, cmd)
switch cmd {
case "restart":
r.restart()
case "shutdown":
r.stop()
}
log.Debugf("[signal-processor] (seq=%d) cmd done: %v", seq, cmd)
}
}()
}