Skip to content

Commit

Permalink
feat(judge): add specify directory paths for each judge server
Browse files Browse the repository at this point in the history
Remove hardcode in code
  • Loading branch information
tobiichi3227 committed Sep 4, 2024
1 parent b6883d3 commit 42ebf54
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 0 additions & 2 deletions src/handlers/contests/manage/pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ async def _rechal(rechals):
pro_id,
pro['testm_conf'],
comp_type,
f"/srv/ntoj/code/{chal_id}/main.{file_ext}",
f"/srv/ntoj/problem/{pro_id}/res",
)

await asyncio.create_task(_rechal(rechals=result))
Expand Down
2 changes: 0 additions & 2 deletions src/handlers/manage/pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,6 @@ async def _rechal(rechals):
pro_id,
pro['testm_conf'],
comp_type,
f"/srv/ntoj/code/{chal_id}/main.{file_ext}",
f"/srv/ntoj/problem/{pro_id}/res",
)

await asyncio.create_task(_rechal(rechals=result))
Expand Down
4 changes: 0 additions & 4 deletions src/handlers/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,11 @@ async def post(self):
self.error('Eparam')
return

file_ext = ChalConst.FILE_EXTENSION[comp_type]

err, _ = await ChalService.inst.emit_chal(
chal_id,
pro_id,
pro['testm_conf'],
comp_type,
f'/srv/ntoj/code/{chal_id}/main.{file_ext}',
f'/srv/ntoj/problem/{pro_id}/res',
)
if err:
self.error(err)
Expand Down
6 changes: 3 additions & 3 deletions src/services/chal.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ async def get_chal(self, chal_id):
},
)

async def emit_chal(self, chal_id, pro_id, testm_conf, comp_type, code_path, res_path):
async def emit_chal(self, chal_id, pro_id, testm_conf, comp_type):
chal_id = int(chal_id)
pro_id = int(pro_id)

Expand Down Expand Up @@ -355,8 +355,8 @@ async def emit_chal(self, chal_id, pro_id, testm_conf, comp_type, code_path, res
'pri': 1,
'chal_id': chal_id,
'test': testl,
'code_path': code_path,
'res_path': res_path,
'code_path': f'{chal_id}/main.{file_ext}',
'res_path': f'{pro_id}/res',
'metadata': chalmeta,
'comp_type': comp_type,
'check_type': test_conf['check_type'],
Expand Down
30 changes: 24 additions & 6 deletions src/services/judge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@


class JudgeServerService:
def __init__(self, rs, server_name, server_url, judge_id) -> None:
def __init__(self, rs, server_name: str, server_url: str, codes_path: str, problems_path: str, judge_id) -> None:
self.rs = rs
self.server_name = server_name
self.server_url = server_url
self.judge_id = judge_id
self.codes_path = codes_path
self.problems_path = problems_path
self.status = True
self.ws = None

Expand Down Expand Up @@ -61,7 +63,7 @@ async def response_handle(self, ret):
res['chal_id'],
test_idx,
result['status'],
int(result['time'] / 10**6), # ns to ms
int(result['time'] / 10 ** 6), # ns to ms
result['memory'],
result['verdict'],
refresh_db=False,
Expand Down Expand Up @@ -130,7 +132,10 @@ async def send(self, data):
),
)

await self.ws.write_message(data)
data['code_path'] = f"{self.codes_path}/{data['code_path']}"
data['res_path'] = f"{self.problems_path}/{data['res_path']}"

await self.ws.write_message(json.dumps(data))

async def offline_notice(self):
await LogService.inst.add_log(f"Judge {self.server_name} offline", "judge.offline")
Expand All @@ -147,10 +152,23 @@ def __init__(self, rs, server_urls: List[Dict]) -> None:
for judge_id, server in enumerate(server_urls):
url = server.get('url')
name = server.get('name')
codes_path = server.get('codes_path')
problems_path = server.get('problems_path')

# TODO: add log
if url is None:
continue

if codes_path is None:
continue

if problems_path is None:
continue

if name is None:
name = ''
name = f'JudgeServer-{judge_id}'

self.servers.append(JudgeServerService(self.rs, name, url, judge_id))
self.servers.append(JudgeServerService(self.rs, name, url, codes_path, problems_path, judge_id))

async def start(self) -> None:
for idx, judge_server in enumerate(self.servers):
Expand Down Expand Up @@ -233,7 +251,7 @@ async def send(self, data, pri, pro_id, contest_id) -> None:
if not status['status']:
continue

await self.servers[i].send(json.dumps(data))
await self.servers[i].send(data)
self.servers[i].chal_map[data['chal_id']] = {"pro_id": pro_id, "contest_id": contest_id}

self.idx = i
Expand Down

0 comments on commit 42ebf54

Please sign in to comment.