-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlifecycle.go
62 lines (55 loc) · 1.52 KB
/
lifecycle.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
package appx
import (
"context"
)
// Lifecycle allows application initializers to register callbacks that are
// executed on application start and stop.
//
// The concept of lifecycle is borrowed from https://github.com/uber-go/fx.
type Lifecycle interface {
Append(Hook)
}
// A Hook is a pair of start and stop callbacks, either of which can be nil.
// If a Hook's OnStart callback isn't executed (because a previous OnStart
// failure short-circuited application startup), its OnStop callback won't be
// executed.
type Hook struct {
OnStart func(ctx context.Context) error
OnStop func(ctx context.Context) error
}
type lifecycleImpl struct {
hooks []Hook
numStarted int
}
func (l *lifecycleImpl) Append(hook Hook) {
l.hooks = append(l.hooks, hook)
}
// Start runs all OnStart hooks, returning immediately if it encounters an
// error.
func (l *lifecycleImpl) Start(ctx context.Context) error {
for _, hook := range l.hooks {
if hook.OnStart != nil {
if err := hook.OnStart(ctx); err != nil {
return err
}
}
l.numStarted++
}
return nil
}
// Stop runs any OnStop hooks whose OnStart counterpart succeeded. OnStop
// hooks run in reverse order.
func (l *lifecycleImpl) Stop(ctx context.Context) (errs []error) {
// Run backward from last successful OnStart.
for ; l.numStarted > 0; l.numStarted-- {
hook := l.hooks[l.numStarted-1]
if hook.OnStop == nil {
continue
}
if err := hook.OnStop(ctx); err != nil {
// For best-effort cleanup, keep going after errors.
errs = append(errs, err)
}
}
return
}