-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
277 lines (228 loc) · 5.37 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
package fuq
import (
"encoding/json"
"fmt"
"io"
"sort"
"strconv"
)
type JobRequest struct {
NumProc int `json:"nproc"`
}
type JobId uint64
type JobDescription struct {
JobId JobId `json:"job_id"`
Name string `json:"name"`
NumTasks int `json:"num_tasks"`
WorkingDir string `json:"working_dir"`
LoggingDir string `json:"logging_dir"`
Command string `json:"command"`
Status JobStatus `json:"status"`
}
func (jd *JobDescription) ExpandPaths(pv *PathVars) error {
var err error
if jd.WorkingDir, err = pv.ExpandPath(jd.WorkingDir); err != nil {
return err
}
if jd.LoggingDir, err = pv.ExpandPath(jd.LoggingDir); err != nil {
return err
}
return nil
}
func (jd *JobDescription) CheckPaths() error {
if jd.WorkingDir == "" {
return fmt.Errorf("invalid working directory '%s'", jd.WorkingDir)
}
if jd.LoggingDir == "" {
return fmt.Errorf("invalid logging directory '%s'", jd.LoggingDir)
}
return nil
}
type JobTaskStatus struct {
Description JobDescription
TasksFinished int // Number of tasks that are finished.
TasksPending int // Number of tasks not yet running.
TasksRunning []int // Tasks currently currently running.
TasksWithErrors []int
}
func ReadJobFile(in io.Reader) (JobDescription, error) {
job := JobDescription{}
err := ParseKVFile(in, func(key, value string) error {
switch key {
case "name":
job.Name = value
case "num_tasks":
p, err := strconv.ParseInt(value, 10, 32)
if err != nil || p <= 0 {
return fmt.Errorf("invalid task count: %s", value)
}
job.NumTasks = int(p)
case "working_dir":
job.WorkingDir = value
case "log_dir":
job.LoggingDir = value
case "command":
job.Command = value
default:
return fmt.Errorf("unknown directive '%s'", key)
}
return nil
})
if err != nil {
return JobDescription{}, fmt.Errorf("error reading job description: %v", err)
}
return job, nil
}
type NewJobResponse struct {
JobId JobId `json:"job_id"`
}
type JobStateChangeResponse struct {
JobId JobId `json:"job_id"`
PrevStatus JobStatus `json:"prev_status"`
NewStatus JobStatus `json:"new_status"`
}
type ClientJobListReq struct {
Name string `json:"name"`
Status string `json:"status"`
}
type ClientStateChangeReq struct {
JobIds []JobId `json:"job_ids"`
Action string
}
type ClientNodeListReq struct {
CookieList bool `json:"cookie_list"`
JobList bool `json:"job_list"`
}
type ClientNodeShutdownReq struct {
UniqNames []string `json:"uniq_names"`
}
type JobStatus int
const (
Waiting JobStatus = iota
Running
Finished
Paused
Cancelled
)
func (js JobStatus) MarshalJSON() ([]byte, error) {
s := ""
switch js {
case Waiting:
s = "waiting"
case Running:
s = "running"
case Finished:
s = "finished"
case Paused:
s = "paused"
case Cancelled:
s = "cancelled"
default:
return nil, fmt.Errorf("unknown status %d", int(js))
}
return json.Marshal(s)
}
func (js *JobStatus) UnmarshalJSON(b []byte) error {
var s string
n := len(b)
if n >= 2 && b[0] == '"' && b[n-1] == '"' {
s = string(b[1 : n-1])
} else {
s = string(b)
}
jsp, err := ParseJobStatus(s)
if err != nil {
return err
}
*js = jsp
return nil
}
func ParseJobStatus(s string) (JobStatus, error) {
switch s {
case "waiting":
return Waiting, nil
case "running":
return Running, nil
case "finished":
return Finished, nil
case "paused":
return Paused, nil
case "cancelled":
return Cancelled, nil
default:
return JobStatus(0), fmt.Errorf("unknown status '%s'", s)
}
}
func (js JobStatus) String() string {
switch js {
case Waiting:
return "waiting"
case Running:
return "running"
case Finished:
return "finished"
case Paused:
return "paused"
case Cancelled:
return "cancelled"
default:
return fmt.Sprintf("unknown_status_%d", int(js))
}
}
type Task struct {
Task int `json:"task"`
JobDescription
}
type TaskPair struct {
JobId JobId `json:"job_id"`
Task int `json:"task"`
}
type JobTaskData struct {
JobId
Pending []int
Running []int
Finished []int
Errors []int
ErrorMessages []string
}
type JobRunning struct {
JobId JobId
Task int
Node string
UniqName string
Pid int
}
func (tasks *JobTaskData) Update(update JobStatusUpdate) error {
ind := sort.SearchInts(tasks.Running, update.Task)
if ind >= len(tasks.Running) || tasks.Running[ind] != update.Task {
return fmt.Errorf("task %d is not in the running list", update.Task)
}
copy(tasks.Running[ind:], tasks.Running[ind+1:])
tasks.Running = tasks.Running[:len(tasks.Running)-1]
if update.Success {
tasks.Finished = append(tasks.Finished, update.Task)
} else {
tasks.Errors = append(tasks.Errors, update.Task)
tasks.ErrorMessages = append(tasks.ErrorMessages, update.Status)
}
return nil
}
type JobStatusUpdate struct {
JobId JobId `json:"job_id"`
Task int `json:"task"`
Success bool `json:"success"`
Status string `json:"status"`
NewJob *JobRequest `json:"newjob"`
}
func MakeJobStatusUpdate(desc JobDescription, tasks JobTaskData) JobTaskStatus {
status := JobTaskStatus{
Description: desc,
TasksFinished: len(tasks.Finished),
TasksPending: len(tasks.Pending),
TasksRunning: make([]int, len(tasks.Running)),
TasksWithErrors: make([]int, len(tasks.Errors)),
}
copy(status.TasksRunning, tasks.Running)
copy(status.TasksWithErrors, tasks.Errors)
return status
}