This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
238 lines (221 loc) · 5.2 KB
/
executor.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
package xgraph
import (
"context"
"sync"
)
type dispatchTracker struct {
// job is the Job
job Job
// ctx is a context for running the Job
ctx context.Context
// notch is the channel to send notifications to
notch chan notification
}
// OnComplete is the completion callback used for dispatching Jobs (implements WorkTracker)
func (dt *dispatchTracker) OnComplete(err error) {
dt.notch <- notification{
job: dt.job,
state: stateCompleted,
err: err,
}
}
func (dt *dispatchTracker) task() error {
dt.notch <- notification{
job: dt.job,
state: stateStarted,
}
err := dt.job.Run(dt.ctx)
return err
}
type notification struct {
// job is the job that this notification is about
job Job
// stats is the state which this notification is reporting
state int
// err is the error (if applicable) from the run
err error
}
const (
stateStarted = 1
stateCompleted = 2
)
type executor struct {
// forest is the jobtree we are using
forest map[string]*jTree
// runner is a WorkRunner used to run Jobs
runner WorkRunner
// wg is a sync.WaitGroup used to track shutdown of the executor
wg sync.WaitGroup
// dispatchch is a channel going to a goroutine which dispatches jobs
dispatchch chan Job
// bufch is a channel going to a goroutine which buffers jobs and relays them to runch
bufch chan Job
// notifych is a channel carrying notifications from the running jobs
notifych chan notification
// evh is the EventHandler being used to track this build
evh EventHandler
// proms is the set of promises for rules
proms map[string]*Promise
// cbset is the set of callbacks for Job completion
cbset map[string]func(error)
// ctx is the context used for execution (with cancel)
ctx context.Context
}
// startDispatcher populates dispatchch and starts a goroutine which dispatches jobs
// stops on channel close and uses the WaitGroup
func (ex *executor) startDispatcher() {
dispatch := ex.dispatchch
ex.wg.Add(1)
go func() {
defer ex.wg.Done()
ctxdone := ex.ctx.Done()
for {
select {
case j, ok := <-dispatch:
if !ok {
return
}
dt := &dispatchTracker{
job: j,
notch: ex.notifych,
ctx: ex.ctx,
}
ex.runner.DoTask(dt.task, dt)
case <-ctxdone:
for j := range dispatch { //drain dispatch buffer
ex.notifych <- notification{ //tell controller that they were canceled
job: j,
state: stateCompleted,
err: context.Canceled,
}
}
return
}
}
}()
ex.dispatchch = dispatch
}
// startDispatchBuffer starts a goroutine which buffers dispatches between bufch and dispatchch
func (ex *executor) startDispatchBuffer() {
bufch := ex.bufch
ex.wg.Add(1)
go func() {
defer ex.wg.Done()
defer close(ex.dispatchch)
buf := []Job{} //we dont care about order so just use a stack
for {
if len(buf) == 0 {
j, ok := <-bufch
if !ok {
return
}
buf = append(buf, j)
} else {
select {
case j, ok := <-bufch:
if !ok {
return
}
buf = append(buf, j)
case ex.dispatchch <- buf[len(buf)-1]:
buf = buf[:len(buf)-1]
}
}
}
}()
ex.bufch = bufch
}
// runJob places a job on the queue and returns a promise that resolves when the job completes
func (ex *executor) runJob(jt *jTree) *Promise {
return NewPromise(func(s FinishHandler, f FailHandler) {
ex.cbset[jt.name] = func(err error) {
if err == nil {
s()
} else {
f(err)
}
}
ex.bufch <- jt.job
})
}
// promise returns a promise that resolves when a given job finished building
func (ex *executor) promise(name string) *Promise {
var p *Promise
for p = ex.proms[name]; p == nil; p = ex.proms[name] {
jt := ex.forest[name]
ex.proms[name] = NewPromise(func(s FinishHandler, f FailHandler) {
//if there is a pre-existing error (e.g. dependency cycle), bail out
if jt.err != nil {
f(jt.err)
return
}
//prep dep promise
var dps *Promise
if len(jt.deps) > 0 {
depps := make(map[string]*Promise)
for _, v := range jt.deps {
depps[v.name] = ex.promise(v.name)
}
dps = newBuildPromise(depps)
} else {
dps = NewPromise(func(s FinishHandler, f FailHandler) {
s()
})
}
//run dep promise
dps.Then(
func() { //on success, run build
sr, err := jt.job.ShouldRun() //check if the job should run
if err != nil { //error out if we cant tell whether it should be run
f(err)
}
if sr {
ex.runJob(jt).Then(s, f)
} else {
s()
}
},
func(err error) {
f(err)
},
)
})
}
return p
}
func (ex *executor) execute() {
// start dispatcher/buffer
defer ex.wg.Wait()
ex.startDispatcher()
ex.startDispatchBuffer()
defer close(ex.bufch)
// start build promises
n := len(ex.forest)
for _, v := range ex.forest {
name := v.name
if v.err == nil { //if might be run, mark as queued
ex.evh.OnQueued(name)
}
ex.promise(name).Then( //start promise
func() {
ex.evh.OnFinish(name)
n--
},
func(err error) {
ex.evh.OnError(name, err)
n--
},
)
}
// do processing loop
for n > 0 {
not := <-ex.notifych
switch not.state {
case stateStarted:
ex.evh.OnStart(not.job.Name())
case stateCompleted:
ex.cbset[not.job.Name()](not.err)
}
}
//we are done!
}