使用 redis 消息队列来传递题目的结果。从而更新题目 ac 和 ts 计数器

This commit is contained in:
virusdefender 2015-08-22 12:56:22 +08:00
parent 46d2ea35c2
commit c12c227ee9
7 changed files with 44 additions and 13 deletions

View File

@ -1,5 +1,5 @@
# coding=utf-8
import datetime
import json
import redis
import MySQLdb
import subprocess

1
mq/__init__.py Normal file
View File

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

1
mq/models.py Normal file
View File

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

1
mq/scripts/__init__.py Normal file
View File

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

37
mq/scripts/info.py Normal file
View File

@ -0,0 +1,37 @@
# coding=utf-8
import json
import redis
from judge.judger_controller.settings import redis_config
from judge.judger.result import result
from submission.models import Submission
from problem.models import Problem
class MessageQueue(object):
def __init__(self):
self.conn = redis.StrictRedis(host=redis_config["host"], port=redis_config["port"], db=redis_config["db"])
self.queue = 'queue'
def listen_task(self):
while True:
submission_id = self.conn.blpop(self.queue, 0)[1]
print submission_id
try:
submission = Submission.objects.get(id=submission_id)
except Submission.DoesNotExist:
print "error 1"
pass
if submission.result == result["accepted"]:
# 更新题目的 ac 计数器
try:
problem = Problem.objects.get(id=submission.problem_id)
problem.total_accepted_number += 1
problem.save()
except Problem.DoesNotExist:
print "error 2"
pass
print "mq running"
MessageQueue().listen_task()

View File

@ -52,7 +52,10 @@ INSTALLED_APPS = (
'problem',
'admin',
'submission',
'mq',
'django_extensions',
'rest_framework',
'rest_framework_swagger',
)

View File

@ -60,18 +60,6 @@ class SubmissionAPIView(APIView):
submission = Submission.objects.get(id=submission_id, user_id=request.user.id)
except Submission.DoesNotExist:
return error_response(u"提交不存在")
# 标记这个submission 已经被统计
if not submission.is_counted:
submission.is_counted = True
submission.save()
if submission.result == result["accepted"]:
# 更新题目的 ac 计数器
try:
problem = Problem.objects.get(id=submission.problem_id)
problem.total_accepted_number += 1
problem.save()
except Problem.DoesNotExist:
pass
response_data = {"result": submission.result}
if submission.result == 0:
response_data["accepted_answer_time"] = submission.accepted_answer_time