-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtasks.go
165 lines (130 loc) · 3.1 KB
/
tasks.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
package parser
import (
"sort"
"strings"
"github.com/tliron/kutil/terminal"
"github.com/tliron/puccini/tosca/parsing"
)
type Executor func(task *Task)
//
// Task
//
type Task struct {
Name string
Executor Executor
Parents Tasks
Dependencies Tasks
}
type Tasks map[*Task]struct{}
type TasksForEntities map[parsing.EntityPtr]*Task
func NewTask(name string) *Task {
return &Task{Name: name, Parents: make(Tasks), Dependencies: make(Tasks)}
}
func (self *Task) IsIndependent() bool {
return len(self.Dependencies) == 0
}
func (self *Task) Execute() {
// If we got here, we should be independent (no dependencies)
self.Executor(self)
}
func (self *Task) Done() {
// If we got here, we should be independent (no dependencies)
for parent := range self.Parents {
parent.Dependencies.Remove(self)
}
// Make sure we won't be reused
self.Executor = nil
self.Parents = nil
self.Dependencies = nil
}
func (self *Task) Print(indent int) {
terminal.PrintIndent(indent)
terminal.Printf("%s\n", terminal.StdoutStylist.Path(self.Name))
self.PrintDependencies(indent, terminal.TreePrefix{})
}
func (self *Task) PrintDependencies(indent int, treePrefix terminal.TreePrefix) {
// Sort
var taskList TaskList
for dependency := range self.Dependencies {
taskList = append(taskList, dependency)
}
sort.Sort(taskList)
last := len(taskList) - 1
for i, dependency := range taskList {
isLast := i == last
dependency.PrintDependency(indent, treePrefix, isLast)
dependency.PrintDependencies(indent, append(treePrefix, isLast))
}
}
func (self *Task) PrintDependency(indent int, treePrefix terminal.TreePrefix, last bool) {
treePrefix.Print(indent, last)
terminal.Printf("%s\n", self.Name)
}
func (self *Task) AddDependency(task *Task) {
self.Dependencies.Add(task)
task.Parents.Add(self)
}
//
// Tasks
//
func (self Tasks) Add(task *Task) {
self[task] = struct{}{}
}
func (self Tasks) Remove(task *Task) {
delete(self, task)
}
func (self Tasks) FindIndependent() (*Task, bool) {
for task := range self {
if task.IsIndependent() {
return task, true
}
}
return nil, false
}
func (self Tasks) Validate() bool {
// TODO make sure there are no endless loops
return true
}
func (self Tasks) Drain() {
if !self.Validate() {
return
}
logTasks.Debugf("starting %d tasks", len(self))
for true {
task, ok := self.FindIndependent()
if !ok {
break
}
self.Remove(task)
task.Execute()
// After one independent task is done, other tasks should become independent
}
if len(self) > 0 {
logTasks.Warningf("%d tasks not completed", len(self))
}
}
// Print
func (self Tasks) Print(indent int) {
// Sort
var taskList TaskList
for task := range self {
taskList = append(taskList, task)
}
sort.Sort(taskList)
for _, task := range taskList {
task.Print(indent)
}
}
type TaskList []*Task
// ([sort.Interface])
func (self TaskList) Len() int {
return len(self)
}
// ([sort.Interface])
func (self TaskList) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
// ([sort.Interface])
func (self TaskList) Less(i, j int) bool {
return strings.Compare(self[i].Name, self[j].Name) < 0
}