-
Notifications
You must be signed in to change notification settings - Fork 0
/
slurm-status.py
executable file
·167 lines (137 loc) · 4.1 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
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
#!/usr/bin/env python3
import re
import subprocess as sp
import shlex
import sys
import time
import logging
import argparse
logger = logging.getLogger("__name__")
parser = argparse.ArgumentParser(description='Get the status of some slurm job.')
parser.add_argument(
'--status_attempts',
action='store',
dest="status_attempts",
type=int,
default=20,
help='number of attenpts to obtain cluster status'
)
parser.add_argument(
'--cluster',
action='store',
dest="cluster",
default="",
help='cluster name'
)
parser.add_argument(
'--debug',
action='store_true',
dest="debug",
help='Change log level to debug'
)
parser.add_argument(
'jobid',
action='store',
help='Slurm job id'
)
args = parser.parse_args()
if args.debug is True:
loglevel = logging.DEBUG
else:
loglevel = logging.WARNING
JOBID = args.jobid
STATUS_ATTEMPTS = args.status_attempts
if args.cluster != "":
cluster = f"--cluster={args.cluster}"
else:
cluster = ""
# setup logging
logging.basicConfig(
level=loglevel,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("slurm-status.log"),
# logging.StreamHandler()
]
)
def run_scontrol(cluster, jobid):
sctrl_res = sp.check_output(
shlex.split(f"scontrol {cluster} -o show job {jobid}")
)
m = re.search(r"JobState=(\w+)", sctrl_res.decode())
res = {jobid: m.group(1)}
if res[jobid] == "PREEMPTED":
m = re.search(r"Requeue=(\w+)", sctrl_res.decode())
requeueable = m.group(1)
if requeueable == "1":
res[jobid] = "REQUEUED"
return res
def run_sacct(cluster, jobid):
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")}
return res
def get_state(jobid, status_attempts):
for i in range(status_attempts):
try:
res = run_sacct(cluster, jobid)
if res[jobid] == "PREEMPTED":
# run scontrol to check whether the job will be requeued
res = run_scontrol(cluster, jobid)
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:
res = run_scontrol(cluster, jobid)
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]
return status
def decide_return_value(jobid, status_attempts, preempt_retry=True, default="running"):
status = get_state(jobid, status_attempts)
logger.info("%s: '%s'", jobid, status)
if status.startswith("CANCELLED by") or status in [
"BOOT_FAIL",
"OUT_OF_MEMORY",
"DEADLINE",
"FAILED",
"NODE_FAIL",
"TIMEOUT",
"PREEMPTED",
]:
logger.warning(f"Job '{jobid}' failed: '{status}'")
return "failed"
# elif status == "PREEMPTED":
# if preempt_retry:
# # wait for 10 seconds and then try again to see if it got requeued
# from time import sleep
# sleep(10)
# return decide_return_value(jobid, status_attempts, preempt_retry=False)
# else:
# logger.warning(f"Job '{jobid}' failed after preemption: '{status}'")
# return "failed"
elif status in [
"PENDING",
"RUNNING",
"REQUEUED",
"COMPLETING",
"SUSPENDED", # Unclear whether SUSPENDED should be treated as running or failed
]:
return "running"
elif status == "COMPLETED":
return "success"
else:
logger.warning(f"Unkonwn job state of job '{jobid}': '{status}'")
return default
status = decide_return_value(JOBID, STATUS_ATTEMPTS)
print(status)