Skip to content

Commit

Permalink
replaces background tasks with celery tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
hklarner committed Sep 14, 2023
1 parent eb089f4 commit 80f4d5b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 49 deletions.
9 changes: 9 additions & 0 deletions adhocracy-plus/config/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ def dummy_task():
print(result)

return result


@celery_app.task(name="crash_task")
def crash_task():
"""
This task is for testing purposes only.
"""

1 / 0
1 change: 0 additions & 1 deletion adhocracy-plus/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"easy_thumbnails",
"ckeditor",
"ckeditor_uploader",
"background_task",
"parler",
# Wagtail cms components
"wagtail.contrib.forms",
Expand Down
21 changes: 0 additions & 21 deletions apps/contrib/management/commands/errored_task_notification.py

This file was deleted.

2 changes: 1 addition & 1 deletion apps/notifications/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def send_no_object(cls, object, *args, **kwargs):
),
"organisation_id": organisation.id,
}
tasks.send_async_no_object(
tasks.send_async_no_object.delay(
cls.__module__, cls.__name__, object_dict, args, kwargs
)
return []
Expand Down
4 changes: 2 additions & 2 deletions apps/projects/tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import importlib

from background_task import background
from celery import shared_task


@background(schedule=1)
@shared_task
def send_async_no_object(email_module_name, email_class_name, object, args, kwargs):
mod = importlib.import_module(email_module_name)
cls = getattr(mod, email_class_name)
Expand Down
28 changes: 18 additions & 10 deletions docs/installation_prod.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ SECURE_CONTENT_TYPE_NOSNIFF = True
SESSION_COOKIE_HTTPONLY = True
FILE_UPLOAD_PERMISSIONS = 0o644
# celery configuration
CELERY_BROKER_URL = "redis://localhost:6379"
CELERY_RESULT_BACKEND = "redis://localhost:6379"
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True
```

#### Populate database

This will create all required tables via so called **migrations**
This will create all required tables via so-called **migrations**

```
python manage.py migrate
Expand All @@ -117,7 +122,7 @@ Cancel the server after testing via `ctrl`+`c`

### Run the server as system daemon

In order to start up the software as a regular system daemon, similar to a database or webserver, we need to create unit files.
In order to start up the software as a regular system daemon, similar to a database or webserver, we need to create service files.

`/etc/systemd/system/adhocracy-plus.service`:

Expand All @@ -139,26 +144,29 @@ StandardError=inherit
WantedBy=default.target
```

`/etc/systemd/system/adhocracy-plus-background-task.service`:
`/etc/systemd/system/adhocracy-plus-celery-worker.service`:

```
[Unit]
Description=adhocracy+ background task
Description=adhocracy+ celery worker
After=network.target
[Service]
Type=forking
User=aplus
WorkingDirectory=/home/aplus/adhocracy-plus
ExecStart=/home/aplus/.virtualenvs/aplus/bin/python manage.py process_tasks --settings adhocracy-plus.config.settings.production --sleep 5
ExecStart=/home/aplus/.virtualenvs/aplus/bin/celery --app adhocracy-plus worker
Restart=always
RestartSec=3
StandardOutput=append:/var/log/adhocracy-plus/adhocracy-plus-background-task.log
StandardOutput=append:/var/log/adhocracy-plus/adhocracy-plus-celery-worker.log
StandardError=inherit
[Install]
WantedBy=default.target
```

Depending on your celery configuration you will also need to start a message broker service like redis or rabbit-mq and configure celery accordingly in `local.py` (see above). If you use redis and the default installation it should already be running, call `service redis status` to check.

This will log all output to files in `/var/log/adhocracy-plus/`. You will also need to create that folder before starting the service (as `root` or using `sudo`):

```
Expand All @@ -170,14 +178,14 @@ Load and start units (as `root` or using `sudo`):
```
systemctl daemon-reload
systemctl start adhocracy-plus
systemctl start adhocracy-plus-background-task
systemctl start adhocracy-plus-celery-worker
```

Enable autostart on boot:

```
systemctl enable adhocracy-plus
systemctl enable adhocracy-plus-background-task
systemctl enable adhocracy-plus-celery-worker
```

### Setting up a proxy webserver
Expand Down Expand Up @@ -254,7 +262,7 @@ The landing page is managed via [wagtail](https://wagtail.io/). You can find the

```
systemctl stop adhocracy-plus
systemctl stop adhocracy-plus-background-task
systemctl stop adhocracy-plus-celery-worker
```

#### Switch to user
Expand Down Expand Up @@ -310,5 +318,5 @@ python manage.py runserver

```
systemctl start adhocracy-plus
systemctl start adhocracy-plus-background-task
systemctl start adhocracy-plus-celery-worker
```
1 change: 0 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ redis==5.0.0
Django==3.2.19
django-allauth==0.54.0
git+https://github.com/liqd/django-autoslug.git@liqd2212#egg=django-autoslug
django-background-tasks==1.2.5
django-ckeditor==6.5.1
django-filter==22.1
django-widget-tweaks==1.4.12
Expand Down
18 changes: 5 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,18 @@

import factory
import pytest
from celery import Celery
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from django.urls.base import get_resolver
from PIL import Image
from pytest_factoryboy import register
from rest_framework.test import APIClient

from adhocracy4.test import factories as a4_factories
from adhocracy4.test.factories.maps import AreaSettingsFactory
from adhocracy4.test.helpers import patch_background_task_decorator

from . import factories


def pytest_configure(config):
# Patch email background_task decorators for all tests
patch_background_task_decorator("adhocracy4.emails.tasks")
patch_background_task_decorator("apps.projects.tasks")

# Populate reverse dict with organisation patterns
resolver = get_resolver()
resolver.reverse_dict


register(factories.UserFactory)
register(factories.UserFactory, "user2")
register(factories.AdminFactory, "admin")
Expand All @@ -48,6 +36,10 @@ def pytest_configure(config):
register(AreaSettingsFactory)


def pytest_configure():
Celery(task_always_eager=True)


@pytest.fixture
def apiclient():
return APIClient()
Expand Down

0 comments on commit 80f4d5b

Please sign in to comment.