-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic.go
404 lines (354 loc) · 8.33 KB
/
basic.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
398
399
400
401
402
403
404
package axe
import (
"context"
"time"
"github.com/256dpi/xo"
"go.mongodb.org/mongo-driver/bson"
"github.com/256dpi/fire/coal"
"github.com/256dpi/fire/stick"
)
// Enqueue will enqueue the specified job with the provided delay and isolation.
// It will return whether a job has been enqueued. If the context carries a
// transaction it must be associated with the specified store.
//
// The job is labeled, no job is queued if there is already a job with the same
// label in the enqueued, dequeued or failed state. If isolation is non-zero
// the same rules applies also to unlabeled jobs in addition to that finished
// jobs must be older than the specified duration.
func Enqueue(ctx context.Context, store *coal.Store, job Job, delay, isolation time.Duration) (bool, error) {
// get meta and base
meta := GetMeta(job)
base := job.GetBase()
// ensure id
if base.DocID.IsZero() {
base.DocID = coal.New()
}
// trace
ctx, span := xo.Trace(ctx, "axe/Enqueue")
span.Tag("name", meta.Name)
span.Tag("label", base.Label)
span.Tag("id", job.ID().Hex())
span.Tag("delay", delay.String())
span.Tag("isolation", isolation.String())
defer span.End()
// check transaction
ok, ts := coal.GetTransaction(ctx)
if ok && ts != store {
return false, xo.F("transaction store does not match supplied store")
}
// validate job
err := job.Validate()
if err != nil {
return false, err
}
// encode job
var data stick.Map
err = data.Marshal(job, meta.Coding)
if err != nil {
return false, err
}
// get time
now := time.Now()
// prepare job
model := &Model{
Base: coal.B(base.DocID),
Name: meta.Name,
Label: base.Label,
Data: data,
State: Enqueued,
Created: now,
Available: now.Add(delay),
Events: []Event{
{
Timestamp: now,
State: Enqueued,
},
},
}
// insert unlabeled non-isolated jobs immediately
if base.Label == "" && isolation == 0 {
err := store.M(&Model{}).Insert(ctx, model)
if err != nil {
return false, err
}
return true, nil
}
// prepare filter
filter := bson.M{
"Name": meta.Name,
"Label": base.Label,
"State": bson.M{
"$in": bson.A{Enqueued, Dequeued, Failed},
},
}
// ensure isolation
if isolation > 0 {
// remove state
delete(filter, "State")
// consider state and finished time
filter["$or"] = bson.A{
// not finished
bson.M{
"State": bson.M{
"$in": bson.A{Enqueued, Dequeued, Failed},
},
"Finished": nil,
},
// finished recently
bson.M{
"State": bson.M{
"$in": bson.A{Completed, Cancelled},
},
"Finished": bson.M{
"$gt": now.Add(-isolation),
},
},
}
}
// insert job if missing
inserted, err := store.M(&Model{}).InsertIfMissing(ctx, filter, model, false)
if err != nil {
return false, err
}
return inserted, nil
}
// Dequeue will dequeue the specified job. The provided timeout will be set to
// allow the job to be dequeued if the worker failed to set its state. Only
// jobs in the "enqueued", "dequeued" (passed timeout) or "failed" state are
// dequeued. It will return whether a job has been dequeued.
func Dequeue(ctx context.Context, store *coal.Store, job Job, timeout time.Duration) (bool, int, error) {
// get meta
meta := GetMeta(job)
// trace
ctx, span := xo.Trace(ctx, "axe/Dequeue")
span.Tag("name", meta.Name)
span.Tag("id", job.ID().Hex())
span.Tag("timeout", timeout.String())
defer span.End()
// check timeout
if timeout == 0 {
return false, 0, xo.F("missing timeout")
}
// get time
now := time.Now()
// dequeue job
var model Model
found, err := store.M(&Model{}).UpdateFirst(ctx, &model, bson.M{
"_id": job.ID(),
"State": bson.M{
"$in": bson.A{Enqueued, Dequeued, Failed},
},
"Available": bson.M{
"$lte": now,
},
}, bson.M{
"$set": bson.M{
"State": Dequeued,
"Available": now.Add(timeout),
"Started": now,
"Ended": nil,
},
"$inc": bson.M{
"Attempts": 1,
},
"$push": bson.M{
"Events": Event{
Timestamp: now,
State: Dequeued,
},
},
}, nil, false)
if err != nil {
return false, 0, err
} else if !found {
return false, 0, nil
}
// decode job
err = model.Data.Unmarshal(job, meta.Coding)
if err != nil {
return false, 0, err
}
// set and log label
job.GetBase().Label = model.Label
span.Tag("label", model.Label)
// validate job
err = job.Validate()
if err != nil {
return false, 0, err
}
return true, model.Attempts, nil
}
// Update will update the specified job and set the provided execution status
// and progress.
func Update(ctx context.Context, store *coal.Store, job Job, status string, progress float64) error {
// get meta and base
meta := GetMeta(job)
base := job.GetBase()
// trace
ctx, span := xo.Trace(ctx, "axe/Update")
span.Tag("name", meta.Name)
span.Tag("label", base.Label)
span.Tag("id", job.ID().Hex())
defer span.End()
// validate job
err := job.Validate()
if err != nil {
return err
}
// encode job
var data stick.Map
err = data.Marshal(job, meta.Coding)
if err != nil {
return err
}
// update job
found, err := store.M(&Model{}).UpdateFirst(ctx, nil, bson.M{
"_id": job.ID(),
"State": Dequeued,
}, bson.M{
"$set": bson.M{
"Data": data,
"Status": status,
"Progress": progress,
},
}, nil, false)
if err != nil {
return err
} else if !found {
return xo.F("missing job")
}
return nil
}
// Complete will complete the specified job. Only jobs in the "dequeued" state
// can be completed.
func Complete(ctx context.Context, store *coal.Store, job Job) error {
// get meta and base
meta := GetMeta(job)
base := job.GetBase()
// trace
ctx, span := xo.Trace(ctx, "axe/Complete")
span.Tag("name", meta.Name)
span.Tag("label", base.Label)
span.Tag("id", job.ID().Hex())
defer span.End()
// validate job
err := job.Validate()
if err != nil {
return err
}
// encode job
var data stick.Map
err = data.Marshal(job, meta.Coding)
if err != nil {
return err
}
// get time
now := time.Now()
// update job
found, err := store.M(&Model{}).UpdateFirst(ctx, nil, bson.M{
"_id": job.ID(),
"State": Dequeued,
}, bson.M{
"$set": bson.M{
"State": Completed,
"Data": data,
"Ended": now,
"Finished": now,
"Status": "",
"Progress": 1,
},
"$push": bson.M{
"Events": Event{
Timestamp: now,
State: Completed,
},
},
}, nil, false)
if err != nil {
return err
} else if !found {
return xo.F("missing job")
}
return nil
}
// Fail will fail the specified job with the provided reason. It may delay the
// job if requested. Only jobs in the "dequeued" state can be failed.
func Fail(ctx context.Context, store *coal.Store, job Job, reason string, delay time.Duration) error {
// get meta and base
meta := GetMeta(job)
base := job.GetBase()
// trace
ctx, span := xo.Trace(ctx, "axe/Fail")
span.Tag("name", meta.Name)
span.Tag("label", base.Label)
span.Tag("id", job.ID().Hex())
span.Tag("reason", reason)
span.Tag("delay", delay.String())
defer span.End()
// get time
now := time.Now()
// update job
found, err := store.M(&Model{}).UpdateFirst(ctx, nil, bson.M{
"_id": job.ID(),
"State": Dequeued,
}, bson.M{
"$set": bson.M{
"State": Failed,
"Available": now.Add(delay),
"Ended": now,
},
"$push": bson.M{
"Events": Event{
Timestamp: now,
State: Failed,
Reason: reason,
},
},
}, nil, false)
if err != nil {
return err
} else if !found {
return xo.F("missing job")
}
return nil
}
// Cancel will cancel the specified job with the provided reason. Only jobs in
// the "dequeued" state can be cancelled.
func Cancel(ctx context.Context, store *coal.Store, job Job, reason string) error {
// get meta and base
meta := GetMeta(job)
base := job.GetBase()
// trace
ctx, span := xo.Trace(ctx, "axe/Cancel")
span.Tag("name", meta.Name)
span.Tag("label", base.Label)
span.Tag("id", job.ID().Hex())
span.Tag("reason", reason)
defer span.End()
// get time
now := time.Now()
// update job
found, err := store.M(&Model{}).UpdateFirst(ctx, nil, bson.M{
"_id": job.ID(),
"State": Dequeued,
}, bson.M{
"$set": bson.M{
"State": Cancelled,
"Ended": now,
"Finished": now,
},
"$push": bson.M{
"Events": Event{
Timestamp: now,
State: Cancelled,
Reason: reason,
},
},
}, nil, false)
if err != nil {
return err
} else if !found {
return xo.F("missing job")
}
return nil
}