This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
forked from waffle-iron/core0
/
job.go
454 lines (383 loc) · 10 KB
/
job.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package pm
// #cgo LDFLAGS: -lcap
// #include <sys/capability.h>
import "C"
import (
"context"
"fmt"
"runtime"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/threefoldtech/0-core/base/pm/stream"
)
const (
StandardStreamBufferSize = 100 //buffer size for each of stdout and stderr
GenericStreamBufferSize = 10 //we only keep last 100 message of all types.
)
type Job interface {
Command() *Command
Signal(sig syscall.Signal) error
Process() Process
Wait() *JobResult
WaitContext(ctx context.Context) *JobResult
StartTime() int64
Subscribe(stream.MessageHandler)
Unschedule()
}
type jobImb struct {
command *Command
factory ProcessFactory
signal chan syscall.Signal
unschedule chan struct{}
unscheduleOnce sync.Once
process Process
hooks []RunnerHook
startTime time.Time
backlog *stream.Buffer
subscribers []stream.MessageHandler
o sync.Once
result *JobResult
wg sync.WaitGroup
registerPID func(GetPID) (int, error)
waitPID func(int) syscall.WaitStatus
running int32
backoff BackOff
}
/*
NewRunner creates a new r object that is bind to this PM instance.
:manager: Bind this r to this PM instance
:command: Command to run
:factory: Process factory associated with command type.
:hooksDelay: Fire the hooks after this delay in seconds if the r is still running. Basically it's a delay for if the
command didn't exit by then we assume it's running successfully
values are:
- 1 means hooks are only called when the command exits
0 means use default delay (default is 2 seconds)
> 0 Use that delay
:hooks: Optionals hooks that are called if the r is considered RUNNING successfully.
The r is considered running, if it ran with no errors for 2 seconds, or exited before the 2 seconds passes
with SUCCESS exit code.
*/
func newJob(command *Command, factory ProcessFactory, hooks ...RunnerHook) *jobImb {
job := &jobImb{
command: command,
factory: factory,
signal: make(chan syscall.Signal, 5), //enough buffer for 5 signals
unschedule: make(chan struct{}),
hooks: hooks,
backlog: stream.NewBuffer(GenericStreamBufferSize),
registerPID: registerPID,
waitPID: waitPID,
backoff: BackOff{
Delay: 2 * time.Second,
Multiply: 1.5,
Max: 5 * time.Minute,
},
}
job.wg.Add(1)
return job
}
func newTestJob(command *Command, factory ProcessFactory, hooks ...RunnerHook) *jobImb {
job := newJob(command, factory, hooks...)
var testTable TestingPIDTable
job.registerPID = testTable.RegisterPID
job.waitPID = testTable.WaitPID
return job
}
func (r *jobImb) Command() *Command {
return r.command
}
func (r *jobImb) timeout() <-chan time.Time {
var timeout <-chan time.Time
if r.command.MaxTime > 0 {
timeout = time.After(time.Duration(r.command.MaxTime) * time.Second)
}
return timeout
}
//set the bounding set for current thread, of course this is un-reversable once set on the
//pm it affects all threads from now on.
func (r *jobImb) setUnprivileged() {
//drop bounding set for children.
bound := []uintptr{
C.CAP_SETPCAP,
C.CAP_SYS_MODULE,
C.CAP_SYS_RAWIO,
C.CAP_SYS_PACCT,
C.CAP_SYS_ADMIN,
C.CAP_SYS_NICE,
C.CAP_SYS_RESOURCE,
C.CAP_SYS_TIME,
C.CAP_SYS_TTY_CONFIG,
C.CAP_AUDIT_CONTROL,
C.CAP_MAC_OVERRIDE,
C.CAP_MAC_ADMIN,
C.CAP_NET_ADMIN,
C.CAP_SYSLOG,
C.CAP_DAC_READ_SEARCH,
C.CAP_LINUX_IMMUTABLE,
C.CAP_NET_BROADCAST,
C.CAP_IPC_LOCK,
C.CAP_IPC_OWNER,
C.CAP_SYS_PTRACE,
C.CAP_SYS_BOOT,
C.CAP_LEASE,
C.CAP_WAKE_ALARM,
C.CAP_BLOCK_SUSPEND,
}
for _, c := range bound {
syscall.Syscall6(syscall.SYS_PRCTL, syscall.PR_CAPBSET_DROP,
c, 0, 0, 0, 0)
}
}
func (r *jobImb) Subscribe(listener stream.MessageHandler) {
//TODO: a race condition might happen here because, while we send the backlog
//a new message might arrive and missed by this listener
for l := r.backlog.Front(); l != nil; l = l.Next() {
switch v := l.Value.(type) {
case *stream.Message:
listener(v)
}
}
r.subscribers = append(r.subscribers, listener)
}
func (r *jobImb) callback(msg *stream.Message) {
defer func() {
//protection against subscriber crashes.
if err := recover(); err != nil {
log.Warningf("error in subsciber: %v", err)
}
}()
//check subscribers here.
msgCallback(r.command, msg)
for _, sub := range r.subscribers {
sub(msg)
}
}
func (r *jobImb) run(unprivileged bool) (jobresult *JobResult) {
r.startTime = time.Now()
jobresult = NewJobResult(r.command)
jobresult.State = StateError
defer func() {
jobresult.StartTime = int64(time.Duration(r.startTime.UnixNano()) / time.Millisecond)
endtime := time.Now()
jobresult.Time = endtime.Sub(r.startTime).Nanoseconds() / int64(time.Millisecond)
if err := recover(); err != nil {
jobresult.State = StateError
jobresult.Critical = fmt.Sprintf("PANIC(%v)", err)
}
}()
r.process = r.factory(r, r.command)
ps := r.process
runtime.LockOSThread()
if unprivileged {
r.setUnprivileged()
}
channel, err := ps.Run()
runtime.UnlockOSThread()
if err != nil {
var code uint32
if err, ok := err.(RunError); ok {
code = err.Code()
}
//this basically means r couldn't spawn
//which indicates a problem with the command itself. So restart won't
//do any good. It's better to terminate it immediately.
jobresult.Code = code
jobresult.Data = err.Error()
return jobresult
}
var result *stream.Message
var critical string
stdout := stream.NewBuffer(StandardStreamBufferSize)
stderr := stream.NewBuffer(StandardStreamBufferSize)
timeout := r.timeout()
handlersTicker := time.NewTicker(1 * time.Second)
defer handlersTicker.Stop()
loop:
for {
select {
case sig := <-r.signal:
if ps, ok := ps.(Signaler); ok {
ps.Signal(sig)
}
case <-timeout:
if ps, ok := ps.(Signaler); ok {
ps.Signal(syscall.SIGKILL)
jobresult.State = StateTimeout
}
case <-handlersTicker.C:
d := time.Now().Sub(r.startTime)
for _, hook := range r.hooks {
go hook.Tick(d)
}
case message := <-channel:
r.backlog.Append(message)
meta := message.Meta
//messages with Exit flags are always the last.
if meta.Is(stream.ExitSuccessFlag) {
jobresult.State = StateSuccess
}
if meta.Assert(stream.ResultMessageLevels...) {
//a result message.
result = message
} else if meta.Assert(stream.LevelStdout) {
stdout.Append(message.Message)
} else if meta.Assert(stream.LevelStderr) {
stderr.Append(message.Message)
} else if meta.Assert(stream.LevelCritical) {
critical = message.Message
}
for _, hook := range r.hooks {
hook.Message(message)
}
//FOR BACKWARD compatibility, we drop the code part from the message meta because watchers
//like watchdog and such are not expecting a code part in the meta (yet)
code := meta.Code()
message.Meta = meta.Base()
//END of BACKWARD compatibility code
//by default, all messages are forwarded to the manager for further processing.
r.callback(message)
if message.Meta.Is(stream.ExitSuccessFlag | stream.ExitErrorFlag) {
jobresult.Code = code
break loop
}
}
}
r.process = nil
//consume channel to the end to allow r to cleanup properly
for range channel {
//noop.
}
if result != nil {
jobresult.Level = result.Meta.Level()
jobresult.Data = result.Message
}
jobresult.Streams = Streams{
stdout.String(),
stderr.String(),
}
jobresult.Critical = critical
return jobresult
}
func (r *jobImb) start(unprivileged bool) {
atomic.StoreInt32(&r.running, 1)
runs := 0
var result *JobResult
defer func() {
atomic.StoreInt32(&r.running, 0)
close(r.signal)
r.Unschedule()
if result != nil {
r.result = result
callback(r.command, result)
r.o.Do(func() {
r.wg.Done()
})
}
cleanUp(r)
}()
loop:
for {
result = r.run(unprivileged)
for _, hook := range r.hooks {
hook.Exit(result.State)
}
if r.command.Flags.Protected {
//immediate restart
delay := r.backoff.Duration()
log.Debugf("Re-spawning protected service '%s' in %s", r.command.ID, delay)
<-time.After(delay)
continue
}
restarting := false
var restartIn time.Duration
if result.State != StateSuccess && r.command.MaxRestart > 0 {
runs++
if runs < r.command.MaxRestart {
log.Debugf("Restarting '%s' due to abnormal exit status, trials: %d/%d", r.command, runs+1, r.command.MaxRestart)
restarting = true
restartIn = 1 * time.Second
}
}
if r.command.RecurringPeriod > 0 {
restarting = true
restartIn = time.Duration(r.command.RecurringPeriod) * time.Second
}
if restarting {
log.Debugf("recurring '%s' in %s", r.command, restartIn)
select {
case <-time.After(restartIn):
case <-r.unschedule:
break loop
}
} else {
break
}
}
}
func (r *jobImb) Unschedule() {
r.unscheduleOnce.Do(func() {
close(r.unschedule)
})
}
func (r *jobImb) Signal(sig syscall.Signal) error {
if atomic.LoadInt32(&r.running) != 1 {
return fmt.Errorf("job is not running")
}
select {
case r.signal <- sig:
return nil
default:
return fmt.Errorf("job not receiving signals")
}
}
func (r *jobImb) Process() Process {
return r.process
}
func (r *jobImb) Wait() *JobResult {
r.wg.Wait()
return r.result
}
func (r *jobImb) WaitContext(ctx context.Context) *JobResult {
ch := make(chan struct{})
cleanup := func() {
close(ch)
}
var once sync.Once
defer once.Do(cleanup)
go func() {
r.Wait()
once.Do(cleanup)
}()
select {
case <-ch:
return r.Wait()
case <-ctx.Done():
return nil
}
}
func (r *jobImb) Terminate(sig syscall.Signal) error {
r.Unschedule()
r.command.Flags.Protected = false
return r.Signal(sig)
}
//implement PIDTable
//intercept pid registration to fire the correct hooks.
func (r *jobImb) RegisterPID(g GetPID) (int, error) {
pid, err := r.registerPID(g)
if err != nil {
return 0, err
}
for _, hook := range r.hooks {
go hook.PID(pid)
}
return 0, nil
}
func (r *jobImb) WaitPID(pid int) syscall.WaitStatus {
return r.waitPID(pid)
}
func (r *jobImb) StartTime() int64 {
return int64(time.Duration(r.startTime.UnixNano()) / time.Millisecond)
}