forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 4
/
watchdog.go
274 lines (226 loc) · 5.94 KB
/
watchdog.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package work
import (
"container/heap"
"log/slog"
"sync"
"sync/atomic"
"time"
)
// WatchdogFailCheckingTimeout a default checking timeout that marks task as failed.
const WatchdogFailCheckingTimeout = 60 * time.Second
const processedJobsBuffer = 256
// The WatchdogStat struct represents statistics for a periodic jobs, including the name, counter,
type WatchdogStat struct {
Name string
Processed int64
Skipped int64
}
// watchdog a struct that checks that periodic tasks are running.
// It is based on data about planned tasks and how they are actually processed.
type watchdog struct {
periodicJobs []*periodicJob
jobs map[string]*watchdogJob
processedJobs chan *Job
failCheckingTimeout time.Duration
stopChan chan struct{}
logger StructuredLogger
}
type watchdogOption func(w *watchdog)
func watchdogWithLogger(logger StructuredLogger) watchdogOption {
return func(w *watchdog) {
w.logger = logger
}
}
func watchdogWithFailCheckingTimeout(t time.Duration) watchdogOption {
return func(w *watchdog) {
w.failCheckingTimeout = t
}
}
func newWatchdog(opts ...watchdogOption) *watchdog {
w := &watchdog{
jobs: make(map[string]*watchdogJob),
processedJobs: make(chan *Job, processedJobsBuffer),
stopChan: make(chan struct{}),
}
for _, opt := range opts {
opt(w)
}
if w.logger == nil {
w.logger = noopLogger
}
if w.failCheckingTimeout == 0 {
w.failCheckingTimeout = WatchdogFailCheckingTimeout
}
return w
}
func (w *watchdog) addPeriodicJobs(jobs ...*periodicJob) {
w.periodicJobs = append(w.periodicJobs, jobs...)
for _, j := range w.periodicJobs {
w.jobs[j.jobName] = &watchdogJob{
checkTimes: newCheckTimesHeap(),
}
}
}
func (w *watchdog) start() {
const checkTimeout = time.Second
go func() {
timer := time.NewTicker(checkTimeout)
defer timer.Stop()
for {
select {
case t := <-timer.C:
w.planning(t)
w.checking(t)
case j := <-w.processedJobs:
w.processed(j)
case <-w.stopChan:
return
}
}
}()
}
func (w *watchdog) stop() {
w.stopChan <- struct{}{}
}
// planning method is responsible for planning the execution of periodic jobs.
// It iterates over the list of periodic jobs, calculates the next scheduled time for each job
// based on the current time `t`, and updates the check list for each job with the new scheduled time.
func (w *watchdog) planning(t time.Time) {
for _, j := range w.periodicJobs {
n := j.schedule.Next(t)
h := w.jobs[j.jobName].checkTimes
if h.Push(n) {
w.logger.Debug("Watchdog: planning job",
slog.String("job_name", j.jobName),
slog.Time("job_next_time", n),
slog.Int("jobs_total_planned", h.Len()),
)
}
}
}
// checking checks for skipped jobs based on the time `t`.
// It iterates over the scheduled times for each job and compares them with the
// current time plus the fail checking timeout. If a job's scheduled time has passed the fail checking
// timeout, it is considered as skipped, removed from the check list, and the `skip` method is called
// to increment the skipped count for that job.
func (w *watchdog) checking(t time.Time) {
for name, job := range w.jobs {
job.each(func(h *checkTimesHeap) bool {
n, _ := h.Peek()
if n.Add(w.failCheckingTimeout).Before(t) {
h.Pop()
job.skipped.Add(1)
w.logger.Warn("Watchdog: skipped job",
slog.String("job_name", name),
slog.Time("job_next_time", n),
slog.Int64("jobs_skipped", job.skipped.Load()),
)
return false
}
return true
})
}
}
// processed method is responsible for handling a processed job in the watchdog system.
// It iterates over the scheduled times for each job and check if job was successfully processed.
func (w *watchdog) processed(j *Job) {
job, ok := w.jobs[j.Name]
if !ok {
return
}
job.each(func(h *checkTimesHeap) bool {
n, _ := h.Peek()
if !n.After(time.Unix(j.EnqueuedAt, 0)) {
h.Pop()
job.processed.Add(1)
w.logger.Debug("Watchdog: successfully processed job",
slog.String("job_name", j.Name),
slog.Time("job_next_time", n),
slog.Int64("jobs_processed", job.processed.Load()),
)
return false
}
return true
})
}
func (w *watchdog) stats() []WatchdogStat {
res := make([]WatchdogStat, 0, len(w.jobs))
for k, v := range w.jobs {
res = append(res, WatchdogStat{
Name: k,
Processed: v.processed.Load(),
Skipped: v.skipped.Load(),
})
}
return res
}
type watchdogJob struct {
checkTimes *checkTimesHeap
processed atomic.Int64
skipped atomic.Int64
}
func (w *watchdogJob) each(cb func(h *checkTimesHeap) bool) {
h := w.checkTimes
for h.Len() > 0 {
if cb(h) {
return
}
}
}
type checkTimes []time.Time
func (h checkTimes) Len() int { return len(h) }
func (h checkTimes) Less(i, j int) bool { return h[i].Before(h[j]) }
func (h checkTimes) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *checkTimes) Push(x any) {
*h = append(*h, x.(time.Time))
}
func (h *checkTimes) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
type checkTimesHeap struct {
mu sync.RWMutex
uniqTimes map[time.Time]struct{}
checkTimes *checkTimes
}
func newCheckTimesHeap() *checkTimesHeap {
h := &checkTimes{}
heap.Init(h)
return &checkTimesHeap{
uniqTimes: make(map[time.Time]struct{}),
checkTimes: h,
}
}
func (h *checkTimesHeap) Len() int {
h.mu.RLock()
defer h.mu.RUnlock()
return h.checkTimes.Len()
}
func (h *checkTimesHeap) Peek() (time.Time, bool) {
h.mu.RLock()
defer h.mu.RUnlock()
if h.checkTimes.Len() > 0 {
return (*h.checkTimes)[0], true
}
return time.Time{}, false
}
func (h *checkTimesHeap) Push(t time.Time) bool {
h.mu.Lock()
defer h.mu.Unlock()
if _, exists := h.uniqTimes[t]; exists {
return false
}
h.uniqTimes[t] = struct{}{}
heap.Push(h.checkTimes, t)
return true
}
func (h *checkTimesHeap) Pop() time.Time {
h.mu.Lock()
defer h.mu.Unlock()
t := heap.Pop(h.checkTimes).(time.Time)
delete(h.uniqTimes, t)
return t
}