-
Notifications
You must be signed in to change notification settings - Fork 27
/
task.go
418 lines (338 loc) · 9.01 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package bokchoy
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/thoas/bokchoy/logging"
)
const (
// Task statuses
taskStatusWaiting int = iota
taskStatusProcessing
taskStatusSucceeded
taskStatusFailed
taskStatusCanceled
)
// Task is the model stored in a Queue.
type Task struct {
ID string
Name string
PublishedAt time.Time
StartedAt time.Time
ProcessedAt time.Time
Status int
OldStatus int
MaxRetries int
Payload interface{}
Result interface{}
Error interface{}
ExecTime float64
TTL time.Duration
Timeout time.Duration
ETA time.Time
RetryIntervals []time.Duration
}
// NewTask initializes a new Task.
func NewTask(name string, payload interface{}, options ...Option) *Task {
opts := newOptions()
for i := range options {
options[i](opts)
}
t := &Task{
ID: ID(),
Name: name,
Payload: payload,
Status: taskStatusWaiting,
PublishedAt: time.Now().UTC(),
}
t.MaxRetries = opts.MaxRetries
t.TTL = opts.TTL
return t
}
// TaskFromPayload returns a Task instance from raw data.
func TaskFromPayload(data map[string]interface{}, serializer Serializer) (*Task, error) {
var ok bool
var err error
t := &Task{}
t.ID, err = mapString(data, "id", false)
if err != nil {
return nil, err
}
t.Name, err = mapString(data, "name", false)
if err != nil {
return nil, err
}
t.Status, err = mapInt(data, "status", false)
if err != nil {
return nil, err
}
t.OldStatus = t.Status
t.PublishedAt, err = mapTime(data, "published_at", false)
if err != nil {
return nil, err
}
t.ProcessedAt, err = mapTime(data, "processed_at", true)
if err != nil {
return nil, err
}
t.StartedAt, err = mapTime(data, "started_at", true)
if err != nil {
return nil, err
}
t.ETA, err = mapTime(data, "eta", true)
if err != nil {
return nil, err
}
t.Timeout, err = mapDuration(data, "timeout", true)
if err != nil {
return nil, err
}
payload, err := mapString(data, "payload", false)
if err != nil {
return nil, err
}
t.MaxRetries, err = mapInt(data, "max_retries", false)
if err != nil {
return nil, err
}
t.TTL, err = mapDuration(data, "ttl", false)
if err != nil {
return nil, err
}
t.ExecTime, err = mapFloat(data, "exec_time", true)
if err != nil {
return nil, err
}
rawRetryIntervals, err := mapString(data, "retry_intervals", true)
if err != nil {
return nil, err
}
if rawRetryIntervals != "" {
strRetryIntervals := strings.Split(rawRetryIntervals, ",")
t.RetryIntervals = make([]time.Duration, len(strRetryIntervals))
for i := range strRetryIntervals {
value, err := strconv.ParseInt(strRetryIntervals[i], 10, 64)
if err != nil {
return nil, errors.Wrapf(ErrAttributeError, "cannot parse %s retry interval to integer", strRetryIntervals[i])
}
t.RetryIntervals[i] = time.Duration(value) * time.Second
}
}
err = serializer.Loads([]byte(payload), &t.Payload)
if err != nil {
return nil, errors.Wrapf(ErrAttributeError, "cannot unserialize `payload`")
}
rawError, ok := data["error"].(string)
if ok {
err = serializer.Loads([]byte(rawError), &t.Error)
if err != nil {
return nil, errors.Wrapf(ErrAttributeError, "cannot unserialize `error`")
}
}
rawResult, ok := data["result"].(string)
if ok {
err = serializer.Loads([]byte(rawResult), &t.Result)
if err != nil {
return nil, errors.Wrapf(ErrAttributeError, "cannot unserialize `result`")
}
}
return t, nil
}
// ETADisplay returns the string representation of the ETA.
func (t Task) ETADisplay() string {
if t.ETA.IsZero() {
return "0s"
}
return t.ETA.Sub(time.Now().UTC()).String()
}
// RetryETA returns the next ETA.
func (t Task) RetryETA() time.Time {
if t.MaxRetries >= len(t.RetryIntervals) {
return t.ETA
}
if len(t.RetryIntervals) > 0 {
intervals := reverseDurations(t.RetryIntervals)
if len(intervals) > t.MaxRetries {
return time.Now().UTC().Add(intervals[t.MaxRetries])
}
return time.Now().UTC().Add(intervals[0])
}
return time.Time{}
}
// MarshalLogObject returns the log representation for the task.
func (t Task) MarshalLogObject(enc logging.ObjectEncoder) error {
enc.AddString("id", t.ID)
enc.AddString("name", t.Name)
enc.AddString("status", t.StatusDisplay())
enc.AddString("payload", fmt.Sprintf("%v", t.Payload))
enc.AddInt("max_retries", t.MaxRetries)
enc.AddDuration("ttl", t.TTL)
enc.AddDuration("timeout", t.Timeout)
enc.AddTime("published_at", t.PublishedAt)
if !t.StartedAt.IsZero() {
enc.AddTime("started_at", t.StartedAt)
}
if !t.ProcessedAt.IsZero() {
enc.AddTime("processed_at", t.ProcessedAt)
enc.AddDuration("duration", t.ProcessedAt.Sub(t.StartedAt))
}
if !t.ETA.IsZero() {
enc.AddTime("eta", t.ETA)
}
if t.ExecTime != 0 {
enc.AddFloat64("exec_time", t.ExecTime)
}
if len(t.RetryIntervals) > 0 {
enc.AddString("retry_intervals", t.RetryIntervalsDisplay())
}
return nil
}
// RetryIntervalsDisplay returns the string representation of the retry intervals.
func (t Task) RetryIntervalsDisplay() string {
intervals := make([]string, len(t.RetryIntervals))
for i := range t.RetryIntervals {
intervals[i] = t.RetryIntervals[i].String()
}
return strings.Join(intervals, ", ")
}
// String returns the string representation of Task.
func (t Task) String() string {
return fmt.Sprintf(
"<Task name=%s id=%s, status=%s, published_at=%s>",
t.Name, t.ID, t.StatusDisplay(), t.PublishedAt.String(),
)
}
// StatusDisplay returns the status in human representation.
func (t Task) StatusDisplay() string {
switch t.Status {
case taskStatusSucceeded:
return "succeeded"
case taskStatusProcessing:
return "processing"
case taskStatusFailed:
return "failed"
case taskStatusCanceled:
return "canceled"
}
return "waiting"
}
// Serialize serializes a Task to raw data.
func (t Task) Serialize(serializer Serializer) (map[string]interface{}, error) {
var err error
data := map[string]interface{}{
"id": t.ID,
"name": t.Name,
"status": t.Status,
"published_at": t.PublishedAt.Unix(),
"max_retries": t.MaxRetries,
"ttl": int(t.TTL.Seconds()),
"timeout": int(t.Timeout.Seconds()),
}
if !t.ETA.IsZero() {
data["eta"] = t.ETA.Unix()
}
if !t.ProcessedAt.IsZero() {
data["processed_at"] = t.ProcessedAt.Unix()
}
if !t.StartedAt.IsZero() {
data["started_at"] = t.StartedAt.Unix()
}
if t.Payload != nil {
payload, err := serializer.Dumps(t.Payload)
if err != nil {
return nil, err
}
data["payload"] = string(payload)
}
if t.Result != nil {
result, err := serializer.Dumps(t.Result)
if err != nil {
return nil, err
}
data["result"] = string(result)
}
if t.Error != nil {
rawErr, ok := t.Error.(error)
if ok {
data["error"], err = serializer.Dumps(rawErr.Error())
if err != nil {
return nil, err
}
}
}
if t.ExecTime != 0 {
data["exec_time"] = t.ExecTime
}
if len(t.RetryIntervals) > 0 {
intervals := make([]string, len(t.RetryIntervals))
for i := range t.RetryIntervals {
intervals[i] = fmt.Sprintf("%d", int(t.RetryIntervals[i].Seconds()))
}
data["retry_intervals"] = strings.Join(intervals, ",")
}
return data, err
}
// Key returns the task key.
func (t Task) Key() string {
return fmt.Sprintf("%s:%s", t.Name, t.ID)
}
// MarkAsProcessing marks a task as processing.
func (t *Task) MarkAsProcessing() {
t.StartedAt = time.Now().UTC()
t.Status = taskStatusProcessing
}
// Finished returns if a task is finished or not.
func (t *Task) Finished() bool {
if t.OldStatus == taskStatusSucceeded {
return true
}
if (t.OldStatus == taskStatusFailed || t.Status == taskStatusFailed) && t.MaxRetries == 0 {
return true
}
if t.Status == taskStatusSucceeded {
return true
}
return false
}
// IsStatusWaiting returns if the task status is waiting.
func (t *Task) IsStatusWaiting() bool {
return t.Status == taskStatusWaiting
}
// IsStatusSucceeded returns if the task status is succeeded.
func (t *Task) IsStatusSucceeded() bool {
return t.Status == taskStatusSucceeded
}
// IsStatusProcessing returns if the task status is processing.
func (t *Task) IsStatusProcessing() bool {
return t.Status == taskStatusProcessing
}
// IsStatusFailed returns if the task status is failed.
func (t *Task) IsStatusFailed() bool {
return t.Status == taskStatusFailed
}
// IsStatusCanceled returns if the task status is canceled.
func (t *Task) IsStatusCanceled() bool {
return t.Status == taskStatusCanceled
}
// MarkAsSucceeded marks a task as succeeded.
func (t *Task) MarkAsSucceeded() {
t.ProcessedAt = time.Now().UTC()
t.Status = taskStatusSucceeded
t.ExecTime = t.ProcessedAt.Sub(t.StartedAt).Seconds()
}
// MarkAsFailed marks a task as failed.
func (t *Task) MarkAsFailed(err error) {
t.ProcessedAt = time.Now().UTC()
t.Status = taskStatusFailed
if err != nil {
t.Error = err
}
t.ExecTime = t.ProcessedAt.Sub(t.StartedAt).Seconds()
}
// MarkAsCanceled marks a task as canceled.
func (t *Task) MarkAsCanceled() {
t.ProcessedAt = time.Now().UTC()
t.Status = taskStatusCanceled
}