This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
204 lines (163 loc) · 5.71 KB
/
run.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os
import signal
import socket
import asyncio
import logging
import argparse
from threading import Event
from logging.handlers import TimedRotatingFileHandler
from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED
import requests
from chainlink import Chainlink
from config import *
import grader.api_keys as api_keys
# globals
worker_id = None
hostname = None
worker_thread = None
heartbeat_interval = HEARTBEAT_INTERVAL
event_loop = asyncio.new_event_loop()
exit_event = Event()
# setting up logger
os.makedirs(LOGS_DIR, exist_ok=True)
logging.basicConfig(
handlers=[
TimedRotatingFileHandler(
"{}/log".format(LOGS_DIR), when="midnight", backupCount=7
),
logging.StreamHandler(),
],
level=logging.INFO,
)
logger = logging.getLogger()
def halt_all():
exit_event.set()
def signal_handler(sig, frame):
halt_all()
def get_url(endpoint):
return "{}://{}:{}{}{}".format(
"https" if USE_SSL else "http", API_HOSTNAME, API_PORT, API_PROXY, endpoint
)
def heartbeat_routine():
while not exit_event.is_set():
response = requests.post(
get_url("{}/{}".format(HEARTBEAT_ENDPOINT, worker_id)),
headers=header,
data="",
)
if response.status_code != SUCCESS_CODE:
logger.critical("Heartbeat failed!\nError: {}".format(response.text))
return
exit_event.wait(heartbeat_interval)
def worker_routine():
asyncio.set_event_loop(event_loop)
while not exit_event.is_set():
# poll from queue
response = requests.get(
get_url("{}/{}".format(GRADING_JOB_ENDPOINT, worker_id)), headers=header
)
# if the queue is empty then sleep for a while
if response.status_code == QUEUE_EMPTY_CODE:
exit_event.wait(JOB_POLL_INTERVAL)
continue
if response.status_code != SUCCESS_CODE:
logger.critical(
"Bad server response while trying to poll job.\nError: {}".format(
response.text
)
)
return
# we successfully polled a job
job = response.json()["data"]
job_id = job.get(api_keys.GRADING_JOB_ID)
logger.info("Starting job {}".format(job_id))
# execute job
try:
chain = Chainlink(job[api_keys.STAGES], workdir=os.getcwd())
job_results = chain.run({})
except Exception as ex:
logger.critical("Grading job failed with exception:\n{}".format(ex))
job_results = [
{
"logs": {
"stdout": b"The container crashed",
"stderr": bytes(str(ex), "utf-8"),
},
"success": False,
}
]
job_stdout = "\n".join(
[r["logs"]["stdout"].decode("utf-8") for r in job_results]
)
job_stderr = "\n".join(
[r["logs"]["stderr"].decode("utf-8") for r in job_results]
)
# remove logs from result array because logs can be bulky we will store then separately
for r in job_results:
del r["logs"]
logger.info("Finished job {}".format(job_id))
if VERBOSE:
logger.info("Job stdout:\n" + job_stdout)
logger.info("Job stderr:\n" + job_stderr)
grading_job_result = {
api_keys.RESULTS: job_results,
api_keys.SUCCESS: job_results[-1]["success"],
api_keys.LOGS: {"stdout": job_stdout, "stderr": job_stderr},
api_keys.GRADING_JOB_ID: job_id,
}
logger.info("Sending job results")
response = requests.post(
get_url("{}/{}".format(GRADING_JOB_ENDPOINT, worker_id)),
json=grading_job_result,
headers=header,
)
if response.status_code != SUCCESS_CODE:
logger.critical(
"Bad server response while updating about job status.\nError: {}".format(
response.text
)
)
return
def register_node():
global worker_id
global heartbeat_interval
response = requests.post(
get_url("{}/{}".format(GRADER_REGISTER_ENDPOINT, worker_id)),
headers=header,
json={api_keys.HOSTNAME: hostname},
)
if response.status_code != SUCCESS_CODE:
logger.critical("Registration failed!\nError: {}".format(response.text))
exit(-1)
logger.info("Registered to server")
server_response = response.json()["data"]
# set heartbeat interval
if api_keys.HEARTBEAT in server_response:
heartbeat_interval = server_response[api_keys.HEARTBEAT]
else:
logger.info(
"Server response did not include heartbeat, using default {}".format(
heartbeat_interval
)
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("token", help="Broadway cluster token")
parser.add_argument(
"worker_id", metavar="worker-id", help="Unique worker id for registration"
)
return parser.parse_args()
if __name__ == "__main__":
# check valid usage
args = parse_args()
signal.signal(signal.SIGINT, signal_handler)
worker_id = args.worker_id
hostname = socket.gethostname()
# register node to server
header = {api_keys.AUTH: "Bearer {}".format(args.token)}
register_node()
# run the grader on two separate threads. If any of the routines fail, the grader shuts down
executor = ThreadPoolExecutor(max_workers=2)
futures = [executor.submit(heartbeat_routine), executor.submit(worker_routine)]
wait(futures, return_when=FIRST_COMPLETED)
executor.shutdown()