-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
226 lines (188 loc) · 6.16 KB
/
pipeline.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
package shutdown
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
// Manager is a shutdown pipeline manager that can be configured to run through a number of parallel and sequencial
// steps when a shutdown is triggered. In order to start the shutdown procedure upon receiving a kernel Interrupt
// signal, use WaitForInterrupt() blocking method.
type Manager struct {
steps []shutdownStep
timeout time.Duration
completionFuncnc func()
logger Logger
lock sync.Mutex
}
// New creates a new shutdown pipeline.
func New() *Manager {
return &Manager{
logger: NewStandardLogger(log.Default()),
}
}
// SetTimeout sets the shutdown pipeline timeout. This indicates that when shutdown is triggered, the entire pipeline
// iteration must finish within the duration specified.
//
// NOTE: If the pipeline times out, the shutdown method is still called and some of the steps in the pipeline will
// still get scheduled but the blocking method (Trigger or WaitForInterrupt) will return immediately without waiting
// for the rest of the shutdown steps to complete.
func (m *Manager) SetTimeout(duration time.Duration) {
m.lock.Lock()
defer m.lock.Unlock()
m.timeout = duration
}
// SetCompletionFunc sets a function to get called after all of the shutdown steps have been executed. Regardless of
// panics or errors, this function will always get executed as the very last step. Even when a the pipeline times out,
// this function gets called before returning.
func (m *Manager) SetCompletionFunc(f func()) {
m.lock.Lock()
defer m.lock.Unlock()
m.completionFuncnc = f
}
// SetLogger sets the shutdown logger. If set to nil, no logs will be written.
func (m *Manager) SetLogger(logger Logger) {
m.lock.Lock()
defer m.lock.Unlock()
m.logger = logger
}
// AddSteps adds parallel shutdown steps. These steps will be executed at the same time together or along
// with previously added steps if they are also able to run in parallel. In another word, calling AddSteps(a) and
// AddSteps(b) is same as AddSteps(a, b)
func (m *Manager) AddSteps(handlers ...NamedHandler) {
m.lock.Lock()
defer m.lock.Unlock()
if len(m.steps) == 0 {
m.steps = append(m.steps, shutdownStep{handlers: handlers, parallel: true})
return
}
lastStep := m.steps[len(m.steps)-1]
if lastStep.parallel {
lastStep.handlers = append(lastStep.handlers, handlers...)
m.steps[len(m.steps)-1] = lastStep
return
}
m.steps = append(m.steps, shutdownStep{handlers: handlers, parallel: true})
}
// AddSequence adds sequencial steps meaning that these handlers will be executed one at a time and in the same order
// given.
// Calling AddSequence(a) and AddSequence(b) is same as AddSequence(a, b)
func (m *Manager) AddSequence(handlers ...NamedHandler) {
m.lock.Lock()
defer m.lock.Unlock()
if len(m.steps) == 0 {
m.steps = append(m.steps, shutdownStep{handlers: handlers, parallel: false})
return
}
lastStep := m.steps[len(m.steps)-1]
if !lastStep.parallel {
lastStep.handlers = append(lastStep.handlers, handlers...)
m.steps[len(m.steps)-1] = lastStep
return
}
m.steps = append(m.steps, shutdownStep{handlers: handlers, parallel: false})
}
// AddParallelSequence is similar to AddSequence but it will execute the handlers all at the same time.
// AddParallelSequence(a) and AddParallelSequence(b) is not the same as AddParallelSequence(a, b). In the former, a
// runs and upon completion, b starts whereas in the latter case a and b both get started at the same time.
func (m *Manager) AddParallelSequence(handlers ...NamedHandler) {
m.lock.Lock()
defer m.lock.Unlock()
m.steps = append(m.steps, shutdownStep{handlers: handlers, parallel: true})
}
// Trigger starts the shutdown pipeline immediately. It will acquire a lock on the pipeline so all changes to the
// pipeline get blocked until the pipeline has completed. Panics and errors are all handled.
func (m *Manager) Trigger(ctx context.Context) {
m.lock.Lock()
defer m.lock.Unlock()
if len(m.steps) == 0 {
return
}
if m.timeout != 0 {
newCtx, cancel := context.WithTimeout(ctx, m.timeout)
ctx = newCtx
defer cancel()
}
errorCount := 0
resultChannel := make(chan handlerResult)
mainLoop:
for _, step := range m.steps {
remainingHandlers := len(step.handlers)
go func() {
for _, handler := range step.handlers {
if step.parallel {
go func(h NamedHandler) {
m.executeHandler(ctx, h, resultChannel)
}(handler)
} else {
m.executeHandler(ctx, handler, resultChannel)
}
}
}()
for remainingHandlers > 0 {
select {
case result := <-resultChannel:
if result.Err != nil {
errorCount++
m.err(result.HandlerName + " shutdown failed: " + result.Err.Error())
} else {
m.info(result.HandlerName + " shutdown completed")
}
remainingHandlers--
case <-ctx.Done():
m.err("context canceled")
errorCount++
break mainLoop
}
}
}
if m.completionFuncnc != nil {
m.completionFuncnc()
}
if errorCount > 0 {
m.err(fmt.Sprintf("shutdown pipeline completed with %d errors", errorCount))
} else {
m.info("shutdown pipeline completed with no errors")
}
}
func (m *Manager) info(text string) {
if m.logger != nil {
m.logger.Info(text)
}
}
func (m *Manager) err(text string) {
if m.logger != nil {
m.logger.Error(text)
}
}
func (m *Manager) executeHandler(ctx context.Context, handler NamedHandler, resultChannel chan<- handlerResult) {
var err error
defer func() {
if panicErr := recover(); panicErr != nil {
resultChannel <- handlerResult{HandlerName: handler.Name(), Err: fmt.Errorf("panic: %s", panicErr)}
} else {
resultChannel <- handlerResult{HandlerName: handler.Name(), Err: err}
}
}()
err = handler.HandleShutdown(ctx)
}
// WaitForInterrupt blocks until an interrupt signal is received and all shutdown steps have been executed.
func (m *Manager) WaitForInterrupt() {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
<-exit
m.info("received interrupt signal, starting shutdown procedures...")
m.Trigger(context.Background())
}
type shutdownStep struct {
handlers []NamedHandler
parallel bool
}
type handlerResult struct {
Err error
HandlerName string
}