-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.go
169 lines (133 loc) · 3 KB
/
worker.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package eventline
import (
"sync"
"time"
"github.com/exograd/eventline/pkg/utils"
"github.com/exograd/go-daemon/daemon"
"github.com/exograd/go-daemon/dlog"
)
type WorkerCfg struct {
Log *dlog.Logger `json:"-"`
Daemon *daemon.Daemon `json:"-"`
Behaviour WorkerBehaviour `json:"-"`
Disabled bool `json:"disabled"`
InitialDelay int `json:"initial_delay"` // millisecond
ErrorDelay int `json:"error_delay"` // millisecond
SleepDuration int `json:"sleep_duration"` // millisecond
NotificationChan chan<- interface{} `json:"-"`
StopChan <-chan struct{} `json:"-"`
Wg *sync.WaitGroup `json:"-"`
}
type WorkerBehaviour interface {
Init(*Worker)
Start() error
Stop()
ProcessJob() (bool, error)
}
type Worker struct {
Name string
Cfg WorkerCfg
Log *dlog.Logger
Daemon *daemon.Daemon
timer *time.Timer
wakeUpChan chan bool
notificationChan chan<- interface{}
initialDelay time.Duration
errorDelay time.Duration
sleepDuration time.Duration
stopChan <-chan struct{}
wg *sync.WaitGroup
}
func NewWorker(name string, cfg WorkerCfg) *Worker {
w := &Worker{
Name: name,
Cfg: cfg,
Log: cfg.Log,
Daemon: cfg.Daemon,
wakeUpChan: make(chan bool, 1),
notificationChan: cfg.NotificationChan,
stopChan: cfg.StopChan,
wg: cfg.Wg,
}
initDuration := func(ms, defaultMs int) time.Duration {
if ms == 0 {
ms = defaultMs
}
return time.Duration(ms) * time.Millisecond
}
w.initialDelay = initDuration(cfg.InitialDelay, 1000)
w.errorDelay = initDuration(cfg.ErrorDelay, 5000)
w.sleepDuration = initDuration(cfg.SleepDuration, 5000)
w.Cfg.Behaviour.Init(w)
return w
}
func (w *Worker) Start() error {
w.Log.Info("starting")
if err := w.Cfg.Behaviour.Start(); err != nil {
return err
}
w.timer = time.NewTimer(w.initialDelay)
w.wg.Add(1)
go w.main()
return nil
}
func (w *Worker) WakeUp() {
// We do not want to block when writing on the wake-up chan. This can
// happen if we are trying to wake up the worker while it is processing a
// job.
select {
case w.wakeUpChan <- true:
default:
}
}
func (w *Worker) main() {
defer func() {
close(w.wakeUpChan)
w.wg.Done()
}()
running := true
for running {
func() {
defer func() {
if value := recover(); value != nil {
msg, trace := utils.RecoverValueData(value)
w.Log.Error("panic: %s\n%s", msg, trace)
time.Sleep(w.sleepDuration)
}
}()
select {
case <-w.stopChan:
running = false
return
case wakeUp := <-w.wakeUpChan:
if wakeUp {
w.processJobs()
}
case <-w.timer.C:
w.processJobs()
}
}()
}
}
func (w *Worker) processJobs() {
for !w.Stopping() {
processed, err := w.Cfg.Behaviour.ProcessJob()
if err != nil {
w.Log.Error("%v", err)
w.timer.Reset(w.errorDelay)
return
}
if !processed {
w.timer.Reset(w.sleepDuration)
return
}
}
}
func (w *Worker) Stopping() bool {
select {
case <-w.stopChan:
return true
default:
return false
}
}