/
task.go
80 lines (73 loc) · 2.48 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
package crew
import (
"time"
)
// A Task represents a unit of work that can be completed by a worker.
// IMPORTANT! If you change task's fields, also update Task.ts in crew-go-javascript
type Task struct {
Id string `json:"id"`
TaskGroupId string `json:"taskGroupId"`
Name string `json:"name"`
Worker string `json:"worker"`
Workgroup string `json:"workgroup"`
Key string `json:"key"`
RemainingAttempts int `json:"remainingAttempts"`
IsPaused bool `json:"isPaused"`
IsComplete bool `json:"isComplete"`
RunAfter time.Time `json:"runAfter"`
IsSeed bool `json:"isSeed"`
ErrorDelayInSeconds int `json:"errorDelayInSeconds"`
Input interface{} `json:"input"`
Output interface{} `json:"output"`
Errors []string `json:"errors"`
CreatedAt time.Time `json:"createdAt"`
ParentIds []string `json:"parentIds"`
BusyExecuting bool `json:"busyExecuting"`
Storage TaskStorage `json:"-"`
}
// NewTask creates a new Task.
func NewTask() *Task {
task := Task{
Id: "",
TaskGroupId: "",
Name: "",
Worker: "",
Workgroup: "",
Key: "",
RemainingAttempts: 5,
IsPaused: false,
IsComplete: false,
RunAfter: time.Now(),
IsSeed: false,
ErrorDelayInSeconds: 60,
Input: nil,
Output: nil,
Errors: make([]string, 0),
CreatedAt: time.Now(),
ParentIds: make([]string, 0),
BusyExecuting: false,
}
return &task
}
// CanExecute determines if a Task is in a state where it can be executed.
func (task *Task) CanExecute(parents []*Task) bool {
// Task should not execute if
// - it is already complete
// - it is paused
// - it has no remaining attempts
// - its task group is paused
// Note that we do not check runAfter here, task timing is handled by operator
if task.IsComplete || task.IsPaused || task.RemainingAttempts <= 0 {
return false
}
if task.Worker == "" {
return false
}
// Task should not execute if any of its parents are incomplete
for _, parent := range parents {
if !parent.IsComplete {
return false
}
}
return true
}