forked from taylorchu/work
-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.go
397 lines (346 loc) · 9.24 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package work
import (
"context"
"errors"
"fmt"
"runtime/debug"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
)
// DequeueFunc generates a job.
type DequeueFunc func(*DequeueOptions) (*Job, error)
// DequeueMiddleware modifies DequeueFunc behavior.
type DequeueMiddleware func(DequeueFunc) DequeueFunc
// EnqueueFunc takes in a job for processing.
type EnqueueFunc func(*Job, *EnqueueOptions) error
// EnqueueMiddleware modifies EnqueueFunc behavior.
type EnqueueMiddleware func(EnqueueFunc) EnqueueFunc
// HandleFunc runs a job.
type HandleFunc func(*Job, *DequeueOptions) error
// ContextHandleFunc runs a job.
type ContextHandleFunc func(context.Context, *Job, *DequeueOptions) error
// HandleMiddleware modifies HandleFunc hehavior.
type HandleMiddleware func(HandleFunc) HandleFunc
// BackoffFunc computes when to retry this job from now.
type BackoffFunc func(*Job, *DequeueOptions) time.Duration
type handler struct {
QueueID string
HandleFunc ContextHandleFunc
JobOptions JobOptions
}
// WorkerOptions is used to create a worker.
type WorkerOptions struct {
Namespace string
Queue Queue
ErrorFunc func(error)
}
// Worker runs jobs.
type Worker struct {
opt WorkerOptions
cancel func()
wg sync.WaitGroup
handlerMap map[string]handler
}
// NewWorker creates a new worker.
func NewWorker(opt *WorkerOptions) *Worker {
return &Worker{
opt: *opt,
handlerMap: make(map[string]handler),
}
}
// JobOptions specifies how a job is executed.
type JobOptions struct {
MaxExecutionTime time.Duration
IdleWait time.Duration
NumGoroutines int64
Backoff BackoffFunc
DequeueMiddleware []DequeueMiddleware
HandleMiddleware []HandleMiddleware
}
// options validation error
var (
ErrMaxExecutionTime = errors.New("work: max execution time should be > 0")
ErrNumGoroutines = errors.New("work: number of goroutines should be > 0")
ErrIdleWait = errors.New("work: idle wait should be > 0")
)
// Validate validates JobOptions.
func (opt *JobOptions) Validate() error {
if opt.MaxExecutionTime <= 0 {
return ErrMaxExecutionTime
}
if opt.IdleWait <= 0 {
return ErrIdleWait
}
if opt.NumGoroutines <= 0 {
return ErrNumGoroutines
}
return nil
}
// Register adds handler for a queue.
// queueID and namespace should be the same as the one used to enqueue.
func (w *Worker) Register(queueID string, h HandleFunc, opt *JobOptions) error {
return w.RegisterWithContext(queueID, func(ctx context.Context, job *Job, o *DequeueOptions) error {
return h(job, o)
}, opt)
}
// RegisterWithContext adds handler for a queue with context.Context.
// queueID and namespace should be the same as the one used to enqueue.
// The context is created with context.WithTimeout set from MaxExecutionTime.
func (w *Worker) RegisterWithContext(queueID string, h ContextHandleFunc, opt *JobOptions) error {
err := opt.Validate()
if err != nil {
return err
}
w.handlerMap[queueID] = handler{
QueueID: queueID,
HandleFunc: h,
JobOptions: *opt,
}
return nil
}
// OnceJobOptions specifies how a job is executed.
type OnceJobOptions struct {
MaxExecutionTime time.Duration
Backoff BackoffFunc
DequeueMiddleware []DequeueMiddleware
HandleMiddleware []HandleMiddleware
}
// Validate validates OnceJobOptions.
func (opt *OnceJobOptions) Validate() error {
if opt.MaxExecutionTime <= 0 {
return ErrMaxExecutionTime
}
return nil
}
// RunOnce simply runs one job from a queue.
// The context is created with context.WithTimeout set from MaxExecutionTime.
//
// This is used with kubernetes where a pod is created directly to run a job.
func (w *Worker) RunOnce(ctx context.Context, queueID string, h ContextHandleFunc, opt *OnceJobOptions) error {
err := opt.Validate()
if err != nil {
return err
}
queue := w.opt.Queue
ns := w.opt.Namespace
dequeue := queue.Dequeue
for _, mw := range opt.DequeueMiddleware {
dequeue = mw(dequeue)
}
handle := func(job *Job, o *DequeueOptions) error {
ctx, cancel := context.WithTimeout(ctx, opt.MaxExecutionTime)
defer cancel()
return h(ctx, job, o)
}
for _, mw := range opt.HandleMiddleware {
handle = mw(handle)
}
handle = catchPanic(handle)
b := opt.Backoff
if b == nil {
b = defaultBackoff()
}
handle = retry(queue, b)(handle)
dopt := &DequeueOptions{
Namespace: ns,
QueueID: queueID,
At: time.Now(),
InvisibleSec: int64(opt.MaxExecutionTime / time.Second),
}
job, err := dequeue(dopt)
if err != nil {
return err
}
err = handle(job, dopt)
if err != nil {
return err
}
err = queue.Ack(job, &AckOptions{
Namespace: dopt.Namespace,
QueueID: dopt.QueueID,
})
if err != nil {
return err
}
return nil
}
// Start starts the worker.
func (w *Worker) Start() {
ctx, cancel := context.WithCancel(context.Background())
w.cancel = cancel
for _, h := range w.handlerMap {
for i := int64(0); i < h.JobOptions.NumGoroutines; i++ {
w.wg.Add(1)
go func(h handler) {
defer w.wg.Done()
w.start(ctx, h)
}(h)
}
}
}
func (w *Worker) start(ctx context.Context, h handler) {
// print errors by default so that problems are noticeable.
errFunc := func(err error) { fmt.Println(err) }
if w.opt.ErrorFunc != nil {
errFunc = w.opt.ErrorFunc
}
var dequeueMiddleware []DequeueMiddleware
dequeueMiddleware = append(dequeueMiddleware, h.JobOptions.DequeueMiddleware...)
dequeueMiddleware = append(dequeueMiddleware, idleWait(ctx, h.JobOptions.IdleWait))
var handleMiddleware []HandleMiddleware
handleMiddleware = append(handleMiddleware, h.JobOptions.HandleMiddleware...)
handleMiddleware = append(handleMiddleware, catchPanic, wrapHandlerError)
opt := &OnceJobOptions{
MaxExecutionTime: h.JobOptions.MaxExecutionTime,
Backoff: h.JobOptions.Backoff,
DequeueMiddleware: dequeueMiddleware,
HandleMiddleware: handleMiddleware,
}
for {
select {
case <-ctx.Done():
return
default:
err := w.RunOnce(ctx, h.QueueID, h.HandleFunc, opt)
if err != nil {
var wrappedError *wrappedHandlerError
if errors.As(err, &wrappedError) {
} else if errors.Is(err, ErrEmptyQueue) {
} else {
errFunc(err)
}
}
}
}
}
// ExportMetrics dumps queue stats if the queue implements MetricsExporter.
func (w *Worker) ExportMetrics() (*Metrics, error) {
var queueMetrics []*QueueMetrics
for _, h := range w.handlerMap {
queue := w.opt.Queue
ns := w.opt.Namespace
exporter, ok := queue.(MetricsExporter)
if !ok {
continue
}
m, err := exporter.GetQueueMetrics(&QueueMetricsOptions{
Namespace: ns,
QueueID: h.QueueID,
At: time.Now(),
})
if err != nil {
return nil, err
}
queueMetrics = append(queueMetrics, m)
}
return &Metrics{
Queue: queueMetrics,
}, nil
}
// Stop stops the worker.
func (w *Worker) Stop() {
if w.cancel != nil {
w.cancel()
}
w.wg.Wait()
}
func idleWait(ctx context.Context, d time.Duration) DequeueMiddleware {
return func(f DequeueFunc) DequeueFunc {
return func(opt *DequeueOptions) (*Job, error) {
job, err := f(opt)
if err != nil {
if errors.Is(err, ErrEmptyQueue) {
select {
case <-time.After(d):
case <-ctx.Done():
}
}
return nil, err
}
return job, nil
}
}
}
type wrappedHandlerError struct {
Err error
}
func (e *wrappedHandlerError) Unwrap() error {
return e.Err
}
func (e *wrappedHandlerError) Error() string {
return e.Err.Error()
}
func wrapHandlerError(f HandleFunc) HandleFunc {
return func(job *Job, opt *DequeueOptions) error {
err := f(job, opt)
if err != nil {
return &wrappedHandlerError{Err: err}
}
return nil
}
}
func catchPanic(f HandleFunc) HandleFunc {
return func(job *Job, opt *DequeueOptions) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v\n\n%s", r, debug.Stack())
}
}()
return f(job, opt)
}
}
// retry error
var (
// ErrDoNotRetry is returned if the job should not be retried;
// this may be because the job is unrecoverable, or because
// the handler has already rescheduled it.
ErrDoNotRetry = errors.New("work: do not retry")
// ErrUnrecoverable is returned if the error is unrecoverable.
// The job will be discarded.
ErrUnrecoverable = fmt.Errorf("work: permanent error: %w", ErrDoNotRetry)
)
// https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md
func defaultBackoff() BackoffFunc {
return func(job *Job, opt *DequeueOptions) time.Duration {
b := backoff.NewExponentialBackOff()
b.InitialInterval = time.Second
b.RandomizationFactor = 0.2
b.Multiplier = 1.6
b.MaxInterval = time.Hour
b.MaxElapsedTime = 0
b.Reset()
var next time.Duration
for i := int64(0); i < job.Retries; i++ {
next = b.NextBackOff()
}
return next
}
}
func retry(queue Queue, backoff BackoffFunc) HandleMiddleware {
return func(f HandleFunc) HandleFunc {
return func(job *Job, opt *DequeueOptions) error {
err := f(job, opt)
if err != nil {
if errors.Is(err, ErrUnrecoverable) {
return nil // ack
}
if errors.Is(err, ErrDoNotRetry) {
// don't ack and don't reenqueue
return err
}
now := time.Now()
job.Retries++
job.LastError = err.Error()
job.UpdatedAt = now
job.EnqueuedAt = now.Add(backoff(job, opt))
queue.Enqueue(job, &EnqueueOptions{
Namespace: opt.Namespace,
QueueID: opt.QueueID,
})
return err
}
return nil
}
}
}