-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
96 lines (81 loc) · 1.69 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package watcher
import (
"context"
"log"
)
// Watch executes f every time n triggers
func Watch(ctx context.Context, n Notifier, f Executer) {
NewWatcher(n, f).Start(ctx)
}
type Executer = func(context.Context, interface{})
type Watcher struct {
n Notifier
f Executer
stop chan struct{}
logger Logger
}
func NewWatcher(n Notifier, f Executer, opts ...WatcherOption) *Watcher {
w := &Watcher{
n: n,
f: f,
stop: make(chan struct{}),
}
for _, opt := range opts {
opt(w)
}
if w.logger == nil {
w.logger = &stdLogger{}
}
return w
}
func (w *Watcher) Start(ctx context.Context) {
go func() {
for {
select {
case v, ok := <-w.n:
if !ok {
w.logger.Info("[watcher] notifier is closed")
return
}
w.Execute(ctx, v)
case <-ctx.Done():
w.logger.Info("[watcher] context is done, err=%v", ctx.Err())
return
case <-w.stop:
w.logger.Info("[watcher] stoped")
return
}
}
}()
}
func (w *Watcher) Stop() {
w.stop <- struct{}{}
}
func (w *Watcher) GetNotifier() Notifier {
return w.n
}
func (w *Watcher) Execute(ctx context.Context, v interface{}) {
defer func() {
if e := recover(); e != nil {
w.logger.Error("[watcher] PANIC occurred!!! msg=%v", e)
}
}()
w.f(ctx, v)
}
type WatcherOption func(*Watcher)
type Logger interface {
Info(format string, args ...interface{})
Error(format string, args ...interface{})
}
func WithLogger(logger Logger) WatcherOption {
return func(w *Watcher) {
w.logger = logger
}
}
type stdLogger struct{}
func (l *stdLogger) Info(format string, args ...interface{}) {
log.Printf(format, args...)
}
func (l *stdLogger) Error(format string, args ...interface{}) {
log.Fatalf(format, args...)
}