-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlifecycle.go
More file actions
140 lines (112 loc) · 3.42 KB
/
Copy pathlifecycle.go
File metadata and controls
140 lines (112 loc) · 3.42 KB
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
package vabastegi
import (
"context"
"os/signal"
"reflect"
"runtime"
"strings"
"syscall"
"time"
"github.com/mrsoftware/errors"
)
// ShutdownListener is what you need to pass to OnShutdown method.
type ShutdownListener = func(ctx context.Context) error
// lifecycle manage application lifecycle like running task or shutdown.
type lifecycle struct {
listeners []ShutdownListener
publisher *eventManager
ctx context.Context
cancel context.CancelCauseFunc
waitGroup *errors.WaitGroup
}
// newLifecycle create a new instance of lifecycle.
func newLifecycle(ctx context.Context, publisher *eventManager) *lifecycle {
waitGroup := errors.NewWaitGroup(errors.WaitGroupWithContext(ctx), errors.WaitGroupWithStopOnError())
return &lifecycle{
listeners: make([]ShutdownListener, 0),
publisher: publisher,
ctx: waitGroup.Context(),
cancel: waitGroup.Stop,
waitGroup: waitGroup,
}
}
// Wait on shutdown or application finish.
func (l *lifecycle) Wait() error {
var err error
select {
case <-l.ctx.Done():
err = l.ctx.Err()
case err = <-errors.WaitChanel(l.waitGroup):
l.Stop(err)
}
return l.callShutdownListeners(l.ctx, err)
}
// RegisterGracefulShutdown start listing on os signal and cancel the parent context on getting one.
func (l *lifecycle) RegisterGracefulShutdown() {
ctx, cancel := signal.NotifyContext(l.ctx, syscall.SIGINT, syscall.SIGTERM)
l.ctx, l.cancel = ctx, cancelToCancelCause(cancel)
}
// Stop the application.
func (l *lifecycle) Stop(cause error) {
l.cancel(cause)
}
// GetContext of lifecycle.
func (l *lifecycle) GetContext() context.Context {
return l.ctx
}
// do Shut down the application.
func (l *lifecycle) callShutdownListeners(ctx context.Context, cause error) error {
errList := errors.NewMultiError()
startAt := time.Now()
l.publisher.Publish(&OnApplicationShutdownExecuting{
Cause: cause,
ShutdownAt: startAt,
})
defer func() {
l.publisher.Publish(&OnApplicationShutdownExecuted{
Runtime: time.Now().Sub(startAt),
Err: errList.Err(),
})
}()
for _, fn := range l.listeners {
errList.Add(l.shutdown(ctx, fn))
}
return errList.Err()
}
func (l *lifecycle) shutdown(ctx context.Context, callback ShutdownListener) (err error) {
startAt := time.Now()
l.publisher.Publish(&OnShutdownExecuting{
ProviderName: getProviderName(callback, 1),
CallerPath: getProviderName(callback, -1),
ShutdownAt: startAt,
})
defer func() {
l.publisher.Publish(&OnShutdownExecuted{
ProviderName: getProviderName(callback, 1),
CallerPath: getProviderName(callback, -1),
Runtime: time.Now().Sub(startAt),
Err: err,
})
}()
return callback(ctx)
}
// OnShutdown add callback to a list of listeners.
func (l *lifecycle) OnShutdown(callback ShutdownListener) {
l.listeners = append(l.listeners, callback)
}
// RunTask in the background.
func (l *lifecycle) RunTask(ctx context.Context, fn func(ctx context.Context) error) {
l.waitGroup.DoWithContext(ctx, fn)
}
func getProviderName(creator interface{}, index int) string {
reference := runtime.FuncForPC(reflect.ValueOf(creator).Pointer()).Name()
if index == -1 {
return reference
}
parts := strings.Split(reference, ".")
return parts[len(parts)-(1+index)]
}
// cancelToCancelCause is just a wrapper to turn context.CancelFunc into context.CancelCauseFunc.
func cancelToCancelCause(cancelFunc context.CancelFunc) context.CancelCauseFunc {
return func(cause error) { cancelFunc() }
}