-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
234 lines (189 loc) · 4.4 KB
/
job.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
package jobs
import (
"context"
"github.com/google/uuid"
"os"
"sync"
"time"
)
const maxUpdateHistory = 20
type JobState int
const (
StateQueued JobState = iota + 1
StateRunning
StateCancelling
StateCompleted
StateError
StateCancelled
)
func (js JobState) String() string {
switch js {
case StateQueued:
return "Queued"
case StateRunning:
return "Running"
case StateCompleted:
return "Done"
case StateError:
return "Error"
case StateCancelling:
return "Cancelling"
case StateCancelled:
return "Cancelled"
}
return "Unknown"
}
func (js JobState) Terminal() bool {
switch js {
case StateCompleted, StateCancelled, StateError:
return true
}
return false
}
// Job is a handle to a running job
type Job struct {
id uuid.UUID
task Task
createdAt time.Time
// state variables. These are under the control of the mutex
stateMutex sync.Mutex
state JobState
err error
cancelFn func()
lastUpdate Update
lastMessage string
updateHistory *updateHistory
data map[string]interface{}
}
func newJob(task Task) *Job {
return &Job{
id: uuid.New(),
task: task,
createdAt: time.Now(),
state: StateQueued,
stateMutex: sync.Mutex{},
updateHistory: newUpdateHistory(maxUpdateHistory),
data: make(map[string]interface{}),
}
}
// exec executes the job. This is assumed to be executed by a go routine
func (j *Job) exec(ctx context.Context, runContext RunContext) {
execCtx, cancelFn := context.WithCancel(ctx)
defer cancelFn()
j.setState(runContext, StateRunning, nil, cancelFn)
err := j.task.Execute(execCtx, runContext)
select {
case <-execCtx.Done():
runContext.PostMessage(execCtx.Err().Error())
runContext.PostUpdate(Update{Summary: "Cancelled"})
j.setState(runContext, StateCancelled, execCtx.Err(), nil)
default:
if err != nil {
runContext.PostMessage("error: " + err.Error())
runContext.PostUpdate(Update{Summary: "Error"})
j.setState(runContext, StateError, err, nil)
} else {
runContext.PostUpdate(Update{Percent: 100.0, Summary: "Done"})
j.setState(runContext, StateCompleted, nil, nil)
}
}
}
func (j *Job) ID() uuid.UUID {
return j.id
}
// Task returns the original task of the job
func (j *Job) Task() Task {
return j.task
}
func (j *Job) CreatedAt() time.Time {
return j.createdAt
}
func (j *Job) State() JobState {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
return j.state
}
func (j *Job) Data() map[string]interface{} {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
dataCp := make(map[string]interface{})
for k, v := range j.data {
dataCp[k] = v
}
return dataCp
}
func (j *Job) GetData(key string) interface{} {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
return j.data[key]
}
func (j *Job) SetData(key string, value interface{}) {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
j.data[key] = value
}
// setState sets the job state in a safe manner
func (j *Job) setState(runContext RunContext, state JobState, err error, cancelFn func()) {
var oldState JobState
// Critical section
j.stateMutex.Lock()
oldState = j.state
j.state = state
j.err = err
j.cancelFn = cancelFn
j.stateMutex.Unlock()
// End critical section
if oldState != state {
runContext.postStateChange(oldState, state)
}
}
// setState sets the job state in a safe manner
func (j *Job) postUpdate(lastUpdate Update) {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
j.lastUpdate = lastUpdate
}
func (j *Job) postMessage(message string) {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
j.lastMessage = message
j.updateHistory.push(message)
}
func (j *Job) LastUpdate() Update {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
return j.lastUpdate
}
func (j *Job) LastMessage() string {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
return j.lastMessage
}
func (j *Job) MessageHistory() []string {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
return j.updateHistory.list()
}
// Cancel cancels a running job
func (j *Job) Cancel() {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
if j.state == StateCancelling {
return
}
if j.cancelFn != nil {
j.state = StateCancelling
j.cancelFn()
}
}
// Err returns the error from running the job
func (j *Job) Err() error {
j.stateMutex.Lock()
defer j.stateMutex.Unlock()
return j.err
}
func (j *Job) Cleanup() {
if tmpDir, ok := j.data["_tmp_dir"].(string); ok {
os.RemoveAll(tmpDir)
}
}