-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjasmin_job_monitor.py
More file actions
269 lines (211 loc) · 8.73 KB
/
jasmin_job_monitor.py
File metadata and controls
269 lines (211 loc) · 8.73 KB
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
import os
import subprocess
import sys
from status_codes import StatusCodes
class JasminJobMonitor:
@staticmethod
def main():
pm_request = ""
for i in range(1, len(sys.argv)):
pm_request += sys.argv[i]
pm_request += " "
# remove last blank
pm_request = pm_request[:-1]
monitor = JasminJobMonitor()
return monitor.run(pm_request)
def run(self, pm_request):
try:
scheduler_interface = self._create_scheduler_interface()
watch_dict = self._parse_watch_dict(pm_request)
process_output = scheduler_interface.call_job_status()
job_status_dict = scheduler_interface.parse_jobs_call(process_output)
resolved, check_log = self._resolve_jobs(watch_dict, job_status_dict)
log_resolved = scheduler_interface.resolve_status_from_log(check_log)
resolved.update(log_resolved)
self._format_and_write(resolved, sys.stdout)
return 0
except (RuntimeError, ValueError) as e:
sys.stderr.write(str(e))
return 1
@staticmethod
def _create_scheduler_interface():
if "SCHEDULER" in os.environ:
scheduler_name = os.environ["SCHEDULER"]
if scheduler_name == "LSF":
return LSFInterface()
elif scheduler_name == "SLURM":
return SLURMInterface()
elif scheduler_name == "SLURM_2":
return SLURMInterface()
else:
raise ValueError("Environment variable 'SCHEDULER' invalid" + scheduler_name)
else:
raise ValueError("Environment variable 'SCHEDULER' is not set")
@staticmethod
def _parse_watch_dict(pm_request):
watch_dict = {}
tokens = pm_request.split(" ")
for token in tokens:
if len(token) == 0:
continue
idx = token.find("_")
if idx < 0:
raise ValueError("Illegal ID encoding: " + token)
job_id = token[:idx]
jobname = token[idx + 1:]
watch_dict.update({job_id: jobname})
return watch_dict
@staticmethod
def _resolve_jobs(watch_dict, job_status_dict):
resolved_dict = {}
check_log_dict = {}
for key, value in watch_dict.items():
jobname = value
if key in job_status_dict:
status = job_status_dict[key]
resolved_dict.update({key: [jobname, status, ""]})
pass
else:
check_log_dict.update({key: jobname})
return resolved_dict, check_log_dict
@staticmethod
def _get_log_dir():
if "PM_LOG_DIR" in os.environ:
log_dir = os.environ["PM_LOG_DIR"]
if not os.path.isdir(log_dir):
raise ValueError("log directory does not exist: " + log_dir)
return log_dir
else:
raise RuntimeError("Missing environment variable 'PM_LOG_DIR'")
@staticmethod
def _format_and_write(results, stream):
if (len(results) == 0):
stream.flush()
return
key_list = list(results.keys())
num_items = len(key_list)
for i in range(0, num_items):
key = key_list[i]
result = results[key]
stream.write(key + "_" + result[0] + ",")
stream.write(result[1].value + ",")
stream.write(result[2])
stream.write("\n")
stream.flush()
class LSFInterface:
def call_job_status(self):
completed_process = subprocess.run(["bjobs"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if completed_process.returncode != 0:
sys.stderr.write(completed_process.stderr.decode("utf-8"))
sys.stderr.flush()
raise RuntimeError("Failed to request job status")
process_output = completed_process.stdout.decode("utf-8")
return process_output
def parse_jobs_call(self, message):
job_status_dict = {}
lines = message.split("\n")
for i in range(1, len(lines)):
if len(lines[i]) == 0:
continue
id_status = self._extract_id_and_status(lines[i])
job_status_dict.update(id_status)
return job_status_dict
def _extract_id_and_status(self, line):
tokens = line.split()
if len(tokens) < 8:
raise ValueError("unable to handle 'bjobs' result: " + line)
status_code = self._status_to_enum(tokens[2])
return {tokens[0]: status_code}
@staticmethod
def _status_to_enum(status):
if "RUN" == status:
return StatusCodes.RUNNING
elif status in ["PEND", "PROV", "WAIT"]:
return StatusCodes.SCHEDULED
elif "DONE" == status:
return StatusCodes.DONE
elif "EXIT" == status:
return StatusCodes.FAILED
elif "UNKWN" == status:
return StatusCodes.UNKNOWN
elif status in ["PSUSP", "USUSP", "SSUSP", "ZOMBI"]:
return StatusCodes.DROPPED
raise ValueError("unsupported status code: " + status)
def resolve_status_from_log(self, check_log_dict):
resolved_dict = {}
log_dir = JasminJobMonitor._get_log_dir()
for key, value in check_log_dict.items():
logfile = os.path.join(log_dir, value + ".out")
if not os.path.isfile(logfile):
resolved_dict.update({key: [value, StatusCodes.DROPPED, "log file for job does not exist: " + logfile]})
continue
with open(logfile) as f:
log_content = f.read()
if "Successfully completed." in log_content:
resolved_dict.update({key: [value, StatusCodes.DONE, ""]})
else:
resolved_dict.update({key: [value, StatusCodes.FAILED, "Check log file for details: " + logfile]})
return resolved_dict
class SLURMInterface:
user_name = None
scheduler_name = None
def __init__(self):
if "MMS_USER" in os.environ:
self.user_name = os.environ["MMS_USER"]
if "SCHEDULER" in os.environ:
self.scheduler_name = os.environ["SCHEDULER"]
else:
raise RuntimeError("Missing environment variable 'MMS_USER'")
def call_job_status(self):
completed_process = subprocess.run(['squeue', '--users=' + self.user_name, '--states=all'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if completed_process.returncode != 0:
sys.stderr.write(completed_process.stderr.decode("utf-8"))
sys.stderr.flush()
raise RuntimeError("Failed to request job status")
process_output = completed_process.stdout.decode("utf-8")
return process_output
def parse_jobs_call(self, message):
job_status_dict = {}
lines = message.split("\n")
for i in range(1, len(lines)):
if len(lines[i]) == 0:
continue
id_status = self._extract_id_and_status(lines[i])
job_status_dict.update(id_status)
return job_status_dict
def _extract_id_and_status(self, line):
tokens = line.split()
# seven tokens are used for canceled jobs, 8 is standard tb 2022-09-21
if len(tokens) < 7:
raise ValueError("unable to handle 'squeue' result: " + line)
if self.scheduler_name == "SLURM_2":
status_token = tokens[7]
else:
status_token = tokens[4]
status_code = self._status_to_enum(status_token)
return {tokens[0]: status_code}
@staticmethod
def _status_to_enum(status):
if status in ["R", "CG", "SO"]:
return StatusCodes.RUNNING
elif status in ["CF", "PD", "RD", "RF", "RH", "RQ", "RS", "RV"]:
return StatusCodes.SCHEDULED
elif status in ["CD", "SE"]:
return StatusCodes.DONE
elif status in ["BF", "DL", "F", "NF", "OOM", "TO"]:
return StatusCodes.FAILED
elif status in ["CA", "PR", "SI", "ST", "S"]:
return StatusCodes.DROPPED
raise ValueError("unsupported status code: " + status)
def resolve_status_from_log(self, check_log_dict):
resolved_dict = {}
log_dir = JasminJobMonitor._get_log_dir()
for key, value in check_log_dict.items():
logfile = os.path.join(log_dir, value + ".out")
if not os.path.isfile(logfile):
resolved_dict.update({key: [value, StatusCodes.DROPPED, "log file for job does not exist: " + logfile]})
else:
resolved_dict.update({key: [value, StatusCodes.DONE, ""]})
return resolved_dict
if __name__ == "__main__":
sys.exit(JasminJobMonitor.main())