-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.go
81 lines (68 loc) · 2.91 KB
/
default.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
package shutdown
import (
"context"
"sync"
"time"
)
var (
defaultPipeline *Manager
defaultLock sync.Mutex
)
// Default returns the default manager. This method is thread-safe.
func Default() *Manager {
if defaultPipeline == nil {
defaultLock.Lock()
defer defaultLock.Unlock()
// if after acquiring the lock, pipeline is still nil, then set it.
if defaultPipeline == nil {
defaultPipeline = New()
}
}
return defaultPipeline
}
// WaitForInterrupt blocks until an interrupt signal is received and all shutdown steps have been executed.
func WaitForInterrupt() {
Default().WaitForInterrupt()
}
// 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 Trigger(ctx context.Context) {
Default().Trigger(ctx)
}
// 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 AddParallelSequence(handlers ...NamedHandler) {
Default().AddParallelSequence(handlers...)
}
// 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 AddSequence(handlers ...NamedHandler) {
Default().AddSequence(handlers...)
}
// 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 AddSteps(handlers ...NamedHandler) {
Default().AddSteps(handlers...)
}
// SetLogger sets the shutdown logger. If set to nil, no logs will be written.
func SetLogger(logger Logger) {
Default().SetLogger(logger)
}
// 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 SetCompletionFunc(f func()) {
Default().SetCompletionFunc(f)
}
// 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 SetTimeout(duration time.Duration) {
Default().SetTimeout(duration)
}