forked from fieldryand/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
72 lines (57 loc) · 1.52 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
package goflow
import (
"math"
"time"
)
// A Task is the unit of work that makes up a job. Whenever a task is executed, it
// calls its associated operator.
type Task struct {
Name string
Operator Operator
TriggerRule triggerRule
Retries int
RetryDelay RetryDelay
remaining int
state state
}
type triggerRule string
const (
allDone triggerRule = "allDone"
allSuccessful triggerRule = "allSuccessful"
)
func (t *Task) run(writes chan writeOp) error {
_, err := t.Operator.Run()
// retry
if err != nil && t.remaining > 0 {
writes <- writeOp{t.Name, upForRetry}
return nil
}
// failed
if err != nil && t.remaining <= 0 {
writes <- writeOp{t.Name, failed}
return err
}
// success
writes <- writeOp{t.Name, successful}
return nil
}
func (t *Task) skip(writes chan writeOp) error {
writes <- writeOp{t.Name, skipped}
return nil
}
// RetryDelay is a type that implements a Wait() method, which is called in between
// task retry attempts.
type RetryDelay interface {
wait(taskName string, attempt int)
}
// ConstantDelay waits a constant number of seconds between task retries.
type ConstantDelay struct{ Period int }
func (d ConstantDelay) wait(task string, attempt int) {
time.Sleep(time.Duration(d.Period) * time.Second)
}
// ExponentialBackoff waits exponentially longer between each retry attempt.
type ExponentialBackoff struct{}
func (d ExponentialBackoff) wait(task string, attempt int) {
delay := math.Pow(2, float64(attempt))
time.Sleep(time.Duration(delay) * time.Second)
}