-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
79 lines (65 loc) · 1.58 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
package ltt
import (
"context"
"strings"
)
type TaskFunc func(context.Context) error
type TaskSelectionStrategyType int
const (
TaskSelectionStrategyRandom TaskSelectionStrategyType = iota
TaskSelectionStrategyInOrder
)
type TaskOptions struct {
SelectionStrategy TaskSelectionStrategyType
StepOutWeight int
Weight int
}
type Task struct {
Name string
Parent *Task
SubTasks []*Task
RunFunc TaskFunc
Options TaskOptions
}
// Adds and returns an empty section Task and passes it to the callback
// The TaskOptions will be set on the new section task.
func (t *Task) AddSection(name string, setup func(*Task), opts TaskOptions) *Task {
st := t.AddSubTask(name, nil, opts)
setup(st)
return st
}
func (t *Task) AddSubTask(name string, f TaskFunc, opts TaskOptions) *Task {
st := NewTask(name, t, f, opts)
t.SubTasks = append(t.SubTasks, st)
return st
}
func (t *Task) FullName() string {
if t.Parent == nil {
return t.Name
}
parentNames := []string{}
p := t.Parent
for p != nil {
parentNames = append(parentNames, p.Name)
p = p.Parent
}
sb := strings.Builder{}
for i := len(parentNames) - 1; i >= 0; i-- {
sb.WriteString(parentNames[i])
sb.WriteString(" / ")
}
sb.WriteString(t.Name)
return sb.String()
}
// The entry task is the first task each User run, and only once
func NewEntryTask(name string, f TaskFunc, opts TaskOptions) *Task {
return NewTask(name, nil, f, opts)
}
func NewTask(name string, parent *Task, f TaskFunc, opts TaskOptions) *Task {
return &Task{
Name: name,
Parent: parent,
RunFunc: f,
Options: opts,
}
}