增加任务计数器

当心存在的死锁问题
This commit is contained in:
virusdefender 2016-10-22 21:22:20 +08:00
parent 830159a656
commit 5099568044
4 changed files with 40 additions and 4 deletions

View File

@ -13,4 +13,4 @@ RUN useradd -r compiler
HEALTHCHECK --interval=5s --retries=3 CMD python /code/service.py
WORKDIR /code
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

View File

@ -20,3 +20,5 @@ COMPILER_GROUP_GID = grp.getgrnam("compiler").gr_gid
TEST_CASE_DIR = "/test_case"
SPJ_SRC_DIR = "/spj"
SPJ_EXE_DIR = "/spj"
COUNTER_FILE_PATH = "/tmp/counter"

View File

@ -10,10 +10,10 @@ import uuid
import web
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 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"
@ -30,9 +30,11 @@ class InitSubmissionEnv(object):
except Exception as e:
logger.exception(e)
raise JudgeClientError("failed to create runtime dir")
TaskCounter().update(+1)
return self.path
def __exit__(self, exc_type, exc_val, exc_tb):
TaskCounter().update(-1)
if not DEBUG:
try:
shutil.rmtree(self.path)
@ -93,6 +95,7 @@ class JudgeServer(object):
spj_config=spj_config,
output=output)
run_result = judge_client.run()
return run_result
def compile_spj(self, spj_version, src, spj_compile_config, test_case_id):

View File

@ -5,6 +5,36 @@ import psutil
import socket
import os
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():
@ -13,7 +43,8 @@ def server_info():
"cpu": psutil.cpu_percent(),
"cpu_core": psutil.cpu_count(),
"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():