-
Notifications
You must be signed in to change notification settings - Fork 24
/
watcher.go
79 lines (71 loc) · 1.9 KB
/
watcher.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
package notifiers
import (
"context"
"github.com/hashicorp/go-multierror"
"go.uber.org/fx"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// Watcher is a generic interface for watchers/trackers.
type Watcher interface {
AddPrefixNotifier(PrefixNotifier) error
RemovePrefixNotifier(PrefixNotifier) error
AddKeyNotifier(KeyNotifier) error
RemoveKeyNotifier(KeyNotifier) error
Start() error
Stop() error
}
// WatcherLifecycle starts/stops watcher and adds/removes prefix notifier(s) to etcd watcher.
func WatcherLifecycle(lc fx.Lifecycle, watcher Watcher, notifiers []PrefixNotifier) {
lc.Append(fx.Hook{
OnStart: func(_ context.Context) error {
err := watcher.Start()
if err != nil {
return err
}
for _, notifier := range notifiers {
err := watcher.AddPrefixNotifier(notifier)
if err != nil {
log.Error().Err(err).Msg("Failed to add notifier")
return err
}
}
return nil
},
OnStop: func(_ context.Context) error {
var errMulti error
for _, notifier := range notifiers {
err := watcher.RemovePrefixNotifier(notifier)
if err != nil {
errMulti = multierror.Append(errMulti, err)
log.Error().Err(err).Msg("Failed to remove notifier")
}
}
err := watcher.Stop()
if err != nil {
errMulti = multierror.Append(errMulti, err)
}
return errMulti
},
})
}
// NotifierLifecycle adds/removed prefix notifier to etcd watcher.
func NotifierLifecycle(lc fx.Lifecycle, watcher Watcher, notifier PrefixNotifier) {
lc.Append(fx.Hook{
OnStart: func(_ context.Context) error {
err := watcher.AddPrefixNotifier(notifier)
if err != nil {
log.Error().Err(err).Msg("Failed to add notifier")
return err
}
return nil
},
OnStop: func(_ context.Context) error {
err := watcher.RemovePrefixNotifier(notifier)
if err != nil {
log.Error().Err(err).Msg("Failed to remove notifier")
return err
}
return nil
},
})
}