-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.go
416 lines (347 loc) · 9.85 KB
/
task.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"sync"
"sync/atomic"
"syscall"
"time"
)
// Task represents one running process/application
type Task struct {
Command string `json:"command"`
Args []string `json:"args"`
WorkDir string `json:"workdir"`
Wait int `json:"wait"`
Pause int `json:"restartPause"`
StartTime int `json:"startTime"`
OneTime bool `json:"oneTime"`
// hidden fields
stopped bool // indicate to don't restart after "die"
oneTimeRunning bool // indicate that we're just running
oneTimeMutex sync.Mutex // mutex for oneTimeRunning
status atomic.Value // string like "none" (not started at all), "running", "finished", "restarting"
timeStarted atomic.Value // when task started (time.Time / nil)
timeFinished atomic.Value // last task finished time (time.Time / nil)
name string // duplicate name from config
cSignal chan os.Signal // send signal to process
rSignal chan bool // restart signal
fSignal chan bool // log flush signal
sSignal chan bool // signal to stop task
eSignal chan bool // exit loop, trigered on task delete
}
// TaskStatus is simple struct suitable for marshaling
type TaskStatus struct {
Status string `json:"status"`
Started time.Time `json:"started,omitempty"`
Finished time.Time `json:"finished,omitempty"`
}
// GetStatus return task's status in struct
func (t *Task) GetStatus() TaskStatus {
result := TaskStatus{}
if status, ok := t.status.Load().(string); ok {
result.Status = status
} else {
result.Status = "not started"
}
if started, ok := t.timeStarted.Load().(time.Time); ok {
result.Started = started
}
if finished, ok := t.timeFinished.Load().(time.Time); ok {
result.Finished = finished
}
return result
}
// Run task one time
func (t *Task) Run(input []byte) {
defer func() {
t.oneTimeMutex.Lock()
t.oneTimeRunning = false
t.oneTimeMutex.Unlock()
}()
t.cSignal = make(chan os.Signal)
t.sSignal = make(chan bool)
config := aConfig.Load()
// for log rotation we need layer in the middle
writer := logWithRotation(fmt.Sprintf("%s/%s%s.log",
config.LogDir, config.LogPrefix, t.name),
config.LogSuffixDate, t.fSignal, config.LogDate,
t.name, config.GrayLog)
defer func() {
err := writer.Close()
if nil != err {
log.Println("Error closing output: ", err)
}
}()
fmt.Fprintf(writer, "[minisv] Starting %s %v\n", t.Command, t.Args)
cmd := exec.Command(t.Command, t.Args...)
cmd.Stdout = writer
cmd.Stderr = writer
if nil != input {
cmd.Stdin = bytes.NewReader(input)
}
if t.WorkDir != "" {
cmd.Dir = t.WorkDir
}
t.timeStarted.Store(time.Now())
t.status.Store("starting")
err := cmd.Start()
if nil != err {
fmt.Fprintf(writer, "[minisv] Error starting %s (%s): %v\n",
t.name, t.Command, err)
t.status.Store("start failed: " + err.Error())
t.timeFinished.Store(time.Now())
return
}
t.status.Store("running")
cmdDone := make(chan error)
go func() {
cmdDone <- cmd.Wait()
}()
killIt := make(chan bool)
killCanceled := false
itsDone:
for {
select {
case err = <-cmdDone:
killCanceled = true
break itsDone
case sig := <-t.cSignal:
fmt.Fprintln(writer, "[minisv] Sending ", sig,
" signal to process ", cmd.Process.Pid)
err = cmd.Process.Signal(sig)
if nil != err {
fmt.Fprintln(writer, "[minisv] Error sending ", sig, ": ", err)
}
case <-t.sSignal:
fmt.Fprintln(writer, "[minisv] Stopping task")
err = cmd.Process.Signal(syscall.SIGTERM)
if nil != err {
fmt.Fprintln(writer, "[minisv] Error sending SIGTERM: ", err)
}
go func() {
time.Sleep(time.Duration(t.Wait) * time.Second)
killIt <- true
}()
case <-killIt:
if !killCanceled {
fmt.Fprintln(writer, "[minisv] Killing task")
err = cmd.Process.Signal(syscall.SIGKILL)
if nil != err {
fmt.Fprintln(writer, "[minisv] Error sending SIGKILL: ", err)
}
}
}
}
if nil != err {
t.status.Store("finished with error: " + err.Error())
fmt.Fprintf(writer, "[minisv] Command %s (%s) ended with error: %v\n",
t.name, t.Command, err)
} else {
t.status.Store("finished")
}
t.timeFinished.Store(time.Now())
}
// Loop task runinng and restarting
func (t *Task) Loop(cExit chan bool, wg *sync.WaitGroup) {
defer wg.Done()
// init channels
t.cSignal = make(chan os.Signal)
t.rSignal = make(chan bool)
t.fSignal = make(chan bool)
t.sSignal = make(chan bool)
t.eSignal = make(chan bool)
config := aConfig.Load()
// for log rotation we need layer in the middle
out := logWithRotation(fmt.Sprintf("%s/%s%s.log",
config.LogDir, config.LogPrefix, t.name),
config.LogSuffixDate, t.fSignal, config.LogDate,
t.name, config.GrayLog)
defer func() {
err := out.Close()
if nil != err {
log.Println("Error closing output: ", err)
}
}()
var err error
// true - main is cmd1, false - main is cmd2 :)
stage := true
startNext := func(okstatus string) (*exec.Cmd, chan error, error) {
fmt.Fprintf(out, "[minisv] Starting %s %v\n", t.Command, t.Args)
cmd := exec.Command(t.Command, t.Args...)
cmd.Stdout = out
cmd.Stderr = out
if t.WorkDir != "" {
cmd.Dir = t.WorkDir
}
t.status.Store("starting")
t.timeStarted.Store(time.Now())
err = cmd.Start()
if nil != err {
t.status.Store("Error starting: " + err.Error())
fmt.Fprintf(out, "[minisv] Error starting %s (%s): %v\n",
t.name, t.Command, err)
time.Sleep(time.Second * time.Duration(t.Pause))
return nil, nil, err
}
t.status.Store(okstatus)
cmdDone := make(chan error)
go func() {
cmdDone <- cmd.Wait()
}()
return cmd, cmdDone, nil
}
var cmd1, cmd2 *exec.Cmd
var done1, done2 chan error
var run1, run2 bool
for {
if !t.stopped {
if stage && !run1 {
cmd1, done1, err = startNext("started")
run1 = nil == err
}
if !stage && !run2 {
cmd2, done2, err = startNext("started")
run2 = nil == err
}
}
select {
case err = <-done1:
run1 = false
if stage {
t.timeFinished.Store(time.Now())
if nil == err {
fmt.Fprintln(out, string("[minisv] Main process normal exit"))
t.status.Store("finished")
} else {
fmt.Fprintln(out, "[minisv] Main process exited, ", err)
t.status.Store("finished with error: " + err.Error())
}
} else {
if nil == err {
fmt.Fprintln(out, "[minisv] Old process normal exit")
} else {
fmt.Fprintln(out, "[minisv] Old process exited, ", err)
}
// don't need wait after old process exit
continue
}
case err = <-done2:
run2 = false
if stage {
if nil == err {
fmt.Fprintln(out, "[minisv] Old process normal exit")
} else {
fmt.Fprintln(out, "[minisv] Old process exited, ", err)
}
// don't need wait after old process exit
continue
} else {
t.timeFinished.Store(time.Now())
if nil == err {
fmt.Fprintln(out, "[minisv] Main process normal exit")
t.status.Store("finished")
} else {
fmt.Fprintln(out, "[minisv] Main process exited, ", err)
t.status.Store("finished with error: " + err.Error())
}
}
case sig := <-t.cSignal:
if stage {
fmt.Fprintln(out, "[minisv] Sending ", sig,
" signal to process ", cmd1.Process.Pid)
err = cmd1.Process.Signal(sig)
if nil != err {
fmt.Fprintln(out, "[minisv] Error sending ", sig, ": ", err)
}
} else {
fmt.Fprintln(out, "[minisv] Sending ", sig,
" signal to process ", cmd2.Process.Pid)
err = cmd2.Process.Signal(sig)
if nil != err {
fmt.Fprintln(out, "[minisv] Error sending ", sig, ": ", err)
}
}
continue
case <-t.sSignal:
fmt.Fprintln(out, "[minisv] Stopping task")
t.stopped = true
if stage {
termChild(run1, cmd1, done1, t.Wait, out, nil)
run1 = false
} else {
termChild(run2, cmd2, done2, t.Wait, out, nil)
run2 = false
}
t.timeFinished.Store(time.Now())
t.status.Store("stoped")
continue
case <-t.rSignal:
if t.stopped {
t.stopped = false
fmt.Fprintln(out, "[minisv] Starting task")
} else {
fmt.Fprintln(out, "[minisv] Doing graceful restart")
// castling of running processes
if stage {
cmd2, done2, err = startNext("restart validation")
run2 = nil == err
} else {
cmd1, done1, err = startNext("restart validation")
run1 = nil == err
}
if nil != err {
t.status.Store("new instance failed")
fmt.Fprintln(out,
"[minisv] Unable to start new instance, continue using old one")
continue
}
var exited bool
if stage {
exited = waitForErrChan(done2, time.Second*time.Duration(t.StartTime))
} else {
exited = waitForErrChan(done1, time.Second*time.Duration(t.StartTime))
}
if exited {
t.status.Store("new instance exited too fast")
fmt.Fprintln(out,
"[minisv] New instance exited too fast, continue using old one")
continue
}
stage = !stage
t.status.Store("restart ok")
fmt.Fprintln(out, "[minisv] New instance running, terminating old one")
if stage {
termChild(run2, cmd2, done2, t.Wait, out, nil)
} else {
termChild(run1, cmd1, done1, t.Wait, out, nil)
}
}
continue
case <-cExit:
fmt.Fprintln(out, "[minisv] Sending term signal to childs")
smallWg := sync.WaitGroup{}
smallWg.Add(2)
go termChild(run1, cmd1, done1, t.Wait, out, &smallWg)
go termChild(run2, cmd2, done2, t.Wait, out, &smallWg)
smallWg.Wait()
return
case <-t.eSignal:
fmt.Fprintln(out, "[minisv] {taskExit} Sending term signal to childs")
smallWg := sync.WaitGroup{}
smallWg.Add(2)
go termChild(run1, cmd1, done1, t.Wait, out, &smallWg)
go termChild(run2, cmd2, done2, t.Wait, out, &smallWg)
smallWg.Wait()
return
}
if t.Pause != 0 {
fmt.Fprintln(out, "Waiting ",
time.Second*time.Duration(t.Pause), " before restart")
time.Sleep(time.Second * time.Duration(t.Pause))
}
}
}