-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobhub.go
229 lines (205 loc) · 4.88 KB
/
jobhub.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
package jobhub
import (
"fmt"
"os"
"os/exec"
"time"
"github.com/cenkalti/backoff"
)
var nextPipelineID = 1
type Pipeline struct {
name string
id int
nextJobID int
jobContainer []Job
jobByID map[int]Job
jobDependency map[int][]int
}
type Job struct {
Name string
Path string
Args []string
Retry int
Backoff backoff.BackOff
id int
pipelineID int
}
type PipelineStatus struct {
PipelineName string
Status ExecutionStatus
JobStatus []JobStatus
}
type JobStatus struct {
Job Job
JobID int
LastStatus ExecutionStatus
Statuses []ExecutionStatus
}
type ExecutionStatus struct {
ExecutionStart time.Time
Code StatusCode
Runtime time.Duration
}
type StatusCode int
const (
Created StatusCode = 0
Scheduled StatusCode = 1
Failed StatusCode = 2
Succeeded StatusCode = 3
)
var statusDescription = map[StatusCode]string{
Created: "Created",
Scheduled: "Scheduled",
Failed: "Failed",
Succeeded: "Succeeded",
}
func (j JobStatus) LastExecutionStatus() *ExecutionStatus {
if len(j.Statuses) > 0 {
return &j.Statuses[len(j.Statuses)-1]
}
return nil
}
func (s StatusCode) String() string {
return statusDescription[s]
}
func NewPipeline(name string) *Pipeline {
return &Pipeline{
name: name,
id: nextIDPipeline(),
jobByID: make(map[int]Job),
jobDependency: make(map[int][]int),
}
}
func nextIDPipeline() int {
tempID := nextPipelineID
nextPipelineID++
return tempID
}
func (p *Pipeline) nextIDJob() int {
p.nextJobID++
return p.nextJobID
}
func (p *Pipeline) AddJob(job Job) (Job, error) {
if p.id == 0 {
return job, fmt.Errorf("%s (%d) not initalized (misuse of constructor)", p.name, p.id)
}
for _, j := range p.jobContainer {
if j.id == job.id {
return j, fmt.Errorf("%s (%d) in %s (%d) has already been added", j.Name, j.id, p.name, p.id)
}
}
job.id = p.nextIDJob()
job.pipelineID = p.id
p.jobContainer = append(p.jobContainer, job)
p.jobByID[job.id] = job
return job, nil
}
func (p *Pipeline) AddJobDependency(job Job, deps ...Job) error {
if p.jobContainer == nil {
return fmt.Errorf("%s (%d) has no jobs added", p.name, p.id)
}
for _, j := range append(deps, job) {
if j.pipelineID != p.id {
return fmt.Errorf("%s (%d) does not belong to %s (%d)", j.Name, j.id, p.name, p.id)
}
}
for _, d := range deps {
p.jobDependency[job.id] = append(p.jobDependency[job.id], d.id)
}
return nil
}
func (p Pipeline) topologicalSort() ([]int, error) {
var (
temporaryMark = make(map[int]bool)
permanentMark = make(map[int]bool)
acyclic = true
queue []int
visit func(int)
)
visit = func(u int) {
if temporaryMark[u] {
acyclic = false
} else if !(temporaryMark[u] || permanentMark[u]) {
temporaryMark[u] = true
for _, v := range p.jobDependency[u] {
visit(v)
if !acyclic {
return
}
}
delete(temporaryMark, u)
permanentMark[u] = true
queue = append(queue, u)
}
}
for u := range p.jobDependency {
if !permanentMark[u] {
visit(u)
if !acyclic {
return nil, fmt.Errorf("%s (%d) is not a DAG", p.name, p.id)
}
}
}
return queue, nil
}
func runJob(job Job) (ExecutionStatus, error) {
ret := ExecutionStatus{ExecutionStart: time.Now().UTC()}
if _, err := os.Stat(job.Path); err != nil {
ret.Code = Failed
return ret, err
}
start := time.Now()
err := exec.Command(job.Path, job.Args...).Run()
ret.Runtime = time.Since(start)
if err != nil {
ret.Code = Failed
return ret, err
}
ret.Code = Succeeded
return ret, err
}
func (p Pipeline) Run() (PipelineStatus, error) {
ret := PipelineStatus{PipelineName: p.name}
queue, err := p.topologicalSort()
if err != nil {
return ret, err
} else if queue == nil {
return ret, fmt.Errorf("%s (%d) has not been scheduled", p.name, p.id)
}
ret.JobStatus = make([]JobStatus, len(queue))
var (
executionStatus ExecutionStatus
currentRetry int
nextBackoff time.Duration
)
for i, jID := range queue {
ret.JobStatus[i].Job = p.jobByID[jID]
ret.JobStatus[i].JobID = jID
ret.JobStatus[i].LastStatus.Code = Scheduled
}
ret.Status.ExecutionStart = time.Now().UTC()
for i, jID := range queue {
job := p.jobByID[jID]
for {
executionStatus, err = runJob(job)
ret.JobStatus[i].Statuses = append(ret.JobStatus[i].Statuses, executionStatus)
ret.JobStatus[i].LastStatus = *ret.JobStatus[i].LastExecutionStatus()
ret.Status.Runtime += executionStatus.Runtime
if err == nil {
break
}
currentRetry++
if job.Backoff != nil {
nextBackoff = job.Backoff.NextBackOff()
}
if (currentRetry >= job.Retry && job.Retry != -1) || nextBackoff == backoff.Stop {
ret.Status.Code = Failed
return ret, fmt.Errorf("%s (%d) in %s (%d) returned a permanent error (%v)",
job.Name, job.id, p.name, p.id, err)
}
time.Sleep(nextBackoff)
}
}
ret.Status.Code = Succeeded
return ret, nil
}