修改判题数据库

This commit is contained in:
virusdefender
2015-08-17 12:48:10 +08:00
parent 3f76df3079
commit 537da5d795
18 changed files with 217 additions and 113 deletions

View File

@@ -1,9 +1,7 @@
# coding=utf-8
import sys
import os
import pymongo
from bson.objectid import ObjectId
import json
import MySQLdb
from client import JudgeClient
from language import languages
@@ -11,9 +9,7 @@ from compiler import compile_
from result import result
from settings import judger_workspace
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from judger_controller.settings import celery_mongodb_config, docker_mongodb_config
from settings import submission_db
# 简单的解析命令行参数
@@ -25,19 +21,31 @@ time_limit = args[4]
memory_limit = args[6]
test_case_id = args[8]
connection = pymongo.MongoClient(host=docker_mongodb_config["host"], port=docker_mongodb_config["port"])
collection = connection["oj"]["oj_submission"]
submission = collection.find_one({"_id": ObjectId(submission_id)})
if not submission:
def db_conn():
return MySQLdb.connect(db=submission_db["db"],
user=submission_db["user"],
passwd=submission_db["password"],
host=submission_db["host"],
port=submission_db["port"], charset="utf8")
conn = db_conn()
cur = conn.cursor()
cur.execute("select language, code from submission where id = %s", (submission_id,))
data = cur.fetchall()
if not data:
exit()
connection.close()
language_code = data[0][0]
code = data[0][1]
conn.close()
# 将代码写入文件
language = languages[submission["language"]]
language = languages[language_code]
src_path = judger_workspace + "run/" + language["src_name"]
f = open(src_path, "w")
f.write(submission["code"].encode("utf8"))
f.write(code.encode("utf8"))
f.close()
# 编译
@@ -45,23 +53,23 @@ try:
exe_path = compile_(language, src_path, judger_workspace + "run/")
except Exception as e:
print e
connection = pymongo.MongoClient(host=docker_mongodb_config["host"], port=docker_mongodb_config["port"])
collection = connection["oj"]["oj_submission"]
data = {"result": result["compile_error"], "info": str(e)}
collection.find_one_and_update({"_id": ObjectId(submission_id)}, {"$set": data})
connection.close()
conn = db_conn()
cur = conn.cursor()
cur.execute("update submission set result=%s, info=%s where id=%s",
(result["compile_error"], str(e), submission_id))
conn.commit()
exit()
print "Compile successfully"
# 运行
try:
client = JudgeClient(language_code=submission["language"],
client = JudgeClient(language_code=language_code,
exe_path=exe_path,
max_cpu_time=int(time_limit),
max_real_time=int(time_limit) * 2,
max_memory=int(memory_limit),
test_case_dir=judger_workspace + "test_case/" + test_case_id + "/")
judge_result = {"result": result["accepted"], "info": client.run()}
judge_result = {"result": result["accepted"], "info": client.run(), "accepted_answer_time": None}
for item in judge_result["info"]:
if item["result"]:
@@ -69,15 +77,23 @@ try:
break
else:
l = sorted(judge_result["info"], key=lambda k: k["cpu_time"])
judge_result["accepted_answer_info"] = {"time": l[-1]["cpu_time"]}
judge_result["accepted_answer_time"] = l[-1]["cpu_time"]
except Exception as e:
print e
judge_result = {"result": result["system_error"], "info": str(e)}
conn = db_conn()
cur = conn.cursor()
cur.execute("update submission set result=%s, info=%s where id=%s", (result["system_error"], str(e), submission_id))
conn.commit()
exit()
print "Run successfully"
print judge_result
connection = pymongo.MongoClient(host=docker_mongodb_config["host"], port=docker_mongodb_config["port"])
collection = connection["oj"]["oj_submission"]
collection.find_one_and_update({"_id": ObjectId(submission_id)}, {"$set": judge_result})
connection.close()
conn = db_conn()
cur = conn.cursor()
cur.execute("update submission set result=%s, info=%s, accepted_answer_time=%s where id=%s",
(judge_result["result"], json.dumps(judge_result["info"]), judge_result["accepted_answer_time"],
submission_id))
conn.commit()
conn.close()

View File

@@ -15,3 +15,11 @@ lrun_gid = 1002
judger_workspace = "/var/judger/"
# 这个是在docker 中访问数据库 ip 不一定和web服务器还有celery的一样
submission_db = {
"host": "192.168.59.3",
"port": 3306,
"db": "oj_submission",
"user": "root",
"password": "root"
}

View File

@@ -4,6 +4,4 @@ from celery import Celery
from .settings import redis_config
app = Celery("judge", broker='redis://%s:%s/%s' % (redis_config["host"], redis_config["port"], redis_config["db"]),
include=["judge.judger_controller.tasks"])
Celery().conf.update(CELERY_ACCEPT_CONTENT = ['json'])
include=["judge.judger_controller.tasks"])

View File

@@ -7,7 +7,7 @@ redis_config = {
docker_config = {
"image_name": "d622347336b8",
"image_name": " a7673b55d263",
"docker_path": "docker",
"shell": True
}
@@ -17,16 +17,10 @@ test_case_dir = "/root/test_case/"
source_code_dir = "/root/"
celery_mongodb_config = {
submission_db = {
"host": "127.0.0.1",
"username": "root",
"password": "root",
"port": 27017
}
docker_mongodb_config = {
"host": "192.168.42.1",
"username": "root",
"password": "root",
"port": 27017
"port": 3306,
"db": "oj_submission",
"user": "root",
"password": "root"
}

View File

@@ -1,11 +1,10 @@
# coding=utf-8
# from __future__ import absolute_import
import MySQLdb
import subprocess
import pymongo
from bson import ObjectId
from ..judger.result import result
from ..judger_controller.celery import app
from settings import docker_config, source_code_dir, test_case_dir, celery_mongodb_config
from settings import docker_config, source_code_dir, test_case_dir, submission_db
@app.task
@@ -24,8 +23,16 @@ def judge(submission_id, time_limit, memory_limit, test_case_id):
submission_id, str(time_limit), str(memory_limit), test_case_id)
subprocess.call(command, shell=docker_config["shell"])
except Exception as e:
connection = pymongo.MongoClient(host=celery_mongodb_config["host"], port=celery_mongodb_config["port"])
collection = connection["oj"]["oj_submission"]
data = {"result": result["system_error"], "info": str(e)}
collection.find_one_and_update({"_id": ObjectId(submission_id)}, {"$set": data})
connection.close()
print e
conn = MySQLdb.connect(db=submission_db["db"],
user=submission_db["user"],
passwd=submission_db["password"],
host=submission_db["host"],
port=submission_db["port"],
character="utf8")
cur = conn.cursor()
cur.execute("update submission set result=%s, info=%s where id=%s",
(result["system_error"], str(e), submission_id))
conn.commit()
conn.close()