From 12f96c6cac06d5a49192bdfc15701f196d7bbbaa Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 9 Dec 2015 22:21:54 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/celeryd.conf | 15 --------------- tools/runserver.cmd | 5 ----- tools/runtest.cmd | 12 ------------ tools/supervisord.conf | 26 -------------------------- 4 files changed, 58 deletions(-) delete mode 100644 tools/celeryd.conf delete mode 100644 tools/runserver.cmd delete mode 100644 tools/runtest.cmd delete mode 100644 tools/supervisord.conf diff --git a/tools/celeryd.conf b/tools/celeryd.conf deleted file mode 100644 index 60e3fc21..00000000 --- a/tools/celeryd.conf +++ /dev/null @@ -1,15 +0,0 @@ -[program:celery] -command=celery worker -A judge.judger_controller --loglevel=DEBUG - -directory=/root/qduoj/ -user=root -numprocs=1 -stdout_logfile=/root/log/celery_worker.log -stderr_logfile=/root/log/celery_worker.log -autostart=true -autorestart=true -startsecs=5 - -stopwaitsecs = 6 - -killasgroup=true diff --git a/tools/runserver.cmd b/tools/runserver.cmd deleted file mode 100644 index b458e559..00000000 --- a/tools/runserver.cmd +++ /dev/null @@ -1,5 +0,0 @@ -@echo off -python manage.py runserver -cls -cd.. -python manage.py runserver \ No newline at end of file diff --git a/tools/runtest.cmd b/tools/runtest.cmd deleted file mode 100644 index e6d9ad7b..00000000 --- a/tools/runtest.cmd +++ /dev/null @@ -1,12 +0,0 @@ -@echo off -coverage run --source='.' manage.py test -coverage html -cd htmlcov -index.html -cls -cd.. -coverage run --source='.' manage.py test -coverage html -cd htmlcov -index.html - diff --git a/tools/supervisord.conf b/tools/supervisord.conf deleted file mode 100644 index 1494d7cd..00000000 --- a/tools/supervisord.conf +++ /dev/null @@ -1,26 +0,0 @@ -[unix_http_server] -file=/tmp/supervisor.sock ; path to your socket file - -[supervisord] -logfile=/root/log/supervisord.log ; supervisord log file -logfile_maxbytes=50MB ; maximum size of logfile before rotation -logfile_backups=10 ; number of backed up logfiles -loglevel=info ; info, debug, warn, trace -pidfile=/root/log/supervisord.pid ; pidfile location -nodaemon=false ; run supervisord as a daemon -minfds=1024 ; number of startup file descriptors -minprocs=200 ; number of process descriptors -user=root ; default user -childlogdir=/root/log/ ; where child log files will live - - -[rpcinterface:supervisor] -supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface - -[supervisorctl] -serverurl=unix:///tmp/supervisor.sock ; use unix:// schem for a unix sockets. - - -[include] - -files=*.conf \ No newline at end of file From e32712be834d18d6f002fc5b44bc38ce449ec7f3 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 9 Dec 2015 22:22:26 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=A4=E9=A2=98?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dockerfiles/judger/Dockerfile | 3 ++- oj/settings.py | 5 ----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/dockerfiles/judger/Dockerfile b/dockerfiles/judger/Dockerfile index 6090728e..f13a9a5d 100644 --- a/dockerfiles/judger/Dockerfile +++ b/dockerfiles/judger/Dockerfile @@ -19,4 +19,5 @@ RUN cd lrun && make install RUN mkdir -p /var/judger/run/ && mkdir /var/judger/test_case/ && mkdir /var/judger/code/ RUN chmod -R 777 /var/judger/run/ COPY policy /var/judger/run/ -WORKDIR /var/judger/code/ \ No newline at end of file +WORKDIR /var/judger/code/judge/ +CMD python server.py \ No newline at end of file diff --git a/oj/settings.py b/oj/settings.py index 1d3cc378..ac766975 100644 --- a/oj/settings.py +++ b/oj/settings.py @@ -22,11 +22,6 @@ if ENV == "local": elif ENV == "server": from .server_settings import * -BROKER_BACKEND = "redis" -CELERY_ACCEPT_CONTENT = ['json'] -CELERY_TASK_SERIALIZER = 'json' -CELERY_RESULT_SERIALIZER = 'json' - BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) From 7d72d6ec334401cd5f08ee552e81c19479035667 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 10 Dec 2015 10:35:32 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E5=8D=9A=E5=AE=A2?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=89=8D=E9=9D=A2=E4=B9=9F=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=9B=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/src/oj/account/user_index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/src/oj/account/user_index.html b/template/src/oj/account/user_index.html index 7a481aa6..6d34593d 100644 --- a/template/src/oj/account/user_index.html +++ b/template/src/oj/account/user_index.html @@ -31,7 +31,7 @@

{% endif %} {% if user.userprofile.blog %} -

+

{{ blog_link }}

{% endif %} From b973dfdfda540adad78ea8465c2886f7e602012f Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 10 Dec 2015 19:29:21 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E8=A7=84=E8=8C=83=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=86=99=E6=B3=95=EF=BC=9B=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=94=A8=E6=88=B7=E5=90=8D=E4=B9=9F=E5=9C=A8=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E4=B8=AD=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oj/local_settings.py | 2 +- oj/server_settings.py | 5 ++--- oj/settings.py | 6 +----- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/oj/local_settings.py b/oj/local_settings.py index 82bfe2ed..794a7b63 100644 --- a/oj/local_settings.py +++ b/oj/local_settings.py @@ -39,4 +39,4 @@ STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/src/"), BASE_DIR] # 模板文件夹 TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'template/src/')] -SSO = {"callback": "http://localhost:8765/login"} \ No newline at end of file +SSO = {"callback": "http://localhost:8765/login"} diff --git a/oj/server_settings.py b/oj/server_settings.py index cd86c16f..afec6d19 100644 --- a/oj/server_settings.py +++ b/oj/server_settings.py @@ -11,7 +11,7 @@ DATABASES = { 'CONN_MAX_AGE': 0.1, 'HOST': os.environ.get("MYSQL_PORT_3306_TCP_ADDR", "127.0.0.1"), 'PORT': 3306, - 'USER': 'root', + 'USER': os.environ.get("MYSQL_ENV_MYSQL_USER", "root"), 'PASSWORD': os.environ.get("MYSQL_ENV_MYSQL_ROOT_PASSWORD", "root") }, 'submission': { @@ -20,7 +20,7 @@ DATABASES = { 'CONN_MAX_AGE': 0.1, 'HOST': os.environ.get("MYSQL_PORT_3306_TCP_ADDR", "127.0.0.1"), 'PORT': 3306, - 'USER': 'root', + 'USER': os.environ.get("MYSQL_ENV_MYSQL_USER", "root"), 'PASSWORD': os.environ.get("MYSQL_ENV_MYSQL_ROOT_PASSWORD", "root") } } @@ -37,7 +37,6 @@ REDIS_QUEUE = { "db": 2 } - DEBUG = False ALLOWED_HOSTS = ['*'] diff --git a/oj/settings.py b/oj/settings.py index ac766975..f5857033 100644 --- a/oj/settings.py +++ b/oj/settings.py @@ -22,7 +22,6 @@ if ENV == "local": elif ENV == "server": from .server_settings import * - BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -187,19 +186,16 @@ WEBSITE_INFO = {"website_name": "qduoj", "website_footer": u"青岛大学信息工程学院 创新实验室 京ICP备15062075号-1", "url": "https://qduoj.com"} - HUEY = { 'backend': 'huey.backends.redis_backend', 'name': 'task_queue', 'connection': {'host': REDIS_QUEUE["host"], 'port': REDIS_QUEUE["port"], 'db': REDIS_QUEUE["db"]}, - 'always_eager': False, # Defaults to False when running via manage.py run_huey + 'always_eager': False, # Defaults to False when running via manage.py run_huey # Options to pass into the consumer when running ``manage.py run_huey`` 'consumer_options': {'workers': 50}, } - SMTP_CONFIG = {"smtp_server": "smtp.mxhichina.com", "email": "noreply@qduoj.com", "password": os.environ.get("smtp_password", "111111"), "tls": False} - From 932d2ea3e77ef81a06cb57cbc4f4eee2567590cc Mon Sep 17 00:00:00 2001 From: sxw Date: Thu, 10 Dec 2015 20:24:30 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dtypo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group/views.py b/group/views.py index 5c2797b1..83b430c3 100644 --- a/group/views.py +++ b/group/views.py @@ -22,7 +22,7 @@ class GroupAPIViewBase(object): def get_group(self, request, group_id): """ 根据group_id查询指定的小组的信息,结合判断用户权限 - 管理员可以查询所有的小组,其他用户查询自己创建的自傲组 + 管理员可以查询所有的小组,其他用户查询自己创建的小组 """ if request.user.admin_type == SUPER_ADMIN: group = Group.objects.get(id=group_id) From a91d2fb4f144662b441d9aa8e53715edee45f46b Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Fri, 11 Dec 2015 14:24:05 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E5=AE=8C=E5=96=84=20sso=20=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/account/views.py b/account/views.py index 5ca7612a..b02c880a 100644 --- a/account/views.py +++ b/account/views.py @@ -350,7 +350,9 @@ class SSOAPIView(APIView): if serializer.is_valid(): try: user = User.objects.get(auth_token=serializer.data["token"]) - return success_response({"username": user.username}) + user.auth_token = None + user.save() + return success_response({"username": user.username, "admin_type": user.admin_type, "avatar": user.userprofile.avatar}) except User.DoesNotExist: return error_response(u"用户不存在") else: From c0a148b6f5350a8fa655a3fcd1f1236942030eee Mon Sep 17 00:00:00 2001 From: sxw Date: Fri, 11 Dec 2015 22:42:36 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dtypo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/src/js/app/oj/account/resetPassword.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/src/js/app/oj/account/resetPassword.js b/static/src/js/app/oj/account/resetPassword.js index ef1128d0..f4be55a5 100644 --- a/static/src/js/app/oj/account/resetPassword.js +++ b/static/src/js/app/oj/account/resetPassword.js @@ -2,8 +2,8 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c var applied_captcha = false; $('form').validator().on('submit', function (e) { if (!e.isDefaultPrevented()) { - var loca = location.href.split("/"); - var token = loca[loca.length-2]; + var splited_location = location.href.split("/"); + var token = splited_location[splited_location.length-2]; var captcha = $("#captcha").val(); var password = $("#new_password").val(); $.ajax({ From 84d390362bafc8a369cfa917b6ed43eabaddb610 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Sat, 12 Dec 2015 11:36:02 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20huey=20=E9=98=9F?= =?UTF-8?q?=E5=88=97=E4=B8=8D=E4=BC=9A=E9=87=8A=E6=94=BE=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E6=98=AF=E7=94=A8=E6=B3=95=E4=B8=8D=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- submission/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/submission/tasks.py b/submission/tasks.py index 25476e36..0e958e7b 100644 --- a/submission/tasks.py +++ b/submission/tasks.py @@ -1,9 +1,9 @@ # coding=utf-8 -from huey.djhuey import task +from huey.djhuey import db_task from judge_dispatcher.tasks import JudgeDispatcher -@task() +@db_task() def _judge(submission, time_limit, memory_limit, test_case_id, is_waiting_task=False): JudgeDispatcher(submission, time_limit, memory_limit, test_case_id).judge(is_waiting_task) \ No newline at end of file From b687d2067b28f6759a47ddcfe7bb575e8f83f679 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Sat, 12 Dec 2015 15:41:57 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E5=BA=9F=E5=BC=83=20huey=EF=BC=8C?= =?UTF-8?q?=E5=A4=9A=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E5=AD=98=E5=9C=A8=20connection=20=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E9=87=8A=E6=94=BE=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E5=9B=9E=E5=88=B0=20celery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- account/tasks.py | 8 +++++ account/views.py | 37 ++++++++++++---------- db1.sqlite3 | 0 dockerfiles/oj_web_server/requirements.txt | 3 +- dockerfiles/oj_web_server/task_queue.conf | 4 +-- judge_dispatcher/tasks.py | 4 +-- oj/__init__.py | 5 +++ oj/celery.py | 19 +++++++++++ oj/local_settings.py | 6 ++++ oj/server_settings.py | 6 ++++ oj/settings.py | 15 +++------ submission/tasks.py | 10 +++--- submission/views.py | 6 ++-- 14 files changed, 85 insertions(+), 40 deletions(-) create mode 100644 account/tasks.py delete mode 100644 db1.sqlite3 create mode 100644 oj/celery.py diff --git a/.gitignore b/.gitignore index e0873616..8707498f 100644 --- a/.gitignore +++ b/.gitignore @@ -52,7 +52,7 @@ db.db #redis dump *.rdb #*.out -db.sqlite3 +*.sqlite3 .DS_Store log/ static/release/css diff --git a/account/tasks.py b/account/tasks.py new file mode 100644 index 00000000..58cb8fc1 --- /dev/null +++ b/account/tasks.py @@ -0,0 +1,8 @@ +# coding=utf-8 +from celery import shared_task +from utils.mail import send_email + + +@shared_task +def _send_email(from_name, to_email, to_name, subject, content): + send_email(from_name, to_email, to_name, subject, content) \ No newline at end of file diff --git a/account/views.py b/account/views.py index b02c880a..502e0363 100644 --- a/account/views.py +++ b/account/views.py @@ -14,7 +14,7 @@ from rest_framework.response import Response from utils.shortcuts import (serializer_invalid_response, error_response, success_response, error_page, paginate, rand_str) from utils.captcha import Captcha -from utils.mail import send_email +from .tasks import _send_email from .decorators import login_required from .models import User, UserProfile @@ -63,7 +63,7 @@ def index_page(request): return render(request, "oj/index.html") if request.META.get('HTTP_REFERER') or request.GET.get("index"): - return render(request, "oj/index.html") + return render(request, "oj/index.html") else: return http.HttpResponseRedirect('/problems/') @@ -151,9 +151,9 @@ class EmailCheckAPIView(APIView): 检测邮箱是否存在,用状态码标识结果 --- """ - #这里是为了适应前端表单验证空间的要求 + # 这里是为了适应前端表单验证空间的要求 reset = request.GET.get("reset", None) - #如果reset为true说明该请求是重置密码页面发出的,要返回的状态码应正好相反 + # 如果reset为true说明该请求是重置密码页面发出的,要返回的状态码应正好相反 if reset: existed = 200 does_not_existed = 400 @@ -287,22 +287,25 @@ class ApplyResetPasswordAPIView(APIView): user = User.objects.get(email=data["email"]) except User.DoesNotExist: return error_response(u"用户不存在") - if user.reset_password_token_create_time and (now() - user.reset_password_token_create_time).total_seconds() < 20 * 60: + if user.reset_password_token_create_time and ( + now() - user.reset_password_token_create_time).total_seconds() < 20 * 60: return error_response(u"20分钟内只能找回一次密码") user.reset_password_token = rand_str() user.reset_password_token_create_time = now() user.save() - email_template = codecs.open(settings.TEMPLATES[0]["DIRS"][0] + "utils/reset_password_email.html", "r", "utf-8").read() + email_template = codecs.open(settings.TEMPLATES[0]["DIRS"][0] + "utils/reset_password_email.html", "r", + "utf-8").read() - email_template = email_template.replace("{{ username }}", user.username).\ - replace("{{ website_name }}", settings.WEBSITE_INFO["website_name"]).\ - replace("{{ link }}", request.scheme + "://" + request.META['HTTP_HOST'] + "/reset_password/t/" + user.reset_password_token) + email_template = email_template.replace("{{ username }}", user.username). \ + replace("{{ website_name }}", settings.WEBSITE_INFO["website_name"]). \ + replace("{{ link }}", request.scheme + "://" + request.META[ + 'HTTP_HOST'] + "/reset_password/t/" + user.reset_password_token) - send_email(settings.WEBSITE_INFO["website_name"], - user.email, - user.username, - settings.WEBSITE_INFO["website_name"] + u" 登录信息找回邮件", - email_template) + _send_email.delay(settings.WEBSITE_INFO["website_name"], + user.email, + user.username, + settings.WEBSITE_INFO["website_name"] + u" 登录信息找回邮件", + email_template) return success_response(u"邮件发送成功,请前往您的邮箱查收") else: return serializer_invalid_response(serializer) @@ -352,7 +355,8 @@ class SSOAPIView(APIView): user = User.objects.get(auth_token=serializer.data["token"]) user.auth_token = None user.save() - return success_response({"username": user.username, "admin_type": user.admin_type, "avatar": user.userprofile.avatar}) + return success_response( + {"username": user.username, "admin_type": user.admin_type, "avatar": user.userprofile.avatar}) except User.DoesNotExist: return error_response(u"用户不存在") else: @@ -366,7 +370,8 @@ class SSOAPIView(APIView): token = rand_str() request.user.auth_token = token request.user.save() - return render(request, "oj/account/sso.html", {"redirect_url": callback + "?token=" + token, "callback": callback}) + return render(request, "oj/account/sso.html", + {"redirect_url": callback + "?token=" + token, "callback": callback}) def reset_password_page(request, token): diff --git a/db1.sqlite3 b/db1.sqlite3 deleted file mode 100644 index e69de29b..00000000 diff --git a/dockerfiles/oj_web_server/requirements.txt b/dockerfiles/oj_web_server/requirements.txt index 56676a92..02d03d5c 100644 --- a/dockerfiles/oj_web_server/requirements.txt +++ b/dockerfiles/oj_web_server/requirements.txt @@ -11,4 +11,5 @@ supervisor pillow jsonfield Envelopes -huey \ No newline at end of file +celery +djcelery \ No newline at end of file diff --git a/dockerfiles/oj_web_server/task_queue.conf b/dockerfiles/oj_web_server/task_queue.conf index 39f837e0..5a9b13f7 100644 --- a/dockerfiles/oj_web_server/task_queue.conf +++ b/dockerfiles/oj_web_server/task_queue.conf @@ -1,6 +1,6 @@ -[program:mq] +[program:task_queue] -command=python manage.py run_huey +command=python manage.py celeryd -B -l DEBUG directory=/code/ user=root diff --git a/judge_dispatcher/tasks.py b/judge_dispatcher/tasks.py index 244c9074..4f1b366a 100644 --- a/judge_dispatcher/tasks.py +++ b/judge_dispatcher/tasks.py @@ -32,7 +32,7 @@ class JudgeDispatcher(object): if servers.exists(): return servers.first() - def judge(self, is_waiting_task=False): + def judge(self): self.submission.judge_start_time = int(time.time() * 1000) with transaction.atomic(): @@ -89,7 +89,7 @@ class JudgeDispatcher(object): submission = Submission.objects.get(id=waiting_submission.submission_id) waiting_submission.delete() - _judge(submission, time_limit=waiting_submission.time_limit, + _judge.delay(submission, time_limit=waiting_submission.time_limit, memory_limit=waiting_submission.memory_limit, test_case_id=waiting_submission.test_case_id, is_waiting_task=True) diff --git a/oj/__init__.py b/oj/__init__.py index f4206374..44aca8db 100644 --- a/oj/__init__.py +++ b/oj/__init__.py @@ -7,3 +7,8 @@ |___/ |___/ |_| https://github.com/QingdaoU/OnlineJudge """ +from __future__ import absolute_import + +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app \ No newline at end of file diff --git a/oj/celery.py b/oj/celery.py new file mode 100644 index 00000000..f20f9dc8 --- /dev/null +++ b/oj/celery.py @@ -0,0 +1,19 @@ +from __future__ import absolute_import + +import os + +from celery import Celery + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oj.settings') + +from django.conf import settings + +app = Celery('oj') + +# Using a string here means the worker will not have to +# pickle the object when using Windows. +app.config_from_object('django.conf:settings') + +# load task modules from all registered Django app configs. +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) \ No newline at end of file diff --git a/oj/local_settings.py b/oj/local_settings.py index 794a7b63..8fa3b8ac 100644 --- a/oj/local_settings.py +++ b/oj/local_settings.py @@ -28,6 +28,12 @@ REDIS_QUEUE = { "db": 2 } + +# for celery +BROKER_URL = 'redis://%s:%s/%s' % (REDIS_QUEUE["host"], str(REDIS_QUEUE["port"]), str(REDIS_QUEUE["db"])) +ACCEPT_CONTENT = ['json'] + + DEBUG = True ALLOWED_HOSTS = [] diff --git a/oj/server_settings.py b/oj/server_settings.py index afec6d19..98eae980 100644 --- a/oj/server_settings.py +++ b/oj/server_settings.py @@ -37,6 +37,12 @@ REDIS_QUEUE = { "db": 2 } + +# for celery +BROKER_URL = 'redis://%s:%s/%s' % (REDIS_QUEUE["host"], str(REDIS_QUEUE["port"]), str(REDIS_QUEUE["db"])) +ACCEPT_CONTENT = ['json'] + + DEBUG = False ALLOWED_HOSTS = ['*'] diff --git a/oj/settings.py b/oj/settings.py index f5857033..97424d0d 100644 --- a/oj/settings.py +++ b/oj/settings.py @@ -10,7 +10,7 @@ https://docs.djangoproject.com/en/1.8/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.8/ref/settings/ """ - +from __future__ import absolute_import # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os @@ -22,6 +22,9 @@ if ENV == "local": elif ENV == "server": from .server_settings import * +import djcelery +djcelery.setup_loader() + BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -53,7 +56,7 @@ INSTALLED_APPS = ( 'judge_dispatcher', 'rest_framework', - 'huey.djhuey', + 'djcelery', ) if DEBUG: @@ -186,14 +189,6 @@ WEBSITE_INFO = {"website_name": "qduoj", "website_footer": u"青岛大学信息工程学院 创新实验室 京ICP备15062075号-1", "url": "https://qduoj.com"} -HUEY = { - 'backend': 'huey.backends.redis_backend', - 'name': 'task_queue', - 'connection': {'host': REDIS_QUEUE["host"], 'port': REDIS_QUEUE["port"], 'db': REDIS_QUEUE["db"]}, - 'always_eager': False, # Defaults to False when running via manage.py run_huey - # Options to pass into the consumer when running ``manage.py run_huey`` - 'consumer_options': {'workers': 50}, -} SMTP_CONFIG = {"smtp_server": "smtp.mxhichina.com", "email": "noreply@qduoj.com", diff --git a/submission/tasks.py b/submission/tasks.py index 0e958e7b..4267841a 100644 --- a/submission/tasks.py +++ b/submission/tasks.py @@ -1,9 +1,9 @@ # coding=utf-8 -from huey.djhuey import db_task - +from __future__ import absolute_import +from celery import shared_task from judge_dispatcher.tasks import JudgeDispatcher -@db_task() -def _judge(submission, time_limit, memory_limit, test_case_id, is_waiting_task=False): - JudgeDispatcher(submission, time_limit, memory_limit, test_case_id).judge(is_waiting_task) \ No newline at end of file +@shared_task +def _judge(submission, time_limit, memory_limit, test_case_id): + JudgeDispatcher(submission, time_limit, memory_limit, test_case_id).judge() \ No newline at end of file diff --git a/submission/views.py b/submission/views.py index ccda8acb..41f5e3b5 100644 --- a/submission/views.py +++ b/submission/views.py @@ -43,7 +43,7 @@ class SubmissionAPIView(APIView): problem_id=problem.id) try: - _judge(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) + _judge.delay(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) except Exception as e: logger.error(e) return error_response(u"提交判题任务失败") @@ -88,7 +88,7 @@ class ContestSubmissionAPIView(APIView): code=data["code"], problem_id=problem.id) try: - _judge(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) + _judge.delay(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) except Exception as e: logger.error(e) return error_response(u"提交判题任务失败") @@ -273,7 +273,7 @@ class SubmissionRejudgeAdminAPIView(APIView): except Problem.DoesNotExist: return error_response(u"题目不存在") try: - _judge(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) + _judge.delay(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) except Exception as e: logger.error(e) return error_response(u"提交判题任务失败") From d7d597cc23d9439be0c0d7bc7d2c6ff94f32bb92 Mon Sep 17 00:00:00 2001 From: virusdefender Date: Sat, 12 Dec 2015 15:59:58 +0800 Subject: [PATCH 10/10] fix error package name and add pip mirrorwq --- dockerfiles/oj_web_server/Dockerfile | 4 ++-- dockerfiles/oj_web_server/requirements.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dockerfiles/oj_web_server/Dockerfile b/dockerfiles/oj_web_server/Dockerfile index 07a73e9f..93623f75 100644 --- a/dockerfiles/oj_web_server/Dockerfile +++ b/dockerfiles/oj_web_server/Dockerfile @@ -3,6 +3,6 @@ ENV PYTHONBUFFERED 1 RUN mkdir -p /code/log /code/test_case /code/upload WORKDIR /code ADD requirements.txt /code/ -RUN pip install -r requirements.txt +RUN pip install -i http://pypi.douban.com/simple -r requirements.txt --trusted-host pypi.douban.com EXPOSE 8010 -CMD supervisord \ No newline at end of file +CMD supervisord diff --git a/dockerfiles/oj_web_server/requirements.txt b/dockerfiles/oj_web_server/requirements.txt index 02d03d5c..b371a350 100644 --- a/dockerfiles/oj_web_server/requirements.txt +++ b/dockerfiles/oj_web_server/requirements.txt @@ -12,4 +12,4 @@ pillow jsonfield Envelopes celery -djcelery \ No newline at end of file +django-celery