-
Notifications
You must be signed in to change notification settings - Fork 21
/
job.go
1011 lines (869 loc) · 24.6 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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package core
import (
b64 "encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/acl"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core/cwl"
//cwl_types "github.com/MG-RAST/AWE/lib/core/cwl/types"
"github.com/MG-RAST/AWE/lib/core/uuid"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/logger/event"
"github.com/davecgh/go-spew/spew"
"gopkg.in/mgo.v2/bson"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
JOB_STAT_INIT = "init" // inital state
JOB_STAT_QUEUING = "queuing" // transition from "init" to "queued"
JOB_STAT_QUEUED = "queued" // all tasks have been added to taskmap
JOB_STAT_INPROGRESS = "in-progress" // a first task went into state 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_QUEUING, JOB_STAT_QUEUED, JOB_STAT_INPROGRESS}
var JOB_STATS_REGISTERED = []string{JOB_STAT_QUEUING, JOB_STAT_QUEUED, JOB_STAT_INPROGRESS, JOB_STAT_SUSPEND}
var JOB_STATS_TO_RECOVER = []string{JOB_STAT_INIT, JOB_STAT_QUEUING, 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 Job struct {
JobRaw `bson:",inline"`
Tasks []*Task `bson:"tasks" json:"tasks"`
}
type JobRaw struct {
RWMutex
Id string `bson:"id" json:"id"` // uuid
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
IsCWL bool `bson:"is_cwl" json:"is_cwl`
CwlVersion cwl.CWLVersion `bson:"cwl_version" json:"cwl_version"`
CWL_objects interface{} `bson:"cwl_objects" json:"cwl_objects`
CWL_job_input interface{} `bson:"cwl_job_input" json:"cwl_job_input` // has to be an array for mongo (id as key would not work)
CWL_collection *cwl.CWL_collection `bson:"-" json:"-" yaml:"-" mapstructure:"-"`
CWL_workflow *cwl.Workflow `bson:"-" json:"-" yaml:"-" mapstructure:"-"`
WorkflowInstances []interface{} `bson:"workflow_instances" json:"workflow_instances" yaml:"workflow_instances" mapstructure:"workflow_instances"`
WorkflowInstancesMap map[string]*WorkflowInstance `bson:"-" json:"-" yaml:"-" mapstructure:"-"`
}
func (job *JobRaw) GetId(do_read_lock bool) (id string, err error) {
if do_read_lock {
read_lock, xerr := job.RLockNamed("String")
if xerr != nil {
err = xerr
return
}
defer job.RUnlockNamed(read_lock)
}
id = job.Id
return
}
func (job *Job) AddWorkflowInstance(id string, inputs cwl.Job_document, remain_tasks int) (err error) {
err = job.LockNamed("AddWorkflowInstance")
if err != nil {
return
}
defer job.Unlock()
if id == "" {
id = "::main::"
}
wi := &WorkflowInstance{Id: id, Inputs: inputs, RemainTasks: remain_tasks}
if job.WorkflowInstances == nil {
err = fmt.Errorf("(AddWorkflowInstance) array does not exist")
return
}
err = dbPushJobWorkflowInstance(job.Id, wi)
if err != nil {
err = fmt.Errorf("(AddWorkflowInstance) dbPushJobWorkflowInstance returned: %s", err.Error())
return
}
job.WorkflowInstances = append(job.WorkflowInstances, wi)
job.WorkflowInstancesMap[id] = wi
return
}
func (job *Job) GetWorkflowInstanceIndex(id string, do_read_lock bool) (index int, err error) {
var wi_int interface{}
for index, wi_int = range job.WorkflowInstances {
var element_wi WorkflowInstance
element_wi, err = NewWorkflowInstanceFromInterface(wi_int)
if err != nil {
err = fmt.Errorf("(GetWorkflowInstance) object was not a WorkflowInstance !? %s", err.Error())
return
}
if element_wi.Id == id {
//job.WorkflowInstancesMap[id] = &element_wi
//wi = &element_wi
return
}
}
err = fmt.Errorf("(GetWorkflowInstance) WorkflowInstance %s not found", id)
return
}
func (job *Job) GetWorkflowInstance(id string, do_read_lock bool) (wi *WorkflowInstance, err error) {
if do_read_lock {
read_lock, xerr := job.RLockNamed("GetWorkflowInstance")
if xerr != nil {
err = xerr
return
}
defer job.RUnlockNamed(read_lock)
}
if id == "" {
id = "::main::"
}
var ok bool
wi, ok = job.WorkflowInstancesMap[id]
if !ok {
// for _, wi_int := range job.WorkflowInstances {
// var element_wi WorkflowInstance
// element_wi, err = NewWorkflowInstanceFromInterface(wi_int)
// if err != nil {
// err = fmt.Errorf("(GetWorkflowInstance) object was not a WorkflowInstance !? %s", err.Error())
// return
// }
// if element_wi.Id == id {
// job.WorkflowInstancesMap[id] = &element_wi
// wi = &element_wi
// return
// }
//
// }
err = fmt.Errorf("(GetWorkflowInstance) id \"%s\" not found", id)
return
}
return
}
func (job *Job) Set_WorkflowInstance_Outputs(id string, outputs cwl.Job_document) (err error) {
err = job.LockNamed("Set_WorkflowInstance_Outputs")
if err != nil {
return
}
defer job.Unlock()
err = dbUpdateJobWorkflow_instancesFieldOutputs(job.Id, id, outputs)
if err != nil {
err = fmt.Errorf("(Set_WorkflowInstance_Outputs) dbUpdateJobWorkflow_instancesFieldOutputs returned: %s", err.Error())
return
}
var index int
index, err = job.GetWorkflowInstanceIndex(id, false)
if err != nil {
err = fmt.Errorf("(Set_WorkflowInstance_Outputs) GetWorkflowInstanceIndex returned: %s", err.Error())
return
}
var workflow_instance WorkflowInstance
workflow_instance_if := job.WorkflowInstances[index]
workflow_instance, err = NewWorkflowInstanceFromInterface(workflow_instance_if)
if err != nil {
err = fmt.Errorf("(Set_WorkflowInstance_Outputs) NewWorkflowInstanceFromInterface returned: %s", err.Error())
return
}
workflow_instance.Outputs = outputs
job.WorkflowInstances[index] = workflow_instance
job.WorkflowInstancesMap[id] = &workflow_instance
return
}
func (job *Job) Decrease_WorkflowInstance_RemainTasks(id string) (remain_tasks int, err error) {
err = job.LockNamed("Decrease_WorkflowInstance_RemainTasks")
if err != nil {
return
}
defer job.Unlock()
var workflow_instance *WorkflowInstance
workflow_instance, _ = job.GetWorkflowInstance(id, false)
remain_tasks = workflow_instance.RemainTasks - 1
err = dbUpdateJobWorkflow_instancesFieldInt(job.Id, id, "remaintasks", remain_tasks)
if err != nil {
err = fmt.Errorf("(Set_WorkflowInstance_DecreaseRemainTasks) dbUpdateJobWorkflow_instancesFieldInt returned: %s", err.Error())
return
}
workflow_instance.RemainTasks = remain_tasks
return
}
// 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]interface{} `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()
task.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 = fmt.Errorf("(job.Init) task.Init returned: %s", xerr.Error())
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 {
var task_str string
task_str, err = task.String()
if err != nil {
return
}
err = fmt.Errorf("(job.Init) invalid inputs: task %s contains multiple inputs with filename=%s", task_str, io.FileName)
return
}
inputFileNames[io.FileName] = true
}
}
//var workflow *cwl.Workflow
if job.IsCWL {
collection := cwl.NewCWL_collection()
object_array, xerr := cwl.NewCWL_object_array(job.CWL_objects)
if xerr != nil {
err = fmt.Errorf("(job.Init) cannot type assert CWL_objects: %s", xerr.Error())
return
}
err = cwl.Add_to_collection(&collection, object_array)
if err != nil {
fmt.Errorf("(job.Init) Add_to_collection returned: %s", err.Error())
return
}
if job.WorkflowInstances != nil {
if len(job.WorkflowInstances) != len(job.WorkflowInstancesMap) {
if job.WorkflowInstancesMap == nil {
job.WorkflowInstancesMap = make(map[string]*WorkflowInstance)
}
for i, _ := range job.WorkflowInstances {
wi_int := job.WorkflowInstances[i]
var wi WorkflowInstance
wi, err = NewWorkflowInstanceFromInterface(wi_int)
if err != nil {
err = fmt.Errorf("(job.Init) object is not a WorkflowInstance: %s", err.Error())
return
}
job.WorkflowInstancesMap[wi.Id] = &wi
}
changed = true
}
}
var main_input *WorkflowInstance
main_input, err = job.GetWorkflowInstance("::main::", true) //job.WorkflowInstancesMap["#main"]
if err != nil {
err = fmt.Errorf("(job.Init) workflow #main not found %s", err.Error())
return
}
//main_input, xerr := cwl.NewJob_documentFromNamedTypes(job.CWL_job_input)
if xerr != nil {
//fmt.Println("\n\njob.CWL_job_input:\n")
//spew.Dump(job.CWL_job_input)
err = fmt.Errorf("(job.Init) cannot create main_input: %s", xerr.Error())
return
}
main_input_map := main_input.Inputs.GetMap()
collection.Job_input_map = &main_input_map
cwl_workflow, ok := collection.Workflows["#main"]
if !ok {
err = fmt.Errorf("(job.Init) Workflow \"main\" not found")
return
}
job.CWL_collection = &collection
job.CWL_workflow = cwl_workflow
}
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 Deserialize_b64(encoding string, target interface{}) (err error) {
byte_array, err := b64.StdEncoding.DecodeString(encoding)
if err != nil {
err = fmt.Errorf("(Deserialize_b64) DecodeString error: %s", err.Error())
return
}
err = json.Unmarshal(byte_array, target)
if err != nil {
return
}
return
}
func (job *Job) Save() (err error) {
if job.Id == "" {
err = fmt.Errorf("(job.Save()) job id empty")
return
}
logger.Debug(1, "(job.Save()) saving job: %s", job.Id)
job.UpdateTime = time.Now()
err = job.SaveToDisk()
if err != nil {
err = fmt.Errorf("(job.Save()) SaveToDisk failed: %s", err.Error())
return
}
logger.Debug(1, "(job.Save()) dbUpsert next: %s", job.Id)
//spew.Dump(job)
err = dbUpsert(job)
if err != nil {
err = fmt.Errorf("(job.Save()) dbUpsert failed (job_id=%s) error=%s", job.Id, err.Error())
return
}
logger.Debug(1, "(job.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)
}
func (job *Job) AddTask(task *Task) (err error) {
err = job.LockNamed("AddTask")
if err != nil {
return
}
defer job.Unlock()
id := job.Id
err = dbPushJobTask(id, task)
if err != nil {
return
}
job.Tasks = append(job.Tasks, task)
return
}
//---Field update functions
func (job *Job) SetState(newState string, oldstates []string) (err error) {
err = job.LockNamed("SetState")
if err != nil {
return
}
defer job.Unlock()
job_state := job.State
if job_state == newState {
return
}
if len(oldstates) > 0 {
matched := false
for _, oldstate := range oldstates {
if oldstate == job_state {
matched = true
break
}
}
if !matched {
oldstates_str := strings.Join(oldstates, ",")
err = fmt.Errorf("(UpdateJobState) old state %s does not match one of the required ones (required: %s)", job_state, oldstates_str)
return
}
}
err = dbUpdateJobFieldString(job.Id, "state", newState)
if err != nil {
return
}
job.State = newState
// set time if completed
switch newState {
case JOB_STAT_COMPLETED:
newTime := time.Now()
err = dbUpdateJobFieldTime(job.Id, "info.completedtime", newTime)
if err != nil {
return
}
job.Info.CompletedTime = newTime
case JOB_STAT_INPROGRESS:
time_now := time.Now()
jobid := job.Id
err = job.Info.SetStartedTime(jobid, time_now)
if err != nil {
return
}
}
// 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()
spew.Dump(newError)
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, err error) {
for _, task := range job.Tasks {
var task_str string
task_str, err = task.String()
if err != nil {
return
}
if taskid == task_str {
env = task.Cmd.Environ.Private
return
}
}
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 {
var tl *TaskLog
tl, err = task.GetTaskLogs()
if err != nil {
return
}
jlog.Tasks = append(jlog.Tasks, tl)
}
return
}
func ReloadFromDisk(path string) (err error) {
id := filepath.Base(path)
jobbson, err := ioutil.ReadFile(path + "/" + id + ".bson")
if err != nil {