Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit d2abb22

Browse files
authored
Use Redis for Celery Result Backend (#348)
* Disable Celery worker mingling * Enable Redis result backend * Setup Redis in dev container * Ensure tasks are invoked correctly in tests * Add Redis to commit tests * Remove unnecessary Redis memory optimization * Use REDIS_URL environment variable * Pin to Redis 7.0 * Pin to PostgreSQL 14.10 * Pin to Redis 7
1 parent 506e13f commit d2abb22

File tree

10 files changed

+28
-18
lines changed

10 files changed

+28
-18
lines changed

.devcontainer/devcontainer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99

1010
// Features to add to the dev container. More info: https://containers.dev/features.
1111
"features": {
12-
"ghcr.io/devcontainers-contrib/features/postgres-asdf:1": {}
12+
"ghcr.io/devcontainers-contrib/features/postgres-asdf:1": {
13+
"version": "14.10"
14+
},
15+
"ghcr.io/itsmechlark/features/redis-server:1": {
16+
"version": "7"
17+
}
1318
},
1419

1520
// Use portAttributes to map port numbers to default attributes.

.env.sample

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
22
ALLOWED_HOSTS=rebootcanadadb.herokuapp.com,rebootcanada.herokuapp.com,localhost,0.0.0.0,127.0.0.1
3-
CELERY_RESULT_BACKEND="rpc://"
43
CLOUDAMQP_APIKEY=
54
CLOUDAMQP_URL=
65
CSRF_TRUSTED_ORIGINS=.preview.app.github.dev
@@ -14,6 +13,7 @@ EMAIL_HOST=smtp.gmail.com
1413
EMAIL_HOST_DISPLAY_NAME=
1514
EMAIL_HOST_USER=
1615
EMAIL_HOST_PASSWORD=
16+
REDIS_URL="redis://localhost:6379/0"
1717
# SECRET_KEY: Check out https://www.miniwebtool.com/django-secret-key-generator/ for generating new key
1818
SECRET_KEY=abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)
1919
SECURE_SSL_REDIRECT=False

.github/workflows/deployment.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ jobs:
5252
env:
5353
ADMIN: NAME,[email protected]
5454
ALLOWED_HOSTS: localhost,0.0.0.0,127.0.0.1
55-
CELERY_RESULT_BACKEND: "rpc://"
5655
CSRF_TRUSTED_ORIGINS: ""
5756
DB_NAME: reboot
5857
DB_USER: root
@@ -62,12 +61,13 @@ jobs:
6261
EMAIL_HOST: smtp.gmail.com
6362
EMAIL_HOST_USER: ""
6463
EMAIL_HOST_PASSWORD: ""
64+
REDIS_URL: "redis://localhost:6379/0"
6565
SECRET_KEY: ${{ github.run_id }}-${{ github.run_attempt }}
6666
SECURE_SSL_REDIRECT: False
6767

6868
services:
6969
postgres:
70-
image: postgres:13
70+
image: postgres:14.10
7171
env:
7272
POSTGRES_USER: ${{ env.DB_USER }}
7373
POSTGRES_PASSWORD: ${{ env.DB_PASSWORD }}
@@ -76,6 +76,12 @@ jobs:
7676
- 5432:5432
7777
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
7878

79+
redis:
80+
image: redis:7
81+
ports:
82+
- 6379:6379
83+
options: --entrypoint redis-server --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
84+
7985
steps:
8086
- name: Checkout the Git repository
8187
uses: actions/checkout@v3

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ migrate:
6868

6969
.PHONY: celery
7070
celery:
71-
celery worker -A reboot --without-gossip --without-heartbeat
71+
celery worker -A reboot --without-heartbeat --without-gossip --without-mingle
7272

7373
.PHONY: clean
7474
clean:

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
worker: celery worker -A reboot --without-gossip --without-heartbeat
1+
worker: celery worker -A reboot --without-heartbeat --without-gossip --without-mingle
22
web: gunicorn reboot.wsgi --log-level info

app/worker/tasks/importers/test__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ def test_historical_data_importer(self) -> None:
9595
})
9696
csvvalue = csvfile.getvalue().splitlines()
9797

98-
importers.historical_data_importer(
99-
csvpath=csvvalue)
98+
importers.historical_data_importer.apply(args=[csvvalue])
10099

101100
got_donor = Donor.objects.get(donor_name="Example Danger Donor")
102101

app/worker/tasks/test__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def test_receiptor_single_file(self) -> None:
2020
queryset_json = serializers.serialize(format="json", queryset=queryset)
2121
total_count = Donation.objects.count()
2222

23-
response = tasks.receiptor(
24-
queryset=queryset_json, total_count=total_count)
23+
result = tasks.receiptor.apply(args=[queryset_json, total_count])
24+
response = result.get()
2525

2626
self.assertEqual(first=response.status_code, second=200)
2727
self.assertEqual(
@@ -34,8 +34,8 @@ def test_receiptor_multiple_files(self) -> None:
3434
queryset_json = serializers.serialize(format="json", queryset=queryset)
3535
total_count = Donation.objects.count()
3636

37-
response = tasks.receiptor(
38-
queryset=queryset_json, total_count=total_count)
37+
result = tasks.receiptor.apply(args=[queryset_json, total_count])
38+
response = result.get()
3939

4040
self.assertEqual(first=response.status_code, second=200)
4141
self.assertEqual(

app/worker/tasks/test_exporter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def test_exporter(self) -> None:
2828
qs = serialize(format="json", queryset=queryset)
2929
total_count = len(queryset)
3030

31-
response = exporter(file_name=file_name, qs=qs,
32-
total_count=total_count)
31+
result = exporter.apply(args=[file_name, qs, total_count])
32+
response = result.get()
3333
content_type = response["Content-Type"]
3434

3535
self.assertEqual(first=response.status_code, second=200)

reboot/celeryconfig.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ssl
2-
from decouple import config
32

3+
from decouple import config
44

55
broker_url = config('CLOUDAMQP_URL', default='amqp://guest@localhost//')
66
broker_connection_timeout = 30
@@ -10,7 +10,7 @@
1010
worker_prefetch_multiplier = 1
1111
worker_concurrency = 10
1212
accept_content = ['json', 'pickle']
13-
result_backend = config("CELERY_RESULT_BACKEND", default=broker_url)
13+
result_backend = config("REDIS_URL")
1414
task_serializer = 'pickle'
1515
result_serializer = 'pickle'
1616

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Commented requirements indicate the latest patches of the earliest releases that support Python 3.9.
22

33
# https://pypi.org/project/celery/5.1.2/
4-
# celery==5.1.2
5-
celery==4.4.7
4+
# celery[redis]==5.1.2
5+
celery[redis]==4.4.7
66
# https://pypi.org/project/Django/2.2.28/
77
Django==2.2.28
88
# https://pypi.org/project/dj-database-url/1.0.0/

0 commit comments

Comments
 (0)