Add basic celery support and demo tasks

This commit is contained in:
Quantum 2019-04-27 20:54:02 -04:00 committed by Guanzhong Chen
parent e940236258
commit 8798827dde
8 changed files with 59 additions and 1 deletions

View File

@ -0,0 +1 @@
from dmoj.celery import app as celery_app

12
dmoj/celery.py Normal file
View File

@ -0,0 +1,12 @@
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dmoj.settings')
app = Celery('dmoj')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

View File

@ -13,7 +13,7 @@ from judge.sitemap import ProblemSitemap, UserSitemap, HomePageSitemap, UrlSitem
BlogPostSitemap, SolutionSitemap
from judge.views import TitledTemplateView
from judge.views import organization, language, status, blog, problem, mailgun, license, register, user, \
submission, widgets, comment, contests, api, ranked_submission, stats, preview, ticket, totp
submission, widgets, comment, contests, api, ranked_submission, stats, preview, ticket, totp, tasks
from judge.views.problem_data import ProblemDataView, ProblemSubmissionDiff, \
problem_data_file, problem_init_view
from judge.views.register import RegistrationView, ActivationView
@ -323,6 +323,11 @@ urlpatterns = [
url(r'^contest/$', ContestSelect2View.as_view(), name='contest_select2'),
url(r'^comment/$', CommentSelect2View.as_view(), name='comment_select2'),
])),
url(r'^tasks/', include([
url(r'^success$', tasks.demo_success),
url(r'^failure$', tasks.demo_failure),
])),
]
favicon_paths = ['apple-touch-icon-180x180.png', 'apple-touch-icon-114x114.png', 'android-chrome-72x72.png',

8
dmoj_celery.py Normal file
View File

@ -0,0 +1,8 @@
try:
import MySQLdb
except ImportError:
import pymysql
pymysql.install_as_MySQLdb()
# noinspection PyUnresolvedReferences
from dmoj.celery import app

1
judge/tasks/__init__.py Normal file
View File

@ -0,0 +1 @@
from judge.tasks.demo import *

13
judge/tasks/demo.py Normal file
View File

@ -0,0 +1,13 @@
from celery import shared_task
__all__ = ('success', 'failure')
@shared_task
def success():
pass
@shared_task
def failure():
raise RuntimeError('This task always fails.')

17
judge/views/tasks.py Normal file
View File

@ -0,0 +1,17 @@
from functools import partial
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from judge.tasks import success, failure
def demo_task(request, task):
if not request.user.is_superuser:
raise PermissionDenied()
result = task.delay()
return HttpResponse('Task %s scheduled.' % (result.id,))
demo_success = partial(demo_task, task=success)
demo_failure = partial(demo_task, task=failure)

View File

@ -27,3 +27,4 @@ qrcode[pil]
jsonfield
pymoss
packaging
celery