-
Notifications
You must be signed in to change notification settings - Fork 8
/
reporter.py
executable file
·175 lines (137 loc) · 5.71 KB
/
reporter.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
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
import io
import json
import sys
import os
import requests
import time
import signal
import argparse
from dwq import Disque, Job
from common import parse_job
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), "utf-8", "replace")
MURDOCK_API_BASE_URL = "http://localhost:8000"
def signal_handler(signal, frame):
print(f"Exiting with signal {signal}: {frame}")
sys.exit(0)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
def save_job_result(job):
if job["type"] in ["builds", "tests"]:
filename = os.path.join(
"output", job["type"], job["application"], f"{job['target']}:{job['toolchain']}.txt"
)
else:
filename = os.path.join("output", f"{job['name']}.txt")
if filename:
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
f.write(job["output"])
return filename
def update_status(data, uid, token, failed_jobs, failed_builds, failed_tests):
status = {}
# copy expected (but optional) fields that are in data
if data is not None:
for f in {"total", "passed", "failed", "status", "eta"} & data.keys():
status[f] = data[f]
# add failed_jobs list (elements have fields "name" [required] and
# "href" [optional]) field
if failed_jobs:
status["failed_jobs"] = []
for jobname in failed_jobs:
failed_job = {"name": jobname}
status["failed_jobs"].append(failed_job)
if failed_builds:
status["failed_builds"] = []
for application, target, toolchain, worker, runtime in failed_builds:
failed_build = {
"application": application,
"target": target,
"toolchain": toolchain,
"worker": worker,
"runtime": runtime,
}
status["failed_builds"].append(failed_build)
if failed_tests:
status["failed_tests"] = []
for application, target, toolchain, worker, runtime in failed_tests:
failed_test = {
"application": application,
"target": target,
"toolchain": toolchain,
"worker": worker,
"runtime": runtime,
}
status["failed_tests"].append(failed_test)
data = json.dumps({"uid" : uid, "status" : status})
requests.put(
f'{MURDOCK_API_BASE_URL}/job/{uid}/status',
headers={"Authorization": token},
data=data
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("queue", type=str, help="Name of the queue to listen to")
parser.add_argument("job_uid", type=str, help="UID of the Murdock job")
parser.add_argument("job_token", type=str, help="Authentication token of the Murdock job")
args = parser.parse_args()
queue = args.queue
uid = args.job_uid
token = args.job_token
disque_url = os.environ.get("DWQ_DISQUE_URL", "localhost:7711")
Disque.connect([disque_url])
last_update = 0
maxfailed_jobs = 20
maxfailed_builds = 20
maxfailed_tests = 20
failed_jobs = []
failed_builds = []
failed_tests = []
nfailed_jobs = 0
nfailed_builds = 0
nfailed_tests = 0
update_status({"status" : "setting up build" }, uid, token, [], [], [])
while True:
_list = Job.wait(queue, count=16)
for _status in _list:
job_raw = _status.get('job')
if job_raw:
job = parse_job(job_raw)
filename = save_job_result(job)
if filename and job["status"] is False:
jobname = job["name"]
worker = job["worker"]
runtime = job["runtime"]
if jobname == "static_tests":
nfailed_jobs += 1
failed_jobs.append(jobname)
elif jobname.startswith("compile/"):
nfailed_builds += 1
if nfailed_builds <= maxfailed_builds:
failed_builds.append(
(job["application"], job["target"], job["toolchain"], worker, runtime)
)
elif jobname.startswith("run_test/"):
nfailed_tests += 1
if nfailed_tests <= maxfailed_tests:
failed_tests.append(
(job["application"], job["target"], job["toolchain"], worker, runtime)
)
failed_jobs = failed_jobs[:maxfailed_jobs]
failed_builds = failed_builds[:maxfailed_builds]
failed_tests = failed_tests[:maxfailed_tests]
if nfailed_jobs > maxfailed_jobs:
failed_jobs.append((f"and {nfailed_jobs - maxfailed_jobs} more failed jobs...", None, None, None, None))
if nfailed_builds > maxfailed_builds:
failed_builds.append((f"and {nfailed_builds - maxfailed_builds} more build failures...", None, None, None, None))
if nfailed_tests > maxfailed_tests:
failed_tests.append((f"and {nfailed_tests - maxfailed_tests} more test failures...", None, None, None, None))
if _status.get("status", "") == "done":
update_status(None, uid, token, failed_jobs, failed_builds, failed_tests)
return
now = time.time()
if now - last_update > 0.5:
update_status(_status, uid, token, failed_jobs, failed_builds, failed_tests)
last_update = now
if __name__=="__main__":
main()