From 290be9f8bc7f0722150064d8c355953cb1c273bc Mon Sep 17 00:00:00 2001 From: virusdefender Date: Sun, 24 Dec 2017 12:08:56 +0800 Subject: [PATCH] service url now is required --- conf/serializers.py | 2 +- conf/tests.py | 12 +----------- conf/views.py | 5 ++--- judge/dispatcher.py | 6 +----- 4 files changed, 5 insertions(+), 20 deletions(-) diff --git a/conf/serializers.py b/conf/serializers.py index 3c741b27..3dd16650 100644 --- a/conf/serializers.py +++ b/conf/serializers.py @@ -43,4 +43,4 @@ class JudgeServerHeartbeatSerializer(serializers.Serializer): memory = serializers.FloatField(min_value=0, max_value=100) cpu = serializers.FloatField(min_value=0, max_value=100) action = serializers.ChoiceField(choices=("heartbeat", )) - service_url = serializers.CharField(max_length=256, required=False) + service_url = serializers.CharField(max_length=256) diff --git a/conf/tests.py b/conf/tests.py index 00cf8147..4d5370df 100644 --- a/conf/tests.py +++ b/conf/tests.py @@ -82,7 +82,7 @@ class JudgeServerHeartbeatTest(APITestCase): def setUp(self): self.url = self.reverse("judge_server_heartbeat_api") self.data = {"hostname": "testhostname", "judger_version": "1.0.4", "cpu_core": 4, - "cpu": 90.5, "memory": 80.3, "action": "heartbeat"} + "cpu": 90.5, "memory": 80.3, "action": "heartbeat", "service_url": "http://127.0.0.1"} self.token = "test" self.hashed_token = hashlib.sha256(self.token.encode("utf-8")).hexdigest() SysOptions.judge_server_token = self.token @@ -93,16 +93,6 @@ class JudgeServerHeartbeatTest(APITestCase): self.assertSuccess(resp) server = JudgeServer.objects.first() self.assertEqual(server.ip, "127.0.0.1") - self.assertEqual(server.service_url, None) - - def test_new_heartbeat_service_url(self): - service_url = "http://1.2.3.4:8000/api/judge" - data = self.data - data["service_url"] = service_url - resp = self.client.post(self.url, data=self.data, **self.headers) - self.assertSuccess(resp) - server = JudgeServer.objects.first() - self.assertEqual(server.service_url, service_url) def test_update_heartbeat(self): self.test_new_heartbeat() diff --git a/conf/views.py b/conf/views.py index 942c0225..2bd059e5 100644 --- a/conf/views.py +++ b/conf/views.py @@ -110,7 +110,6 @@ class JudgeServerHeartbeatAPI(CSRFExemptAPIView): client_token = request.META.get("HTTP_X_JUDGE_SERVER_TOKEN") if hashlib.sha256(SysOptions.judge_server_token.encode("utf-8")).hexdigest() != client_token: return self.error("Invalid token") - service_url = data.get("service_url") try: server = JudgeServer.objects.get(hostname=data["hostname"]) @@ -118,7 +117,7 @@ class JudgeServerHeartbeatAPI(CSRFExemptAPIView): server.cpu_core = data["cpu_core"] server.memory_usage = data["memory"] server.cpu_usage = data["cpu"] - server.service_url = service_url + server.service_url = data["service_url"] server.ip = request.META["HTTP_X_REAL_IP"] server.last_heartbeat = timezone.now() server.save() @@ -129,7 +128,7 @@ class JudgeServerHeartbeatAPI(CSRFExemptAPIView): memory_usage=data["memory"], cpu_usage=data["cpu"], ip=request.META["REMOTE_ADDR"], - service_url=service_url, + service_url=data["service_url"], last_heartbeat=timezone.now(), ) # 新server上线 处理队列中的,防止没有新的提交而导致一直waiting diff --git a/judge/dispatcher.py b/judge/dispatcher.py index 289e50a1..682e054e 100644 --- a/judge/dispatcher.py +++ b/judge/dispatcher.py @@ -154,11 +154,7 @@ class JudgeDispatcher(DispatcherBase): Submission.objects.filter(id=self.submission.id).update(result=JudgeStatus.JUDGING) - service_url = server.service_url - # not set service_url, it should be a linked container - if not service_url: - service_url = settings.DEFAULT_JUDGE_SERVER_SERVICE_URL - resp = self._request(urljoin(service_url, "/judge"), data=data) + resp = self._request(urljoin(server.service_url, "/judge"), data=data) if resp["err"]: self.submission.result = JudgeStatus.COMPILE_ERROR self.submission.statistic_info["err_info"] = resp["data"]