Skip to content

Commit 776c3fc

Browse files
committed
add celery
1 parent 5261d14 commit 776c3fc

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

base-requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ feedparser==6.0.8
1919
beautifulsoup4==4.11.2
2020
icalendar==4.0.7
2121
chardet==4.0.0
22+
celery[redis]==5.3.6
23+
django-celery-beat==2.5.0
2224
# TODO: We may drop 'django-imagekit' completely.
2325
django-imagekit==4.0.2
2426
django-haystack==3.2.1

docker-compose.yml

+25
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ services:
1414
test: ["CMD", "pg_isready", "-U", "pythondotorg", "-d", "pythondotorg"]
1515
interval: 1s
1616

17+
redis:
18+
image: redis:7-bullseye
19+
ports:
20+
- "6379:6379"
21+
healthcheck:
22+
test: ["CMD", "redis-cli","ping"]
23+
interval: 1s
24+
1725
web:
1826
build: .
27+
image: pythondotorg:docker-compose
1928
command: python manage.py runserver 0.0.0.0:8000
2029
volumes:
2130
- .:/code
@@ -27,3 +36,19 @@ services:
2736
depends_on:
2837
postgres:
2938
condition: service_healthy
39+
redis:
40+
condition: service_healthy
41+
42+
worker:
43+
image: pythondotorg:docker-compose
44+
command: celery -A pydotorg worker -B -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler
45+
volumes:
46+
- .:/code
47+
environment:
48+
DATABASE_URL: postgresql://pythondotorg:pythondotorg@postgres:5432/pythondotorg
49+
DJANGO_SETTINGS_MODULE: pydotorg.settings.local
50+
depends_on:
51+
postgres:
52+
condition: service_healthy
53+
redis:
54+
condition: service_healthy

pydotorg/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from pydotorg.celery import app as celery_app
2+
3+
__all__ = ("celery_app",)

pydotorg/celery.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
from celery import Celery
4+
from django.core import management
5+
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pydotorg.settings.local")
7+
8+
app = Celery("pydotorg")
9+
app.config_from_object("django.conf:settings", namespace="CELERY")
10+
11+
@app.task(bind=True)
12+
def run_management_command(self, command_name, args, kwargs):
13+
management.call_command(command_name, *args, **kwargs)
14+
15+
app.autodiscover_tasks()

pydotorg/settings/base.py

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@
3131
)
3232
}
3333

34+
# celery settings
35+
_REDIS_URL = config("REDIS_URL", default="redis://redis:6379/0")
36+
37+
CELERY_BROKER_URL = _REDIS_URL
38+
CELERY_RESULT_BACKEND = _REDIS_URL
39+
40+
CELERY_BEAT_SCHEDULE = {
41+
# "example-management-command": {
42+
# "task": "pydotorg.celery.run_management_command",
43+
# "schedule": crontab(hour=12, minute=0),
44+
# "args": ("daily_volunteer_reminder", [], {}),
45+
# },
46+
# 'example-task': {
47+
# 'task': 'users.tasks.example_task',
48+
# },
49+
}
50+
3451
### Locale settings
3552

3653
TIME_ZONE = 'UTC'
@@ -163,6 +180,7 @@
163180
'django.contrib.admin',
164181
'django.contrib.admindocs',
165182

183+
'django_celery_beat',
166184
'django_translation_aliases',
167185
'pipeline',
168186
'sitetree',

0 commit comments

Comments
 (0)