-
Notifications
You must be signed in to change notification settings - Fork 21
/
job.go
348 lines (313 loc) · 9.56 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package core
import (
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/acl"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core/uuid"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/logger/event"
"gopkg.in/mgo.v2/bson"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
)
const (
JOB_STAT_INIT = "init"
JOB_STAT_QUEUED = "queued"
JOB_STAT_INPROGRESS = "in-progress"
JOB_STAT_COMPLETED = "completed"
JOB_STAT_SUSPEND = "suspend"
JOB_STAT_DELETED = "deleted"
)
var JOB_STATS_ACTIVE = []string{JOB_STAT_QUEUED, JOB_STAT_INPROGRESS}
var JOB_STATS_REGISTERED = []string{JOB_STAT_QUEUED, JOB_STAT_INPROGRESS, JOB_STAT_SUSPEND}
var JOB_STATS_TO_RECOVER = []string{JOB_STAT_QUEUED, JOB_STAT_INPROGRESS, JOB_STAT_SUSPEND}
type Job struct {
Id string `bson:"id" json:"id"`
Jid string `bson:"jid" json:"jid"`
Acl acl.Acl `bson:"acl" json:"-"`
Info *Info `bson:"info" json:"info"`
Tasks []*Task `bson:"tasks" json:"tasks"`
Script script `bson:"script" json:"-"`
State string `bson:"state" json:"state"`
Registered bool `bson:"registered" json:"registered"`
RemainTasks int `bson:"remaintasks" json:"remaintasks"`
Expiration time.Time `bson:"expiration" json:"expiration"` // 0 means no expiration
UpdateTime time.Time `bson:"updatetime" json:"updatetime"`
Notes string `bson:"notes" json:"notes"`
LastFailed string `bson:"lastfailed" json:"lastfailed"`
Resumed int `bson:"resumed" json:"resumed"` // number of times the job has been resumed from suspension
ShockHost string `bson:"shockhost" json:"shockhost"` // this is a fall-back default if not specified at a lower level
}
// Deprecated JobDep struct uses deprecated TaskDep struct which uses the deprecated IOmap. Maintained for backwards compatibility.
// Jobs that cannot be parsed into the Job struct, but can be parsed into the JobDep struct will be translated to the new Job struct.
// (=deprecated=)
type JobDep struct {
Id string `bson:"id" json:"id"`
Jid string `bson:"jid" json:"jid"`
Acl acl.Acl `bson:"acl" json:"-"`
Info *Info `bson:"info" json:"info"`
Tasks []*TaskDep `bson:"tasks" json:"tasks"`
Script script `bson:"script" json:"-"`
State string `bson:"state" json:"state"`
Registered bool `bson:"registered" json:"registered"`
RemainTasks int `bson:"remaintasks" json:"remaintasks"`
UpdateTime time.Time `bson:"updatetime" json:"updatetime"`
Notes string `bson:"notes" json:"notes"`
LastFailed string `bson:"lastfailed" json:"lastfailed"`
Resumed int `bson:"resumed" json:"resumed"` //number of times the job has been resumed from suspension
ShockHost string `bson:"shockhost" json:"shockhost"` // this is a fall-back default if not specified at a lower level
}
type JobMin struct {
Id string `bson:"id" json:"id"`
Name string `bson:"name" json:"name"`
Size int64 `bson:"size" json:"size"`
SubmitTime time.Time `bson:"submittime" json:"submittime"`
CompletedTime time.Time `bson:"completedtime" json:"completedtime"`
Task []int `bson:"task" json:"task"`
State []string `bson:"state" json:"state"`
UserAttr map[string]string `bson:"userattr" json:"userattr"`
}
type JobLog struct {
Id string `bson:"id" json:"id"`
State string `bson:"state" json:"state"`
UpdateTime time.Time `bson:"updatetime" json:"updatetime"`
Notes string `bson:"notes" json:"notes"`
LastFailed string `bson:"lastfailed" json:"lastfailed"`
Resumed int `bson:"resumed" json:"resumed"`
Tasks []*TaskLog `bson:"tasks" json:"tasks"`
}
type JobID struct {
Name string `bson:"name" json:"name"`
Max int `bson:"max" json:"max"`
}
//set job's uuid
func (job *Job) setId() {
job.Id = uuid.New()
return
}
//set job's jid
func (job *Job) setJid(jid string) {
job.Jid = jid
}
func (job *Job) initJob(jid string) {
if job.Info == nil {
job.Info = new(Info)
}
job.Info.SubmitTime = time.Now()
job.Info.Priority = conf.BasePriority
job.setId() //uuid for the job
job.setJid(jid) //an incremental id for the jobs within a AWE server domain
job.State = JOB_STAT_INIT
job.Registered = true
}
type script struct {
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Path string `bson:"path" json:"-"`
}
//---Script upload
func (job *Job) UpdateFile(files FormFiles) (err error) {
_, isRegularUpload := files["upload"]
if isRegularUpload {
if err = job.SetFile(files["upload"]); err != nil {
return err
}
delete(files, "upload")
}
return
}
func (job *Job) Save() (err error) {
job.UpdateTime = time.Now()
bsonPath := fmt.Sprintf("%s/%s.bson", job.Path(), job.Id)
os.Remove(bsonPath)
nbson, err := bson.Marshal(job)
if err != nil {
err = errors.New("error in Marshal in job.Save(), error=" + err.Error())
return
}
// this is incase job path does not exist, ignored if it does
err = job.Mkdir()
if err != nil {
err = errors.New("error creating dir in job.Save(), error=" + err.Error())
return
}
err = ioutil.WriteFile(bsonPath, nbson, 0644)
if err != nil {
err = errors.New("error writing file in job.Save(), error=" + err.Error())
return
}
err = dbUpsert(job)
if err != nil {
err = errors.New("error in dbUpdate in job.Save(), error=" + err.Error())
return
}
return
}
func (job *Job) Delete() (err error) {
if err = dbDelete(bson.M{"id": job.Id}, conf.DB_COLL_JOBS); err != nil {
return err
}
if err = job.Rmdir(); err != nil {
return err
}
logger.Event(event.JOB_FULL_DELETE, "jobid="+job.Id)
return
}
func (job *Job) Mkdir() (err error) {
err = os.MkdirAll(job.Path(), 0777)
if err != nil {
return
}
return
}
func (job *Job) Rmdir() (err error) {
return os.RemoveAll(job.Path())
}
func (job *Job) SetFile(file FormFile) (err error) {
os.Rename(file.Path, job.FilePath())
job.Script.Name = file.Name
return
}
//---Path functions
func (job *Job) Path() string {
return getPathByJobId(job.Id)
}
func (job *Job) FilePath() string {
if job.Script.Path != "" {
return job.Script.Path
}
return getPathByJobId(job.Id) + "/" + job.Id + ".script"
}
func getPathByJobId(id string) string {
return fmt.Sprintf("%s/%s/%s/%s/%s", conf.DATA_PATH, id[0:2], id[2:4], id[4:6], id)
}
//---Task functions
func (job *Job) TaskList() []*Task {
return job.Tasks
}
func (job *Job) NumTask() int {
return len(job.Tasks)
}
//---Field update functions
func (job *Job) UpdateState(newState string, notes string) (err error) {
job.State = newState
if len(notes) > 0 {
job.Notes = notes
}
return job.Save()
}
//invoked to modify job info in mongodb when a task in that job changed to the new status
func (job *Job) UpdateTask(task *Task) (remainTasks int, err error) {
idx := -1
for i, t := range job.Tasks {
if t.Id == task.Id {
idx = i
break
}
}
if idx == -1 {
return job.RemainTasks, errors.New("job.UpdateTask: no task found with id=" + task.Id)
}
job.Tasks[idx] = task
//if this task is complete, count remain tasks for the job
if task.State == TASK_STAT_COMPLETED ||
task.State == TASK_STAT_SKIPPED ||
task.State == TASK_STAT_FAIL_SKIP {
remain_tasks := len(job.Tasks)
for _, t := range job.Tasks { //double check all task other than the one with state change
if t.State == TASK_STAT_COMPLETED ||
t.State == TASK_STAT_SKIPPED ||
t.State == TASK_STAT_FAIL_SKIP {
remain_tasks -= 1
}
}
job.RemainTasks = remain_tasks
if job.RemainTasks == 0 {
job.State = JOB_STAT_COMPLETED
job.Info.CompletedTime = time.Now()
}
}
return job.RemainTasks, job.Save()
}
func (job *Job) SetPipeline(pipeline string) (err error) {
job.Info.Pipeline = pipeline
for _, task := range job.Tasks {
task.Info.Pipeline = pipeline
}
err = job.Save()
return
}
func (job *Job) SetExpiration(expire string) (err error) {
parts := ExpireRegex.FindStringSubmatch(expire)
if len(parts) == 0 {
return errors.New("expiration format '" + expire + "' is invalid")
}
var expireTime time.Duration
expireNum, _ := strconv.Atoi(parts[1])
currTime := time.Now()
switch parts[2] {
case "M":
expireTime = time.Duration(expireNum) * time.Minute
case "H":
expireTime = time.Duration(expireNum) * time.Hour
case "D":
expireTime = time.Duration(expireNum*24) * time.Hour
}
job.Expiration = currTime.Add(expireTime)
err = job.Save()
return
}
//set token
func (job *Job) SetDataToken(token string) (err error) {
job.Info.DataToken = token
job.Info.Auth = true
for _, task := range job.Tasks {
task.Info.DataToken = token
task.setTokenForIO()
}
err = job.Save()
return
}
func (job *Job) GetDataToken() (token string) {
return job.Info.DataToken
}
func (job *Job) GetPrivateEnv(taskid string) (env map[string]string) {
for _, task := range job.Tasks {
if taskid == task.Id {
return task.Cmd.Environ.Private
}
}
return
}
func (job *Job) GetJobLogs() (jlog *JobLog, err error) {
jlog = new(JobLog)
jlog.Id = job.Id
jlog.State = job.State
jlog.UpdateTime = job.UpdateTime
jlog.Notes = job.Notes
jlog.LastFailed = job.LastFailed
jlog.Resumed = job.Resumed
for _, task := range job.Tasks {
jlog.Tasks = append(jlog.Tasks, task.GetTaskLogs())
}
return
}
func ReloadFromDisk(path string) (err error) {
id := filepath.Base(path)
jobbson, err := ioutil.ReadFile(path + "/" + id + ".bson")
if err != nil {
return
}
job := new(Job)
err = bson.Unmarshal(jobbson, &job)
if err == nil {
if err = dbUpsert(job); err != nil {
return err
}
}
return
}