/
barnacle_status.py
executable file
·64 lines (50 loc) · 1.56 KB
/
barnacle_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
#!/usr/bin/env python
import subprocess
import sys
import time
import re
from io import StringIO
def parse_qstat(lines):
q_dict = {}
p = re.compile('^ +(.+?) = (.+)')
for line in lines:
if line.startswith(' '):
entry = line.rstrip()
m = p.match(entry.rstrip())
q_dict[m.groups()[0].lower()] = m.groups()[1]
elif line.startswith('\t'):
entry += line.strip()
m = p.match(entry.rstrip())
q_dict[m.groups()[0].lower()] = m.groups()[1]
return(q_dict)
def get_status(q_dict):
try:
if q_dict['job_state'] == 'R':
status = "running"
elif q_dict['job_state'] == 'Q':
status = "running"
elif q_dict['job_state'] == 'F' and q_dict['exit_status'] == '0':
status = "success"
elif q_dict['job_state'] == 'F' and q_dict['exit_status'] != '0':
status = "failed"
else:
status = "failed"
except KeyError:
status = "failed"
return(status)
def main():
jobid = sys.argv[1]
TRY_TIMES = 3
for i in range(TRY_TIMES):
cmd = "qstat -f {} -x".format(jobid)
p1 = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
out, err = p1.communicate()
if err.decode().startswith('qstat: Unknown Job Id Error'):
time.sleep(5)
continue
q_dict = parse_qstat(StringIO(out.decode()))
status = get_status(q_dict)
print(status)
if __name__ == '__main__':
main()