-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.go
126 lines (110 loc) · 3.57 KB
/
triggers.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
package jasper
import (
"context"
"syscall"
"time"
"github.com/tychoish/grip"
"github.com/tychoish/grip/message"
"github.com/tychoish/jasper/options"
)
// ProcessTrigger describes the way to write cleanup functions for
// processes, which provide ways of adding behavior to processes after
// they complete. A ProcessTrigger can read the fields within
// ProcessInfo.Options but should not mutate them.
type ProcessTrigger func(ProcessInfo)
// ProcessTriggerSequence is simply a convenience type to simplify
// running more than one triggered operation.
type ProcessTriggerSequence []ProcessTrigger
// Run loops over triggers and calls each of them successively.
func (s ProcessTriggerSequence) Run(info ProcessInfo) {
for _, trigger := range s {
trigger(info)
}
}
// SignalTrigger describes the way to write hooks that will execute
// before a process is about to be signaled. It returns a bool
// indicating if the signal should be skipped after execution of the
// trigger. A SignalTrigger can read the fields within ProcessInfo.Options but
// should not mutate them.
type SignalTrigger func(ProcessInfo, syscall.Signal) (skipSignal bool)
// SignalTriggerSequence is a convenience type to simplify running
// more than one signal trigger.
type SignalTriggerSequence []SignalTrigger
// Run loops over signal triggers and calls each of them successively.
// It returns a boolean indicating whether or not the signal should
// be skipped after executing all of the signal triggers.
func (s SignalTriggerSequence) Run(info ProcessInfo, sig syscall.Signal) (skipSignal bool) {
skipSignal = false
for _, trigger := range s {
skipSignal = trigger(info, sig) || skipSignal
}
return
}
// SignalTriggerID is the unique representation of a signal trigger.
type SignalTriggerID string
const (
// CleanTerminationSignalTrigger is the ID for the signal trigger to use for
// termination of processes with exit code 0.
CleanTerminationSignalTrigger SignalTriggerID = "clean_terminate"
)
func makeOptionsCloseTrigger() ProcessTrigger {
return func(info ProcessInfo) {
grip.Warning(message.WrapError(info.Options.Close(),
"error occurred while closing creation options"))
}
}
func MakeDefaultTrigger(ctx context.Context, m Manager, opts *options.Create, parentID string) ProcessTrigger {
deadline, hasDeadline := ctx.Deadline()
timeout := time.Until(deadline)
return func(info ProcessInfo) {
switch {
case info.Timeout:
var (
newctx context.Context
cancel context.CancelFunc
)
for _, opt := range opts.OnTimeout {
if hasDeadline {
newctx, cancel = context.WithTimeout(context.Background(), timeout)
} else {
newctx, cancel = context.WithCancel(ctx)
}
p, err := m.CreateProcess(newctx, opt.Copy())
if err != nil {
grip.Warning(message.WrapError(err, message.Fields{
"trigger": "on-timeout",
"parent": parentID,
}))
cancel()
continue
}
p.Tag(parentID)
_ = p.RegisterTrigger(ctx, func(_ ProcessInfo) { cancel() })
}
case info.Successful:
for _, opt := range opts.OnSuccess {
p, err := m.CreateProcess(ctx, opt.Copy())
if err != nil {
grip.Warning(message.WrapError(err, message.Fields{
"trigger": "on-success",
"parent": parentID,
}))
continue
}
p.Tag(parentID)
}
case !info.Successful:
for _, opt := range opts.OnFailure {
p, err := m.CreateProcess(ctx, opt.Copy())
if err != nil {
grip.Warning(message.WrapError(err, message.Fields{
"trigger": "on-failure",
"parent": parentID,
}))
continue
}
p.Tag(parentID)
}
}
}
}