diff --git a/chess_backend/__init__.py b/chess_backend/__init__.py index e69de29..5568b6d 100644 --- a/chess_backend/__init__.py +++ b/chess_backend/__init__.py @@ -0,0 +1,5 @@ +# 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 + +__all__ = ("celery_app",) diff --git a/chess_backend/celery.py b/chess_backend/celery.py new file mode 100644 index 0000000..20f73ee --- /dev/null +++ b/chess_backend/celery.py @@ -0,0 +1,21 @@ +import os + +from celery import Celery + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chess_backend.settings") + +app = Celery("chess_backend") + +# 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() + + +@app.task(bind=True, ignore_result=True) +def debug_task(self): + print(f"Request: {self.request!r}") diff --git a/chess_backend/settings.py b/chess_backend/settings.py index a426581..712aa2c 100644 --- a/chess_backend/settings.py +++ b/chess_backend/settings.py @@ -124,3 +124,9 @@ LOGGING = { }, }, } + +# Celery Configuration Options +CELERY_TIMEZONE = "Europe/Moscow" +CELERY_TASK_TRACK_STARTED = True +CELERY_TASK_TIME_LIMIT = 30 * 60 +CELERY_BROKER_URL = 'redis://localhost:6379/0' diff --git a/requirements/base.txt b/requirements/base.txt index af49290..c274de8 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -4,3 +4,5 @@ djangorestframework==3.13.1 Pillow==9.1.1 PyJWT==2.4.0 channels-redis==3.4.1 +celery==5.2.7 +redis==4.3.4 \ No newline at end of file diff --git a/room/tasks.py b/room/tasks.py new file mode 100644 index 0000000..c41f66a --- /dev/null +++ b/room/tasks.py @@ -0,0 +1,3 @@ +from celery import shared_task + +# TODO: add timeout for state