-
Notifications
You must be signed in to change notification settings - Fork 3
/
task.go
370 lines (301 loc) · 7.22 KB
/
task.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
package axe
import (
"context"
"fmt"
"reflect"
"time"
"gopkg.in/tomb.v2"
"github.com/256dpi/fire"
"github.com/256dpi/fire/coal"
)
// Error is used to signal failed job executions.
type Error struct {
Reason string
Retry bool
}
// Error implements the error interface.
func (c *Error) Error() string {
return c.Reason
}
// E is a short-hand to construct an error.
func E(reason string, retry bool) *Error {
return &Error{
Reason: reason,
Retry: retry,
}
}
// Model can be any BSON serializable type.
type Model interface{}
// Context holds and stores contextual data.
type Context struct {
// Context is the standard context that is cancelled when the timeout has
// been exceeded.
context.Context
// Model is the model carried by the job.
Model Model
// Result can be set with a custom result.
Result coal.Map
// Task is the task that processes this job.
//
// Usage: Read Only
Task *Task
// Queue is the queue this job was dequeued from.
//
// Usage: Read Only
Queue *Queue
// Store is the store used by the queue.
//
// Usage: Read Only
Store *coal.Store
// The tracer used to trace code execution.
//
// Usage: Read Only
Tracer *fire.Tracer
}
// TC is a shorthand to get a traced collection for the specified model.
func (c *Context) TC(model coal.Model) *coal.Collection {
return c.Store.TC(c.Tracer, model)
}
// Task describes work that is managed using a job queue.
type Task struct {
// Name is the unique name of the task.
Name string
// Model is the model that holds task related data.
Model Model
// Handler is the callback called with jobs for processing. The handler
// should return errors formatted with E to properly indicate the status of
// the job. If a task execution is successful the handler may return some
// data that is attached to the job.
Handler func(*Context) error
// Workers defines the number for spawned workers that dequeue and execute
// jobs in parallel.
//
// Default: 2.
Workers int
// MaxAttempts defines the maximum attempts to complete a task. Zero means
// that the jobs is retried forever. The error retry field will take
// precedence to this setting and allow retry beyond the configured maximum.
//
// Default: 0
MaxAttempts int
// Interval defines the rate at which the worker will request a job from the
// queue.
//
// Default: 100ms.
Interval time.Duration
// MinDelay is the minimal time after a failed task is retried.
//
// Default: 1s.
MinDelay time.Duration
// MaxDelay is the maximal time after a failed task is retried.
//
// Default: 10m.
MaxDelay time.Duration
// DelayFactor defines the exponential increase of the delay after individual
// attempts.
//
// Default: 2.
DelayFactor float64
// Timeout is the time after which a task can be dequeued again in case the
// worker was not able to set its status.
//
// Default: 10m.
Timeout time.Duration
// Lifetime is the time after which the context of a job is cancelled and
// the execution should be stopped. Should be several minutes less than
// timeout to prevent race conditions.
//
// Default: 5m.
Lifetime time.Duration
// Periodically may be set to let the system enqueue a job automatically
// every given interval.
//
// Default: 0.
Periodically time.Duration
// PeriodicJob is the blueprint of the job that is periodically enqueued.
//
// Default: Blueprint{Name: Task.Name}.
PeriodicJob Blueprint
}
func (t *Task) start(q *Queue) {
// set default workers
if t.Workers == 0 {
t.Workers = 2
}
// set default interval
if t.Interval == 0 {
t.Interval = 100 * time.Millisecond
}
// set default minimal delay
if t.MinDelay == 0 {
t.MinDelay = time.Second
}
// set default maximal delay
if t.MaxDelay == 0 {
t.MaxDelay = 10 * time.Minute
}
// set default delay factor
if t.DelayFactor < 1 {
t.DelayFactor = 2
}
// set default timeout
if t.Timeout == 0 {
t.Timeout = 10 * time.Minute
}
// set default lifetime
if t.Lifetime == 0 {
t.Lifetime = 5 * time.Minute
}
// check timeout
if t.Lifetime > t.Timeout {
panic("axe: lifetime must be less than timeout")
}
// start workers for queue
for i := 0; i < t.Workers; i++ {
q.tomb.Go(func() error {
return t.worker(q)
})
}
// run periodic enqueuer if interval is given
if t.Periodically > 0 {
q.tomb.Go(func() error {
return t.enqueuer(q)
})
}
}
func (t *Task) worker(q *Queue) error {
// run forever
for {
// return if queue is closed
if !q.tomb.Alive() {
return tomb.ErrDying
}
// attempt to get job from queue
job := q.get(t.Name)
if job == nil {
// wait some time before trying again
select {
case <-time.After(t.Interval):
case <-q.tomb.Dying():
return tomb.ErrDying
}
continue
}
// execute job and report errors
err := t.execute(q, job)
if err != nil {
if q.reporter != nil {
q.reporter(err)
}
}
}
}
func (t *Task) enqueuer(q *Queue) error {
// prepare blueprint
blueprint := t.PeriodicJob
// override task name
blueprint.Name = t.Name
for {
// enqueue task
_, err := q.Enqueue(blueprint)
if err != nil && q.reporter != nil {
// report error
q.reporter(err)
// wait some time
select {
case <-time.After(time.Second):
case <-q.tomb.Dying():
return tomb.ErrDying
}
continue
}
// wait for next interval
select {
case <-time.After(t.Periodically):
case <-q.tomb.Dying():
return tomb.ErrDying
}
}
}
func (t *Task) execute(q *Queue, job *Job) error {
// dequeue job
job, err := Dequeue(q.store, job.ID(), t.Timeout)
if err != nil {
return err
}
// return if missing (might be dequeued already by another process)
if job == nil {
return nil
}
// get time
start := time.Now()
// prepare model
var model Model
// check model
if t.Model != nil {
// instantiate model
model = reflect.New(reflect.TypeOf(t.Model).Elem()).Interface()
// unmarshal model
err = job.Data.Unmarshal(model)
if err != nil {
return err
}
}
// create tracer
tracer := fire.NewTracerWithRoot(t.Name)
defer tracer.Finish(true)
// create context
c, cancel := context.WithTimeout(context.Background(), t.Lifetime)
defer cancel()
// prepare context
ctx := &Context{
Model: model,
Context: c,
Task: t,
Queue: q,
Store: q.store,
Tracer: tracer,
}
// run handler
err = t.Handler(ctx)
// return immediately if lifetime has been reached
if time.Since(start) > t.Lifetime {
return fmt.Errorf(`task "%s" ran longer than the specified lifetime`, t.Name)
}
// check error
if e, ok := err.(*Error); ok {
// check retry
if e.Retry {
// fail job
err = Fail(q.store, job.ID(), e.Reason, Backoff(t.MinDelay, t.MaxDelay, t.DelayFactor, job.Attempts))
if err != nil {
return err
}
return nil
}
// cancel job
err = Cancel(q.store, job.ID(), e.Reason)
if err != nil {
return err
}
return nil
}
// handle other errors
if err != nil {
// check attempts
if t.MaxAttempts == 0 || job.Attempts < t.MaxAttempts {
// fail job
_ = Fail(q.store, job.ID(), err.Error(), Backoff(t.MinDelay, t.MaxDelay, t.DelayFactor, job.Attempts))
return err
}
// cancel job
_ = Cancel(q.store, job.ID(), err.Error())
return err
}
// complete job
err = Complete(q.store, job.ID(), ctx.Result)
if err != nil {
return err
}
return nil
}