forked from percyfal/slurm
-
Notifications
You must be signed in to change notification settings - Fork 44
/
slurm-status.py
executable file
·106 lines (93 loc) · 3.16 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
#!/usr/bin/env python3
import json
import os
import re
import requests
import subprocess as sp
import shlex
import sys
import time
import logging
from CookieCutter import CookieCutter
logger = logging.getLogger(__name__)
STATUS_ATTEMPTS = 20
SIDECAR_VARS = os.environ.get("SNAKEMAKE_CLUSTER_SIDECAR_VARS", None)
DEBUG = bool(int(os.environ.get("SNAKEMAKE_SLURM_DEBUG", "0")))
if DEBUG:
logging.basicConfig(level=logging.DEBUG)
logger.setLevel(logging.DEBUG)
def get_status_direct(jobid):
"""Get status directly from sacct/scontrol"""
cluster = CookieCutter.get_cluster_option()
for i in range(STATUS_ATTEMPTS):
try:
sacct_res = sp.check_output(shlex.split(f"sacct {cluster} -P -b -j {jobid} -n"))
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:
logger.error(e)
pass
# Try getting job with scontrol instead in case sacct is misconfigured
try:
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)}
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)
return res[jobid] or ""
def get_status_sidecar(jobid):
"""Get status from cluster sidecar"""
sidecar_vars = json.loads(SIDECAR_VARS)
url = "http://localhost:%d/job/status/%s" % (sidecar_vars["server_port"], jobid)
headers = {"Authorization": "Bearer %s" % sidecar_vars["server_secret"]}
try:
resp = requests.get(url, headers=headers)
if resp.status_code == 404:
return "" # not found yet
logger.debug("sidecar returned: %s" % resp.json())
resp.raise_for_status()
return resp.json().get("status") or ""
except requests.exceptions.ConnectionError as e:
logger.warning("slurm-status.py: could not query side car: %s", e)
logger.info("slurm-status.py: falling back to direct query")
return get_status_direct(jobid)
jobid = sys.argv[1]
if SIDECAR_VARS:
logger.debug("slurm-status.py: querying sidecar")
status = get_status_sidecar(jobid)
else:
logger.debug("slurm-status.py: direct query")
status = get_status_direct(jobid)
logger.debug("job status: %s", repr(status))
if status == "BOOT_FAIL":
print("failed")
elif status == "OUT_OF_MEMORY":
print("failed")
elif status.startswith("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")
elif status == "SUSPENDED":
print("running")
else:
print("running")