判题暂时可以跑起来了,真要被自己蠢哭了

This commit is contained in:
virusdefender 2015-08-12 19:55:41 +08:00
parent 92ab7e5fb2
commit 66d3cd3bfe
13 changed files with 43 additions and 31 deletions

View File

@ -1 +0,0 @@
# coding=utf-8

View File

@ -1,2 +0,0 @@
# coding=utf-8
DB_NAME = "db.sqlite3"

View File

@ -30,7 +30,7 @@ class JudgeClient(object):
:param test_case_dir: 测试用例文件夹路径 :param test_case_dir: 测试用例文件夹路径
:return:返回结果list :return:返回结果list
""" """
self._language = languages[str(language_code)] self._language = languages[language_code]
self._exe_path = exe_path self._exe_path = exe_path
self._max_cpu_time = max_cpu_time self._max_cpu_time = max_cpu_time
self._max_real_time = max_real_time self._max_real_time = max_real_time

View File

@ -32,5 +32,4 @@ def compile_(language_item, src_path, exe_path):
if parse_result["exit_code"] or parse_result["term_sig"] or parse_result["siginaled"] or parse_result["exceed"]: if parse_result["exit_code"] or parse_result["term_sig"] or parse_result["siginaled"] or parse_result["exceed"]:
raise CompileError("Compile error") raise CompileError("Compile error")
return exe_path return exe_path

View File

@ -6,14 +6,14 @@ languages = {
"name": "c", "name": "c",
"src_name": "main.c", "src_name": "main.c",
"code": 1, "code": 1,
"compile_command": "gcc -DONLINE_JUDGE -O2 -w -std=c99 -pipe {src_path} -lm -o {exe_path}main", "compile_command": "gcc -DONLINE_JUDGE -O2 -w -std=c99 {src_path} -lm -o {exe_path}main",
"execute_command": "{exe_path}main" "execute_command": "{exe_path}main"
}, },
2: { 2: {
"name": "cpp", "name": "cpp",
"src_name": "main.cpp", "src_name": "main.cpp",
"code": 2, "code": 2,
"compile_command": "g++ -DONLINE_JUDGE -O2 -w -std=c++11 -pipe {src_path} -lm -o {exe_path}main", "compile_command": "g++ -DONLINE_JUDGE -O2 -w -std=c++11 {src_path} -lm -o {exe_path}main",
"execute_command": "{exe_path}main" "execute_command": "{exe_path}main"
}, },
3: { 3: {

View File

@ -8,14 +8,8 @@ from client import JudgeClient
from language import languages from language import languages
from compiler import compile_ from compiler import compile_
from result import result from result import result
from settings import judger_workspace from settings import judger_workspace, mongodb_config
import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
print sys.path
from oj import settings
# 简单的解析命令行参数 # 简单的解析命令行参数
# 参数有 -solution_id -time_limit -memory_limit -test_case_id # 参数有 -solution_id -time_limit -memory_limit -test_case_id
@ -26,16 +20,13 @@ time_limit = args[4]
memory_limit = args[6] memory_limit = args[6]
test_case_id = args[8] test_case_id = args[8]
connection = pymongo.MongoClient(host=mongodb_config["host"], port=mongodb_config["port"])
mongodb_setting = settings.DATABASES["mongodb"]
connection = pymongo.MongoClient(host=mongodb_setting["HOST"], port=mongodb_setting["PORT"])
collection = connection["oj"]["oj_submission"] collection = connection["oj"]["oj_submission"]
submission = collection.find_one({"_id": ObjectId(solution_id)}) submission = collection.find_one({"_id": ObjectId(solution_id)})
if not submission: if not submission:
exit() exit()
# 将代码写入文件 # 将代码写入文件
language = languages[submission["language"]] language = languages[submission["language"]]
src_path = judger_workspace + "run/" + language["src_name"] src_path = judger_workspace + "run/" + language["src_name"]
@ -47,18 +38,18 @@ f.close()
try: try:
exe_path = compile_(language, src_path, judger_workspace + "run/") exe_path = compile_(language, src_path, judger_workspace + "run/")
except Exception as e: except Exception as e:
print e print {"result": result["compile_error"]}
print [{"result": result["compile_error"]}]
exit() exit()
try: try:
client = JudgeClient(language_code=language, client = JudgeClient(language_code=submission["language"],
exe_path=exe_path, exe_path=exe_path,
max_cpu_time=int(time_limit), max_cpu_time=int(time_limit),
max_real_time=int(time_limit) * 2, max_real_time=int(time_limit) * 2,
max_memory=int(memory_limit), max_memory=int(memory_limit),
test_case_dir="/var/judger/test_case/" + str(test_case_id) + "/") test_case_dir= judger_workspace + "test_case/" + test_case_id + "/")
print client.run() print client.run()
except Exception as e: except Exception as e:
print e print e
print {"result": result["system_error"]}

View File

@ -11,5 +11,12 @@ lrun_uid = 1001
# lrun用户组gid # lrun用户组gid
lrun_gid = 1002 lrun_gid = 1002
#judger工作目录 # judger工作目录
judger_workspace = "/var/judger/" judger_workspace = "/var/judger/"
mongodb_config = {
"host": "192.168.59.3",
"username": "root",
"password": "root",
"port": 27017
}

View File

@ -1,5 +1,10 @@
# coding=utf-8 # coding=utf-8
from __future__ import absolute_import from __future__ import absolute_import
from celery import Celery from celery import Celery
from .settings import redis_config
app = Celery("judge", broker="redis://localhost:6379/0", include=["judger_controller.tasks"]) app = Celery("judge", broker="redis://" +
redis_config["host"] + ":" +
str(redis_config["port"]) +
"/" + str(redis_config["db"]),
include=["judger_controller.tasks"])

View File

@ -0,0 +1,6 @@
# coding=utf-8
redis_config = {
"host": "127.0.0.1",
"port": 6379,
"db": 0
}

View File

@ -8,13 +8,13 @@ import subprocess32 as subprocess
def judge(solution_id, time_limit, memory_limit, test_case_id): def judge(solution_id, time_limit, memory_limit, test_case_id):
try: try:
subprocess.call("docker run -t -i --privileged --rm=true " subprocess.call("docker run -t -i --privileged --rm=true "
"-v /var/test_case/:/var/judger/test_case/ " "-v /Users/virusdefender/Desktop/test_case/:/var/judger/test_case/ "
"-v /Users/virusdefender/Desktop/:/var/judger/code/ " "-v /Users/virusdefender/Desktop/:/var/judger/code/ "
"-p 27017:27017 "
"judger " "judger "
"python judger/run.py " "python judger/run.py "
"--solution_id %s --time_limit %s --memory_limit %s --test_case_id %s" % "--solution_id %s --time_limit %s --memory_limit %s --test_case_id %s" %
(solution_id, str(time_limit), str(memory_limit), test_case_id), (solution_id, str(time_limit), str(memory_limit), test_case_id),
timeout=(time_limit / 100) * 20, shell=True) # 如果设置的最长运行时间小于1000毫秒那么/1000就是0处理一下这个情况设置为两秒
timeout=(time_limit / 1000 * 3) or 2, shell=True)
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
print "docker timeout" print "docker timeout"

View File

@ -7,6 +7,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 下面是需要自己修改的 # 下面是需要自己修改的
LOG_PATH = "LOG/" LOG_PATH = "LOG/"
# 注意这是web 服务器访问的地址,判题度武器访问的地址不一定一样,因为可能不在一台机器上
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.sqlite3',
@ -24,5 +25,6 @@ DATABASES = {
DEBUG = True DEBUG = True
TEST_CASE_DIR = "/var/test_case/" # 同理 这是 web 服务器的上传路径
TEST_CASE_DIR = "/Users/virusdefender/Desktop/test_case/"

View File

@ -1,4 +1,4 @@
require(["jquery", "code_mirror"], function ($, code_mirror) { require(["jquery", "code_mirror", "csrf"], function ($, code_mirror, csrfHeader) {
var code_editor = code_mirror($("#code-editor")[0], "text/x-csrc"); var code_editor = code_mirror($("#code-editor")[0], "text/x-csrc");
var language = "1"; var language = "1";
@ -21,7 +21,11 @@ require(["jquery", "code_mirror"], function ($, code_mirror) {
$("#submit-code-button").click(function () { $("#submit-code-button").click(function () {
show_loading(); show_loading();
$.ajax({ $.ajax({
beforeSend: csrfHeader,
url: "/api/submission/",
method: "post",
data: JSON.stringify({problem_id: 2, language: language, code: code_editor.getValue()}),
contentType: "application/json"
}); });
setTimeout( setTimeout(
function () { function () {

View File

@ -32,6 +32,7 @@ class SubmissionnAPIView(APIView):
serializer = CreateSubmissionSerializer(data=request.data) serializer = CreateSubmissionSerializer(data=request.data)
if serializer.is_valid(): if serializer.is_valid():
data = serializer.data data = serializer.data
# data["language"] = int(data["language"])
data["user_id"] = request.user.id data["user_id"] = request.user.id
data["result"] = result["waiting"] data["result"] = result["waiting"]
try: try: