-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslurm-status.py
executable file
·61 lines (56 loc) · 1.62 KB
/
slurm-status.py
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
#!/usr/bin/env python3
import re
import subprocess as sp
import shlex
import sys
import time
import logging
logger = logging.getLogger("__name__")
STATUS_ATTEMPTS = 20
jobid = sys.argv[1]
for i in range(STATUS_ATTEMPTS):
try:
sacct_res = sp.check_output(shlex.split("sacct -P -b -j {} -n".format(jobid)))
res = {x.split("|")[0]: x.split("|")[1] for x in sacct_res.decode().strip().split("\n")}
break
except sp.CalledProcessError as e:
logger.error("sacct process error")
logger.error(e)
except IndexError as e:
pass
# Try getting job with scontrol instead in case sacct is misconfigured
try:
sctrl_res = sp.check_output(shlex.split("scontrol -o show job {}".format(jobid)))
m = re.search("JobState=(\w+)", sctrl_res.decode())
res = {jobid: m.group(1)}
break
except sp.CalledProcessError as e:
logger.error("scontrol process error")
logger.error(e)
if i >= STATUS_ATTEMPTS - 1:
print("failed")
exit(0)
else:
time.sleep(1)
status = res[jobid]
if (status == "BOOT_FAIL"):
print("failed")
elif (status == "CANCELLED"):
print("failed")
elif (status == "COMPLETED"):
print("success")
elif (status == "DEADLINE"):
print("failed")
elif (status == "FAILED"):
print("failed")
elif (status == "NODE_FAIL"):
print("failed")
elif (status == "PREEMPTED"):
print("failed")
elif (status == "TIMEOUT"):
print("failed")
# Unclear whether SUSPENDED should be treated as running or failed
elif (status == "SUSPENDED"):
print("failed")
else:
print("running")