-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
64 lines (53 loc) · 2.45 KB
/
worker.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
import asyncio
import websockets
import os
import signal
import msgpack
import collections
import tempfile
import pathlib
import shutil
OUTPUT_CHUCK = 10000000
async def main():
async with websockets.connect(f"ws://{os.environ['CS5223FET_HOST']}/websocket") as websocket:
print(websocket)
while True:
to_worker = msgpack.loads(await websocket.recv(), raw=False)
print(f'Task #{to_worker["task_id"]}', to_worker['command'])
with tempfile.TemporaryDirectory() as workspace:
shutil.copytree('.', workspace, dirs_exist_ok=True)
with open(pathlib.Path(workspace) / 'submit.tar.gz', 'wb') as submit_file:
submit_file.write(bytes(to_worker['upload']))
proc = await asyncio.create_subprocess_shell(to_worker['command'],
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT,
cwd=workspace, preexec_fn=os.setsid)
async def reader(proc):
output = collections.deque()
output_length = 0
while True:
chuck = await proc.stdout.read(OUTPUT_CHUCK)
if not chuck:
return ''.join(output)
output.append(chuck.decode())
output_length += len(chuck)
while len(output) > 1 and output_length - len(output[0]) >= OUTPUT_CHUCK:
discard = output.popleft()
output_length -= len(discard)
output_task = asyncio.create_task(reader(proc))
is_timeout = False
try:
await asyncio.wait_for(proc.wait(), to_worker['timeout'])
except asyncio.TimeoutError:
is_timeout = True
print(f'kill {proc}')
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
output = await output_task
print(f'output length: {len(output)}')
if is_timeout:
output += '\n*** Terminated on hard timeout.'
from_worker = {
'task_id': to_worker['task_id'],
'output': output,
}
await websocket.send(msgpack.dumps(from_worker, use_bin_type=True))
asyncio.run(main())