mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2025-11-04 14:50:01 +08:00
增加任务计数器
当心存在的死锁问题
This commit is contained in:
parent
830159a656
commit
5099568044
@ -13,4 +13,4 @@ RUN useradd -r compiler
|
|||||||
HEALTHCHECK --interval=5s --retries=3 CMD python /code/service.py
|
HEALTHCHECK --interval=5s --retries=3 CMD python /code/service.py
|
||||||
WORKDIR /code
|
WORKDIR /code
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
CMD chown compiler:compiler /spj; gunicorn --workers 4 --threads 4 --error-logfile /log/gunicorn.log --bind 0.0.0.0:8080 server:wsgiapp
|
CMD chown compiler:compiler /spj; echo 0 > /tmp/counter; gunicorn --workers 4 --threads 4 --error-logfile /log/gunicorn.log --bind 0.0.0.0:8080 server:wsgiapp
|
||||||
|
|||||||
@ -20,3 +20,5 @@ COMPILER_GROUP_GID = grp.getgrnam("compiler").gr_gid
|
|||||||
TEST_CASE_DIR = "/test_case"
|
TEST_CASE_DIR = "/test_case"
|
||||||
SPJ_SRC_DIR = "/spj"
|
SPJ_SRC_DIR = "/spj"
|
||||||
SPJ_EXE_DIR = "/spj"
|
SPJ_EXE_DIR = "/spj"
|
||||||
|
|
||||||
|
COUNTER_FILE_PATH = "/tmp/counter"
|
||||||
|
|||||||
@ -10,10 +10,10 @@ import uuid
|
|||||||
import web
|
import web
|
||||||
|
|
||||||
from compiler import Compiler
|
from compiler import Compiler
|
||||||
from config import JUDGER_WORKSPACE_BASE, TEST_CASE_DIR, SPJ_SRC_DIR, SPJ_EXE_DIR
|
from config import JUDGER_WORKSPACE_BASE, SPJ_SRC_DIR, SPJ_EXE_DIR, COUNTER_FILE_PATH
|
||||||
from exception import TokenVerificationFailed, CompileError, SPJCompileError,JudgeClientError
|
from exception import TokenVerificationFailed, CompileError, SPJCompileError,JudgeClientError
|
||||||
from judge_client import JudgeClient
|
from judge_client import JudgeClient
|
||||||
from utils import server_info, get_token, logger
|
from utils import server_info, get_token, logger, TaskCounter
|
||||||
|
|
||||||
|
|
||||||
DEBUG = os.environ.get("judger_debug") == "1"
|
DEBUG = os.environ.get("judger_debug") == "1"
|
||||||
@ -30,9 +30,11 @@ class InitSubmissionEnv(object):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
raise JudgeClientError("failed to create runtime dir")
|
raise JudgeClientError("failed to create runtime dir")
|
||||||
|
TaskCounter().update(+1)
|
||||||
return self.path
|
return self.path
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
TaskCounter().update(-1)
|
||||||
if not DEBUG:
|
if not DEBUG:
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(self.path)
|
shutil.rmtree(self.path)
|
||||||
@ -93,6 +95,7 @@ class JudgeServer(object):
|
|||||||
spj_config=spj_config,
|
spj_config=spj_config,
|
||||||
output=output)
|
output=output)
|
||||||
run_result = judge_client.run()
|
run_result = judge_client.run()
|
||||||
|
|
||||||
return run_result
|
return run_result
|
||||||
|
|
||||||
def compile_spj(self, spj_version, src, spj_compile_config, test_case_id):
|
def compile_spj(self, spj_version, src, spj_compile_config, test_case_id):
|
||||||
|
|||||||
33
utils.py
33
utils.py
@ -5,6 +5,36 @@ import psutil
|
|||||||
import socket
|
import socket
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
import fcntl
|
||||||
|
|
||||||
|
from config import COUNTER_FILE_PATH
|
||||||
|
|
||||||
|
|
||||||
|
class TaskCounter(object):
|
||||||
|
def __init__(self, file_path=COUNTER_FILE_PATH):
|
||||||
|
self.f = open(file_path, "r+")
|
||||||
|
self.fd = self.f.fileno()
|
||||||
|
|
||||||
|
def update(self, action):
|
||||||
|
# lock file
|
||||||
|
fcntl.lockf(self.fd, fcntl.LOCK_EX)
|
||||||
|
try:
|
||||||
|
value = self.f.read()
|
||||||
|
self.f.seek(0)
|
||||||
|
self.f.write(str(int(value) + action))
|
||||||
|
finally:
|
||||||
|
# release lock
|
||||||
|
fcntl.lockf(self.fd, fcntl.LOCK_UN)
|
||||||
|
|
||||||
|
def get(self):
|
||||||
|
# lock file
|
||||||
|
fcntl.lockf(self.fd, fcntl.LOCK_EX)
|
||||||
|
try:
|
||||||
|
value = self.f.read()
|
||||||
|
return int(value)
|
||||||
|
finally:
|
||||||
|
# release lock
|
||||||
|
fcntl.lockf(self.fd, fcntl.LOCK_UN)
|
||||||
|
|
||||||
|
|
||||||
def server_info():
|
def server_info():
|
||||||
@ -13,7 +43,8 @@ def server_info():
|
|||||||
"cpu": psutil.cpu_percent(),
|
"cpu": psutil.cpu_percent(),
|
||||||
"cpu_core": psutil.cpu_count(),
|
"cpu_core": psutil.cpu_count(),
|
||||||
"memory": psutil.virtual_memory().percent,
|
"memory": psutil.virtual_memory().percent,
|
||||||
"judger_version": ".".join([str((ver >> 16) & 0xff), str((ver >> 8) & 0xff), str(ver & 0xff)])}
|
"judger_version": ".".join([str((ver >> 16) & 0xff), str((ver >> 8) & 0xff), str(ver & 0xff)]),
|
||||||
|
"running_task_number": TaskCounter().get()}
|
||||||
|
|
||||||
|
|
||||||
def get_token():
|
def get_token():
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user