add judge server is_disabled field

This commit is contained in:
virusdefender 2017-12-24 12:10:02 +08:00
parent 290be9f8bc
commit 03cf356bab
6 changed files with 42 additions and 6 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-12-24 03:44
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('conf', '0002_auto_20171011_1214'),
]
operations = [
migrations.AddField(
model_name='judgeserver',
name='is_disabled',
field=models.BooleanField(default=False),
),
]

View File

@ -13,6 +13,7 @@ class JudgeServer(models.Model):
create_time = models.DateTimeField(auto_now_add=True)
task_number = models.IntegerField(default=0)
service_url = models.CharField(max_length=256, blank=True, null=True)
is_disabled = models.BooleanField(default=False)
@property
def status(self):

View File

@ -44,3 +44,8 @@ class JudgeServerHeartbeatSerializer(serializers.Serializer):
cpu = serializers.FloatField(min_value=0, max_value=100)
action = serializers.ChoiceField(choices=("heartbeat", ))
service_url = serializers.CharField(max_length=256)
class EditJudgeServerSerializer(serializers.Serializer):
id = serializers.IntegerField()
is_disabled = serializers.BooleanField()

View File

@ -105,7 +105,7 @@ class JudgeServerHeartbeatTest(APITestCase):
class JudgeServerAPITest(APITestCase):
def setUp(self):
JudgeServer.objects.create(**{"hostname": "testhostname", "judger_version": "1.0.4",
self.server = JudgeServer.objects.create(**{"hostname": "testhostname", "judger_version": "1.0.4",
"cpu_core": 4, "cpu_usage": 90.5, "memory_usage": 80.3,
"last_heartbeat": timezone.now()})
self.url = self.reverse("judge_server_api")
@ -121,6 +121,11 @@ class JudgeServerAPITest(APITestCase):
self.assertSuccess(resp)
self.assertFalse(JudgeServer.objects.filter(hostname="testhostname").exists())
def test_disabled_judge_server(self):
resp = self.client.put(self.url, data={"is_disabled": True, "id": self.server.id})
self.assertSuccess(resp)
self.assertTrue(JudgeServer.objects.get(id=self.server.id).is_disabled)
class LanguageListAPITest(APITestCase):
def test_get_languages(self):

View File

@ -17,7 +17,7 @@ from .models import JudgeServer
from .serializers import (CreateEditWebsiteConfigSerializer,
CreateSMTPConfigSerializer, EditSMTPConfigSerializer,
JudgeServerHeartbeatSerializer,
JudgeServerSerializer, TestSMTPConfigSerializer)
JudgeServerSerializer, TestSMTPConfigSerializer, EditJudgeServerSerializer)
class SMTPAPI(APIView):
@ -102,6 +102,12 @@ class JudgeServerAPI(APIView):
JudgeServer.objects.filter(hostname=hostname).delete()
return self.success()
@validate_serializer(EditJudgeServerSerializer)
@super_admin_required
def put(self, request):
JudgeServer.objects.filter(id=request.data["id"]).update(is_disabled=request.data["is_disabled"])
return self.success()
class JudgeServerHeartbeatAPI(CSRFExemptAPIView):
@validate_serializer(JudgeServerHeartbeatSerializer)

View File

@ -6,7 +6,6 @@ from urllib.parse import urljoin
import requests
from django.db import transaction
from django.db.models import F
from django.conf import settings
from account.models import User
from conf.models import JudgeServer
@ -47,7 +46,7 @@ class DispatcherBase(object):
@staticmethod
def choose_judge_server():
with transaction.atomic():
servers = JudgeServer.objects.select_for_update().all().order_by("task_number")
servers = JudgeServer.objects.select_for_update().filter(is_disabled=False).order_by("task_number")
servers = [s for s in servers if s.status == "normal"]
if servers:
server = servers[0]