From 42ebf5458ec097bb0f3af26bbc9d5a289fe3c73f Mon Sep 17 00:00:00 2001 From: tobiichi3227 Date: Wed, 4 Sep 2024 10:42:01 +0800 Subject: [PATCH] feat(judge): add specify directory paths for each judge server Remove hardcode in code --- src/handlers/contests/manage/pro.py | 2 -- src/handlers/manage/pro.py | 2 -- src/handlers/submit.py | 4 ---- src/services/chal.py | 6 +++--- src/services/judge.py | 30 +++++++++++++++++++++++------ 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/handlers/contests/manage/pro.py b/src/handlers/contests/manage/pro.py index 128c4062..c3e5191f 100644 --- a/src/handlers/contests/manage/pro.py +++ b/src/handlers/contests/manage/pro.py @@ -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)) diff --git a/src/handlers/manage/pro.py b/src/handlers/manage/pro.py index 6abef192..fc15081b 100644 --- a/src/handlers/manage/pro.py +++ b/src/handlers/manage/pro.py @@ -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)) diff --git a/src/handlers/submit.py b/src/handlers/submit.py index c335a545..0aab007d 100644 --- a/src/handlers/submit.py +++ b/src/handlers/submit.py @@ -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) diff --git a/src/services/chal.py b/src/services/chal.py index fae6964c..b5f266d8 100644 --- a/src/services/chal.py +++ b/src/services/chal.py @@ -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) @@ -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'], diff --git a/src/services/judge.py b/src/services/judge.py index 535b5c65..3171104f 100644 --- a/src/services/judge.py +++ b/src/services/judge.py @@ -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 @@ -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, @@ -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") @@ -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): @@ -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