-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdstat_wrapper.py
44 lines (41 loc) · 1.49 KB
/
dstat_wrapper.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
import subprocess
import sys
import json
import re
# this script will run the command (passed as the command args) and then parse the json that it reads from
# stdout and emit a single line with the status which starts with either "completed" or "in-progress". This is all to make it easier for conseq to determine
# whether a job is running or not
# based on https://cloud.google.com/batch/docs/reference/rest/v1alpha/projects.locations.jobs#State
terminal_states = ["SUCCEEDED", "FAILED", "CANCELLED"]
in_progress_state = [
"QUEUED",
"SCHEDULED",
"RUNNING",
"DELETION_IN_PROGRESS",
"CANCELLATION_IN_PROGRESS",
]
command = sys.argv[1:]
stdout = subprocess.check_output(command)
try:
status = json.loads(stdout)
assert len(status) == 1
status_message = status[0]["status-message"]
if status_message is None: # seems to happen right after job submission
prefix = "IN_PROGRESS"
else:
m = re.match(
"Job state is set from [A-Z]+ to ([A-Z]+) for job.*", status_message
)
assert m is not None
state = m.group(1)
if state in terminal_states:
prefix = "COMPLETED"
else:
assert state in in_progress_state
prefix = "IN_PROGRESS"
except Exception as ex:
sys.stderr.write(f"got exception parsing output from command {command}: {stdout}")
raise ex
with open("last_check_status.log", "wt") as fd:
fd.write(f"command: {command}\n{stdout}")
print(f"{prefix}: {status_message}")