-
Notifications
You must be signed in to change notification settings - Fork 21
/
job.go
682 lines (600 loc) · 15.5 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
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"
"path/filepath"
"strconv"
//"strings"
"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_FAILED_PERMANENT = "failed-permanent" // this sepcific error state can be trigger by the workflow software
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_INIT, JOB_STAT_QUEUED, JOB_STAT_INPROGRESS, JOB_STAT_SUSPEND}
type JobError struct {
ClientFailed string `bson:"clientfailed" json:"clientfailed"`
WorkFailed string `bson:"workfailed" json:"workfailed"`
TaskFailed string `bson:"taskfailed" json:"taskfailed"`
ServerNotes string `bson:"servernotes" json:"servernotes"`
WorkNotes string `bson:"worknotes" json:"worknotes"`
AppError string `bson:"apperror" json:"apperror"`
Status string `bson:"status" json:"status"`
}
type JobRaw struct {
RWMutex
Id string `bson:"id" json:"id"`
//Tasks []*Task `bson:"tasks" json:"tasks"`
Acl acl.Acl `bson:"acl" json:"-"`
Info *Info `bson:"info" json:"info"`
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"`
Error *JobError `bson:"error" json:"error"` // error struct exists when in suspended state
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 Job struct {
JobRaw `bson:",inline"`
Tasks []*Task `bson:"tasks" json:"tasks"`
}
// 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 {
JobRaw `bson:",inline"`
Tasks []*TaskDep `bson:"tasks" json:"tasks"`
}
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"`
ComputeTime int `bson:"computetime" json:"computetime"`
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"`
Error *JobError `bson:"error" json:"error"`
Resumed int `bson:"resumed" json:"resumed"`
Tasks []*TaskLog `bson:"tasks" json:"tasks"`
}
func NewJobRaw() (job *JobRaw) {
r := &JobRaw{
Info: NewInfo(),
Acl: acl.Acl{},
}
r.RWMutex.Init("Job")
return r
}
func NewJob() (job *Job) {
r_job := NewJobRaw()
job = &Job{JobRaw: *r_job}
return
}
func NewJobDep() (job *JobDep) {
r_job := NewJobRaw()
job = &JobDep{JobRaw: *r_job}
return
}
// this has to be called after Unmarshalling from JSON
func (job *Job) Init() (changed bool, err error) {
changed = false
job.RWMutex.Init("Job")
if job.State == "" {
job.State = JOB_STAT_INIT
changed = true
}
job.Registered = true
if job.Id == "" {
job.setId() //uuid for the job
logger.Debug(3, "(Job.Init) Set JobID: %s", job.Id)
changed = true
} else {
logger.Debug(3, "(Job.Init) Already have JobID: %s", job.Id)
}
if job.Info == nil {
logger.Error("job.Info == nil")
job.Info = NewInfo()
}
if job.Info.SubmitTime.IsZero() {
job.Info.SubmitTime = time.Now()
changed = true
}
if job.Info.Priority < conf.BasePriority {
job.Info.Priority = conf.BasePriority
changed = true
}
old_remaintasks := job.RemainTasks
job.RemainTasks = 0
for _, task := range job.Tasks {
if task.Id == "" {
// suspend and create error
logger.Error("(job.Init) task.Id empty, job %s broken?", job.Id)
task.Id = job.Id + "_" + uuid.New()
job.State = JOB_STAT_SUSPEND
job.Error = &JobError{
ServerNotes: "task.Id was empty",
TaskFailed: task.Id,
}
changed = true
}
t_changed, xerr := task.Init(job)
if xerr != nil {
err = xerr
return
}
if t_changed {
changed = true
}
if task.State != TASK_STAT_COMPLETED {
job.RemainTasks += 1
}
}
// try to fix inconsistent state
if job.RemainTasks != old_remaintasks {
changed = true
}
// try to fix inconsistent state
if job.RemainTasks == 0 && job.State != JOB_STAT_COMPLETED {
job.State = JOB_STAT_COMPLETED
logger.Debug(3, "fixing state to JOB_STAT_COMPLETED")
changed = true
}
// fix job.Info.CompletedTime
if job.State == JOB_STAT_COMPLETED && job.Info.CompletedTime.IsZero() {
// better now, than never:
job.Info.CompletedTime = time.Now()
changed = true
}
// try to fix inconsistent state
if job.RemainTasks > 0 && job.State == JOB_STAT_COMPLETED {
job.State = JOB_STAT_QUEUED
logger.Debug(3, "fixing state to JOB_STAT_QUEUED")
changed = true
}
if len(job.Tasks) == 0 {
err = errors.New("(job.Init) invalid job script: task list empty")
return
}
// check that input FileName is not repeated within an individual task
for _, task := range job.Tasks {
inputFileNames := make(map[string]bool)
for _, io := range task.Inputs {
if _, exists := inputFileNames[io.FileName]; exists {
err = errors.New("invalid inputs: task " + task.Id + " contains multiple inputs with filename=" + io.FileName)
return
}
inputFileNames[io.FileName] = true
}
}
return
}
func (job *Job) RLockRecursive() {
for _, task := range job.Tasks {
task.RLockAnon()
}
}
func (job *Job) RUnlockRecursive() {
for _, task := range job.Tasks {
task.RUnlockAnon()
}
}
//set job's uuid
func (job *Job) setId() {
job.Id = uuid.New()
return
}
type script struct {
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Path string `bson:"path" json:"-"`
}
//---Script upload (e.g. field="upload")
func (job *Job) UpdateFile(files FormFiles, field string) (err error) {
_, isRegularUpload := files[field]
if isRegularUpload {
if err = job.SetFile(files[field]); err != nil {
return err
}
delete(files, field)
}
return
}
func (job *Job) SaveToDisk() (err error) {
var job_path string
job_path, err = job.Path()
if err != nil {
err = fmt.Errorf("Save() Path error: %v", err)
return
}
bsonPath := path.Join(job_path, job.Id+".bson")
os.Remove(bsonPath)
logger.Debug(1, "Save() bson.Marshal next: %s", job.Id)
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
}
return
}
func (job *Job) Save() (err error) {
if job.Id == "" {
err = fmt.Errorf("job id empty")
return
}
logger.Debug(1, "Save() saving job: %s", job.Id)
job.UpdateTime = time.Now()
err = job.SaveToDisk()
if err != nil {
return
}
logger.Debug(1, "Save() dbUpsert next: %s", job.Id)
err = dbUpsert(job)
if err != nil {
err = fmt.Errorf("error in dbUpsert in job.Save(), (job_id=%s) error=%v", job.Id, err)
return
}
logger.Debug(1, "Save() job saved: %s", job.Id)
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) {
var path string
path, err = job.Path()
if err != nil {
return
}
err = os.MkdirAll(path, 0777)
if err != nil {
err = fmt.Errorf("Could not run os.MkdirAll (path: %s) %s", path, err.Error())
return
}
return
}
func (job *Job) Rmdir() (err error) {
var path string
path, err = job.Path()
if err != nil {
return
}
return os.RemoveAll(path)
}
func (job *Job) SetFile(file FormFile) (err error) {
var path string
path, err = job.FilePath()
os.Rename(file.Path, path)
job.Script.Name = file.Name
return
}
//---Path functions
func (job *Job) Path() (path string, err error) {
return getPathByJobId(job.Id)
}
func (job *Job) FilePath() (path string, err error) {
if job.Script.Path != "" {
path = job.Script.Path
return
}
path, err = getPathByJobId(job.Id)
if err != nil {
return
}
path = path + "/" + job.Id + ".script"
return
}
func getPathByJobId(id string) (path string, err error) {
if len(id) < 6 {
err = fmt.Errorf("Job-Id format wrong: \"%s\"", id)
return
}
path = fmt.Sprintf("%s/%s/%s/%s/%s", conf.DATA_PATH, id[0:2], id[2:4], id[4:6], id)
return
}
func (job *Job) GetTasks() (tasks []*Task, err error) {
tasks = []*Task{}
read_lock, err := job.RLockNamed("GetTasks")
if err != nil {
return
}
defer job.RUnlockNamed(read_lock)
for _, task := range job.Tasks {
tasks = append(tasks, task)
}
return
}
func (job *Job) GetState(do_lock bool) (state string, err error) {
if do_lock {
read_lock, xerr := job.RLockNamed("GetState")
if xerr != nil {
err = xerr
return
}
defer job.RUnlockNamed(read_lock)
}
state = job.State
return
}
//---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) SetState(newState string) (err error) {
err = job.LockNamed("SetState")
if err != nil {
return
}
defer job.Unlock()
if job.State == newState {
return
}
err = dbUpdateJobFieldString(job.Id, "state", newState)
if err != nil {
return
}
job.State = newState
// set time if completed
if newState == JOB_STAT_COMPLETED {
newTime := time.Now()
err = dbUpdateJobFieldTime(job.Id, "info.completedtime", newTime)
if err != nil {
return
}
job.Info.CompletedTime = newTime
}
// unset error if not suspended
if (newState != JOB_STAT_SUSPEND) && (job.Error != nil) {
err = dbUpdateJobFieldNull(job.Id, "error")
if err != nil {
return
}
job.Error = nil
}
return
}
func (job *Job) SetError(newError *JobError) (err error) {
err = job.LockNamed("SetError")
if err != nil {
return
}
defer job.Unlock()
update_value := bson.M{"error": newError}
err = dbUpdateJobFields(job.Id, update_value)
if err != nil {
return
}
job.Error = newError
return
}
func (job *Job) GetRemainTasks() (remain_tasks int, err error) {
remain_tasks = job.RemainTasks
return
}
func (job *Job) SetRemainTasks(remain_tasks int) (err error) {
err = job.LockNamed("SetRemainTasks")
if err != nil {
return
}
defer job.Unlock()
if remain_tasks == job.RemainTasks {
return
}
err = dbUpdateJobFieldInt(job.Id, "remaintasks", remain_tasks)
if err != nil {
return
}
job.RemainTasks = remain_tasks
return
}
func (job *Job) IncrementRemainTasks(inc int) (err error) {
err = job.LockNamed("IncrementRemainTasks")
if err != nil {
return
}
defer job.Unlock()
newRemainTask := job.RemainTasks + inc
err = dbUpdateJobFieldInt(job.Id, "remaintasks", newRemainTask)
if err != nil {
return
}
job.RemainTasks = newRemainTask
return
}
func (job *Job) IncrementResumed(inc int) (err error) {
err = job.LockNamed("IncrementResumed")
if err != nil {
return
}
defer job.Unlock()
newResumed := job.Resumed + inc
err = dbUpdateJobFieldInt(job.Id, "resumed", newResumed)
if err != nil {
return
}
job.Resumed = newResumed
return
}
func (job *Job) SetClientgroups(clientgroups string) (err error) {
err = job.LockNamed("SetClientgroups")
if err != nil {
return
}
defer job.Unlock()
err = dbUpdateJobFieldString(job.Id, "info.clientgroups", clientgroups)
if err != nil {
return
}
job.Info.ClientGroups = clientgroups
return
}
func (job *Job) SetPriority(priority int) (err error) {
err = job.LockNamed("SetPriority")
if err != nil {
return
}
defer job.Unlock()
err = dbUpdateJobFieldInt(job.Id, "info.priority", priority)
if err != nil {
return
}
job.Info.Priority = priority
return
}
func (job *Job) SetPipeline(pipeline string) (err error) {
err = job.LockNamed("SetPipeline")
if err != nil {
return
}
defer job.Unlock()
err = dbUpdateJobFieldString(job.Id, "info.pipeline", pipeline)
if err != nil {
return
}
job.Info.Pipeline = pipeline
return
}
func (job *Job) SetDataToken(token string) (err error) {
err = job.LockNamed("SetDataToken")
if err != nil {
return
}
defer job.Unlock()
if job.Info.DataToken == token {
return
}
// update toekn in info
err = dbUpdateJobFieldString(job.Id, "info.token", token)
if err != nil {
return
}
job.Info.DataToken = token
// update token in IO structs
err = QMgr.UpdateQueueToken(job)
if err != nil {
return
}
// set using auth if not before
if !job.Info.Auth {
err = dbUpdateJobFieldBoolean(job.Id, "info.auth", true)
if err != nil {
return
}
job.Info.Auth = true
}
return
}
func (job *Job) SetExpiration(expire string) (err error) {
err = job.LockNamed("SetExpiration")
if err != nil {
return
}
defer job.Unlock()
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
}
newExpiration := currTime.Add(expireTime)
err = dbUpdateJobFieldTime(job.Id, "expiration", newExpiration)
if err != nil {
return
}
job.Expiration = newExpiration
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.Error = job.Error
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 := NewJob()
err = bson.Unmarshal(jobbson, &job)
if err == nil {
if err = dbUpsert(job); err != nil {
return err
}
}
return
}