Skip to content

Commit

Permalink
adhocracy-plus/config: adds support for celery task queues
Browse files Browse the repository at this point in the history
  • Loading branch information
hklarner committed Sep 5, 2023
1 parent 3e303b3 commit 9216143
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions adhocracy-plus/config/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "adhocracy-plus.config.settings")


class Config:
broker_url = "redis://localhost:6379"
result_backend = "redis"
broker_connection_retry_on_startup = True


celery = Celery(main="adhocracy-plus")
celery.config_from_object(Config)
celery.autodiscover_tasks()


@celery.task
def celery_dummy_task():
print("hello")

return "world"
2 changes: 2 additions & 0 deletions adhocracy-plus/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
"apps.polls",
"apps.topicprio",
"apps.debate",
# Celery
"celery",
)

MIDDLEWARE = (
Expand Down
2 changes: 2 additions & 0 deletions apps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib
import logging

logger = logging.getLogger(__name__)
celery = getattr(importlib.import_module("adhocracy-plus.config.celery"), "celery")
4 changes: 4 additions & 0 deletions changelog/7601.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Added

- adds support for celery task queues with a redis message broker

21 changes: 21 additions & 0 deletions docs/celery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

## Background

We want to upgrade Django from the current version to at least 4. But our current approach to running background tasks, namely `django-background-tasks` is no longer supported in Django 4. Hence, we decided to switch to celery for distributed tasks.


## Developer Notes

The celery configuration file is `adhocracy-plus/config/celery.py`. The celery app is identically configured for all environments and for simplicity we use a `Config` class inside `celery.py` instead of namespaced variables in Django settings files.

Celery is set up to autodiscover tasks. To define a celery task simply import the celery app and use it to decorate the task function. Since the celery config file is located in a directory that uses a hyphen (`adhocracy-plus`) we use the `importlib` to import the app. For convenience, you can import celery from apps:

```python
from apps import celery

@celery.task
def celery_dummy_task():
print("hello")

return "world"
```
4 changes: 4 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ easy-thumbnails[svg]==2.8.5
jsonfield==3.1.0
python-dateutil==2.8.2
rules==3.3

# celery
celery==5.3.1
redis==5.0.0

0 comments on commit 9216143

Please sign in to comment.