-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adhocracy-plus/config: adds support for celery task queues
- Loading branch information
Showing
6 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,8 @@ | |
"apps.polls", | ||
"apps.topicprio", | ||
"apps.debate", | ||
# Celery | ||
"celery", | ||
) | ||
|
||
MIDDLEWARE = ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters