-
Notifications
You must be signed in to change notification settings - Fork 8
/
job_help.go
182 lines (169 loc) · 4.66 KB
/
job_help.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
package wfl
import (
"context"
"errors"
"github.com/dgruber/drmaa2interface"
"github.com/mitchellh/copystructure"
)
// mergeJobTemplateWithDefaultTemplate adds requests from _def_ into _req_
// if specified in req
//
// Note there is no "unset" convention yet for the job template. It will
// only take actually implemented (in drmaa2os job tracker) settings into
// account.
func mergeJobTemplateWithDefaultTemplate(req, def drmaa2interface.JobTemplate) drmaa2interface.JobTemplate {
if req.JobCategory == "" {
req.JobCategory = def.JobCategory
}
if req.InputPath == "" {
req.InputPath = def.InputPath
}
if req.OutputPath == "" {
req.OutputPath = def.OutputPath
}
if req.ErrorPath == "" {
req.ErrorPath = def.ErrorPath
}
if req.AccountingID == "" {
req.AccountingID = def.AccountingID
}
if req.JobName == "" {
req.JobName = def.JobName
}
if req.WorkingDirectory == "" {
req.WorkingDirectory = def.WorkingDirectory
}
// replaces destination machines
if req.CandidateMachines == nil && def.CandidateMachines != nil {
if cm, err := copystructure.Copy(def.CandidateMachines); err == nil {
req.CandidateMachines = cm.([]string)
}
}
// replace extensions
if req.ExtensionList == nil && def.ExtensionList != nil {
if el, err := copystructure.Copy(def.ExtensionList); err == nil {
req.ExtensionList = el.(map[string]string)
}
}
// join files to stage
req.StageInFiles = mergeStringMap(req.StageInFiles, def.StageInFiles)
// join enviroment variables
req.JobEnvironment = mergeStringMap(req.JobEnvironment, def.JobEnvironment)
// TODO implement more when required
return req
}
func mergeStringMap(dst, src map[string]string) map[string]string {
if src != nil {
if dst == nil {
dst = make(map[string]string, len(src))
}
for k, v := range src {
if dst[k] == "" {
dst[k] = v
}
}
}
return dst
}
func waitForJobEndAndState(j *Job) drmaa2interface.JobState {
job, jobArray, err := j.jobCheck()
if err != nil {
return drmaa2interface.Undetermined
}
if job != nil {
lastError := job.WaitTerminated(drmaa2interface.InfiniteTime)
if lastError != nil {
return drmaa2interface.Undetermined
}
return job.GetState()
}
return jobArrayState(jobArray, true)
}
func jobArrayState(jobArray drmaa2interface.ArrayJob, wait bool) drmaa2interface.JobState {
// it is a job array - waiting for each single task
// if one of the tasks failed - the whole job array failed
// if one of the tasks is undetermined and the rest is done, the array
// is undetermined.
jobArrayState := drmaa2interface.Done
for _, job := range jobArray.GetJobs() {
if wait {
lastError := job.WaitTerminated(drmaa2interface.InfiniteTime)
if lastError != nil {
return drmaa2interface.Undetermined
}
}
switch job.GetState() {
case drmaa2interface.Done:
continue
case drmaa2interface.Failed:
// overwrites all
jobArrayState = drmaa2interface.Failed
case drmaa2interface.Undetermined:
// overwrites done
if jobArrayState == drmaa2interface.Done {
jobArrayState = drmaa2interface.Undetermined
}
}
}
return jobArrayState
}
func waitArrayJobTerminated(jobArray drmaa2interface.ArrayJob) error {
var lastErr error
for _, job := range jobArray.GetJobs() {
err := job.WaitTerminated(drmaa2interface.InfiniteTime)
if err != nil {
lastErr = err
}
}
return lastErr
}
func (j *Job) lastJob() *task {
if len(j.tasklist) == 0 {
return nil
}
return j.tasklist[len(j.tasklist)-1]
}
func (j *Job) jobCheck() (drmaa2interface.Job, drmaa2interface.ArrayJob, error) {
task := j.lastJob()
if task == nil {
j.errorf(j.ctx, "jobCheck(): task is nil")
return nil, nil, errors.New("job task not available")
} else if task.job == nil && task.jobArray == nil {
j.errorf(j.ctx, "jobCheck(): task has no drmaa2 job")
return nil, nil, errors.New("job not available")
}
return task.job, task.jobArray, nil
}
func (j *Job) checkCtx() error {
if j.wfl == nil {
return errors.New("no workflow defined")
}
if j.wfl.ctx == nil {
return errors.New("no context defined")
}
return nil
}
func (j *Job) begin(ctx context.Context, f string) {
if j == nil || j.wfl == nil || j.wfl.log == nil {
return
}
j.wfl.log.Begin(ctx, f)
}
func (j *Job) infof(ctx context.Context, s string, args ...interface{}) {
if j == nil || j.wfl == nil || j.wfl.log == nil {
return
}
j.wfl.log.Infof(ctx, s, args...)
}
func (j *Job) warningf(ctx context.Context, s string, args ...interface{}) {
if j == nil || j.wfl == nil || j.wfl.log == nil {
return
}
j.wfl.log.Warningf(ctx, s, args...)
}
func (j *Job) errorf(ctx context.Context, s string, args ...interface{}) {
if j == nil || j.wfl == nil || j.wfl.log == nil {
return
}
j.wfl.log.Errorf(ctx, s, args...)
}