forked from pzread/judge
/
Server.py
164 lines (121 loc) · 4.2 KB
/
Server.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
'''Server module.
Handle and response challenge requests from the frontend server.
'''
import json
from collections import deque
from tornado import gen
from tornado.ioloop import IOLoop, PollIOLoop
from tornado.web import Application
from tornado.websocket import WebSocketHandler
import PyExt
import Privilege
import Config
from StdChal import StdChal
class EvIOLoop(PollIOLoop):
'''Tornado compatible ioloop interface.'''
def initialize(self, **kwargs):
'''Initialize.'''
super().initialize(impl=PyExt.EvPoll(), **kwargs)
class JudgeHandler(WebSocketHandler):
'''Judge request handler.
Static attributes:
chal_running_count (int): Number of current running challenges.
chal_queue (deque): Pending challenges.
'''
chal_running_count = 0
chal_queue = deque()
@staticmethod
@gen.coroutine
def start_chal(obj, websk):
'''Start a challenge.
Check the challenge config, issue judge tasks, then report the result.
Args:
obj (dict): Challenge config.
websk (WebSocketHandler): Websocket object.
Returns:
None
'''
chal_id = obj['chal_id']
code_path = '/srv/nfs' + obj['code_path'][4:]
test_list = obj['testl']
res_path = '/srv/nfs' + obj['res_path'][4:]
test_paramlist = list()
comp_type = test_list[0]['comp_type']
assert comp_type in ['g++', 'clang++', 'makefile', 'python3']
for test in test_list:
assert test['comp_type'] == comp_type
assert test['check_type'] == 'diff'
test_idx = test['test_idx']
memlimit = test['memlimit']
timelimit = test['timelimit']
data_ids = test['metadata']['data']
for data_id in data_ids:
test_paramlist.append({
'in': res_path + '/testdata/%d.in'%data_id,
'ans': res_path + '/testdata/%d.out'%data_id,
'timelimit': timelimit,
'memlimit': memlimit,
})
chal = StdChal(chal_id, code_path, comp_type, res_path, test_paramlist)
result_list = yield chal.start()
idx = 0
for test in test_list:
test_idx = test['test_idx']
data_ids = test['metadata']['data']
total_runtime = 0
total_mem = 0
total_status = 0
for data_id in data_ids:
runtime, peakmem, status = result_list[idx]
total_runtime += runtime
total_mem += peakmem
total_status = max(total_status, status)
idx += 1
websk.write_message(json.dumps({
'chal_id': chal_id,
'test_idx': test_idx,
'state': total_status,
'runtime': total_runtime,
'memory': total_mem,
}))
JudgeHandler.chal_running_count -= 1
JudgeHandler.emit_chal()
@staticmethod
def emit_chal(obj=None, websk=None):
'''Emit a challenge to the queue and trigger the start_chal.
Args:
obj (dict, optional): Challenge config.
websk (WebSocketHandler): Websocket object.
Returns:
None
'''
if obj is not None:
JudgeHandler.chal_queue.append((obj, websk))
while len(JudgeHandler.chal_queue) > 0 \
and JudgeHandler.chal_running_count < Config.TASK_MAXCONCURRENT:
chal = JudgeHandler.chal_queue.popleft()
JudgeHandler.chal_running_count += 1
IOLoop.instance().add_callback(JudgeHandler.start_chal, *chal)
def open(self):
'''Handle open event'''
pass
def on_message(self, msg):
'''Handle message event'''
obj = json.loads(msg, 'utf-8')
JudgeHandler.emit_chal(obj, self)
def on_close(self):
'''Handle close event'''
pass
def main():
'''Main function.'''
Privilege.init()
PyExt.init()
StdChal.init()
IOLoop.configure(EvIOLoop)
app = Application([
(r'/judge', JudgeHandler),
])
app.listen(2501)
IOLoop.instance().start()
if __name__ == '__main__':
main()