diff --git a/hangul_tutor/__init__.py b/hangul_tutor/__init__.py index 044e667..2e2429e 100644 --- a/hangul_tutor/__init__.py +++ b/hangul_tutor/__init__.py @@ -1 +1,4 @@ from .celery import app as celery_app # noqa: F401 + + +__all__ = ("celery_app",) diff --git a/hangul_tutor/celery.py b/hangul_tutor/celery.py index 7284d45..afbb2a9 100644 --- a/hangul_tutor/celery.py +++ b/hangul_tutor/celery.py @@ -3,19 +3,28 @@ import time import celery -from django.conf import settings +# Set the default Django settings module for the 'celery' program. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hangul_tutor.settings") + +# Instantiate Celery once per Django start. app = celery.Celery("hangul_tutor") -app.config_from_object("django.conf:settings") -app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) + +# Using a string here means the worker doesn't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object("django.conf:settings", namespace="CELERY") + +# Load task modules from all registered Django apps. +app.autodiscover_tasks() logger = logging.getLogger(__name__) -@app.task(bind=True) +@app.task(bind=True, ignore_result=True) def debug_task(self): logger.info("Starting debug task") time.sleep(5) diff --git a/hangul_tutor/settings.py b/hangul_tutor/settings.py index 8ac7016..ecac968 100644 --- a/hangul_tutor/settings.py +++ b/hangul_tutor/settings.py @@ -203,18 +203,21 @@ } # CELERY STUFF -BROKER_URL = os.environ.get( +CELERY_BROKER_URL = os.environ.get( "CELERY_BROKER_URL", "redis://localhost:6379", ) CELERY_RESULT_BACKEND = os.environ.get( "CELERY_RESULT_BACKEND", - BROKER_URL, + CELERY_BROKER_URL, ) CELERY_ACCEPT_CONTENT = ["application/json"] CELERY_TASK_SERIALIZER = "json" CELERY_RESULT_SERIALIZER = "json" CELERY_TIMEZONE = "UTC" +CELERY_TASK_TRACK_STARTED = True +CELERY_TASK_TIME_LIMIT = 30 * 60 +CELERY_TASK_SOFT_TIME_LIMIT = 20 * 60 AUTHENTICATION_BACKENDS = [ # Needed to login by username in Django admin, regardless of `allauth`