Skip to content

Commit 87c9014

Browse files
authored
Upgrade course 1.3 (#8)
updates
1 parent 4cca9de commit 87c9014

35 files changed

+329
-311
lines changed

.env/.dev-sample

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SECRET_KEY=dbaa1_i7%*3r9-=z-+_mz4r-!qeed@(-a_r(g@k8jo8y3r27%m
33
DJANGO_ALLOWED_HOSTS=*
44

55
SQL_ENGINE=django.db.backends.postgresql
6-
SQL_DATABASE=hello_django_dev
6+
SQL_DATABASE=hello_django
77
SQL_USER=hello_django
88
SQL_PASSWORD=hello_django
99
SQL_HOST=db

.env/.prod-sample

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SECRET_KEY=dbaa1_i7%*3r9-=z-+_mz4r-!qeed@(-a_r(g@k8jo8y3r27%m
33
DJANGO_ALLOWED_HOSTS=*
44

55
SQL_ENGINE=django.db.backends.postgresql
6-
SQL_DATABASE=hello_django_dev
6+
SQL_DATABASE=hello_django
77
SQL_USER=hello_django
88
SQL_PASSWORD=hello_django
99
SQL_HOST=db

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
https://testdriven.io/courses/django-celery/
1+
Source code of the [The Definitive Guide to Celery and Djang](https://testdriven.io/courses/django-celery/)
2+
3+
By [Michael Yin](https://github.com/michael-yin/)

compose/local/django/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.9-slim-buster
1+
FROM python:3.10-slim-buster
22

33
ENV PYTHONUNBUFFERED 1
44
ENV PYTHONDONTWRITEBYTECODE 1

compose/local/django/celery/beat/start

+2
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ set -o errexit
44
set -o nounset
55

66
rm -f './celerybeat.pid'
7+
8+
# celery -A django_celery_example beat -l INFO
79
celery -A django_celery_example beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler

compose/local/django/celery/worker/start

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
set -o errexit
44
set -o nounset
55

6-
# celery -A django_celery_example worker -l INFO
7-
python manage.py celery_worker
6+
watchfiles \
7+
--filter python \
8+
'celery -A django_celery_example worker --loglevel=info -Q high_priority,default'
9+

compose/production/django/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.9-slim-buster
1+
FROM python:3.10-slim-buster
22

33
ENV PYTHONUNBUFFERED 1
44

compose/production/django/celery/beat/start

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -o pipefail
55
set -o nounset
66

77

8-
celery -A django_celery_example beat -l INFO
8+
exec celery -A django_celery_example beat -l INFO

compose/production/django/celery/flower/start

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ until worker_ready; do
1313
done
1414
>&2 echo 'Celery workers is available'
1515

16-
celery -A django_celery_example \
16+
exec celery -A django_celery_example \
1717
--broker="${CELERY_BROKER}" \
1818
flower \
1919
--basic_auth="${CELERY_FLOWER_USER}:${CELERY_FLOWER_PASSWORD}" \

compose/production/django/celery/worker/start

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ set -o errexit
44
set -o pipefail
55
set -o nounset
66

7-
celery -A django_celery_example worker -l INFO
7+
exec celery -A django_celery_example worker -l INFO

compose/production/nginx/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM nginx:1.21-alpine
1+
FROM nginx:1.23-alpine
22

33
RUN rm /etc/nginx/conf.d/default.conf
44
COPY nginx.conf /etc/nginx/conf.d

django_celery_example/asgi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
It exposes the ASGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
7+
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
88
"""
99

10-
1110
import os
1211

1312
from channels.routing import ProtocolTypeRouter, URLRouter
@@ -23,3 +22,4 @@
2322
routing.urlpatterns
2423
)
2524
})
25+

django_celery_example/celery.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""
2-
https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html
2+
https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html
33
"""
44
import os
55
import logging
6+
67
from celery import Celery
78
from celery.signals import after_setup_logger
89

@@ -19,7 +20,7 @@
1920
# config keys has `CELERY` prefix
2021
app.config_from_object('django.conf:settings', namespace='CELERY')
2122

22-
# discover and load tasks.py in django apps
23+
# discover and load tasks.py from from all registered Django apps
2324
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
2425

2526

django_celery_example/settings.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
"""
22
Django settings for django_celery_example project.
33
4-
Generated by 'django-admin startproject' using Django 3.2.9.
4+
Generated by 'django-admin startproject' using Django 4.0.7.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.2/topics/settings/
7+
https://docs.djangoproject.com/en/4.0/topics/settings/
88
99
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/3.2/ref/settings/
10+
https://docs.djangoproject.com/en/4.0/ref/settings/
1111
"""
1212
import os
1313

1414
from pathlib import Path
15+
1516
from kombu import Queue
1617

1718
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1819
BASE_DIR = Path(__file__).resolve().parent.parent
1920

2021

2122
# Quick-start development settings - unsuitable for production
22-
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
23+
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
2324

25+
# SECURITY WARNING: keep the secret key used in production secret!
2426
SECRET_KEY = os.environ.get("SECRET_KEY", "&nl8s430j^j8l*je+m&ys5dv#zoy)0a2+x1!m8hx290_sx&0gh")
2527

28+
# SECURITY WARNING: don't run with debug turned on in production!
2629
DEBUG = int(os.environ.get("DEBUG", default=1))
2730

2831
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "127.0.0.1").split(" ")
@@ -74,9 +77,8 @@
7477
# WSGI_APPLICATION = 'django_celery_example.wsgi.application'
7578
ASGI_APPLICATION = 'django_celery_example.asgi.application'
7679

77-
7880
# Database
79-
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
81+
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
8082

8183
DATABASES = {
8284
"default": {
@@ -91,7 +93,7 @@
9193

9294

9395
# Password validation
94-
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
96+
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
9597

9698
AUTH_PASSWORD_VALIDATORS = [
9799
{
@@ -110,32 +112,33 @@
110112

111113

112114
# Internationalization
113-
# https://docs.djangoproject.com/en/3.2/topics/i18n/
115+
# https://docs.djangoproject.com/en/4.0/topics/i18n/
114116

115117
LANGUAGE_CODE = 'en-us'
116118

117119
TIME_ZONE = 'UTC'
118120

119121
USE_I18N = True
120122

121-
USE_L10N = True
122-
123123
USE_TZ = True
124124

125125

126126
# Static files (CSS, JavaScript, Images)
127-
# https://docs.djangoproject.com/en/3.2/howto/static-files/
127+
# https://docs.djangoproject.com/en/4.0/howto/static-files/
128128

129-
STATIC_URL = '/static/'
129+
STATIC_URL = 'static/'
130130
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
131131

132+
# where Django hold upload files
133+
MEDIA_URL = '/media/'
134+
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
135+
132136
# Default primary key field type
133-
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
137+
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
134138

135139
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
136140

137-
MEDIA_URL = '/media/'
138-
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
141+
################################################################################
139142

140143
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER", "redis://127.0.0.1:6379/0")
141144
CELERY_RESULT_BACKEND = os.environ.get("CELERY_BACKEND", "redis://127.0.0.1:6379/0")
@@ -149,13 +152,15 @@
149152
},
150153
}
151154

155+
152156
CELERY_BEAT_SCHEDULE = {
153157
# 'task-clear-session': {
154158
# 'task': 'task_clear_session',
155159
# "schedule": 5.0, # five seconds
156160
# },
157161
}
158162

163+
159164
CELERY_TASK_DEFAULT_QUEUE = 'default'
160165

161166
# Force all queues to be explicitly listed in `CELERY_TASK_QUEUES` to help prevent typos
@@ -169,7 +174,6 @@
169174
Queue('low_priority'),
170175
)
171176

172-
# manual task routing
173177

174178
# CELERY_TASK_ROUTES = {
175179
# 'django_celery_example.celery.*': {
@@ -187,3 +191,4 @@ def route_task(name, args, kwargs, options, task=None, **kw):
187191

188192

189193
CELERY_TASK_ROUTES = (route_task,)
194+

django_celery_example/urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""django_celery_example URL Configuration
22
33
The `urlpatterns` list routes URLs to views. For more information please see:
4-
https://docs.djangoproject.com/en/3.2/topics/http/urls/
4+
https://docs.djangoproject.com/en/4.0/topics/http/urls/
55
Examples:
66
Function views
77
1. Add an import: from my_app import views
@@ -19,6 +19,6 @@
1919

2020
urlpatterns = [
2121
path('admin/', admin.site.urls),
22-
path("member/", include('tdd.urls')),
22+
path('member/', include('tdd.urls')),
2323
path('', include('polls.urls')),
2424
]

django_celery_example/wsgi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
7+
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
88
"""
99

1010
import os

docker-compose.prod.yml

+21-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
volumes:
3535
- postgres_data:/var/lib/postgresql/data/
3636
environment:
37-
- POSTGRES_DB=hello_django_dev
37+
- POSTGRES_DB=hello_django
3838
- POSTGRES_USER=hello_django
3939
- POSTGRES_PASSWORD=hello_django
4040

@@ -92,29 +92,29 @@ services:
9292
- redis
9393
- db
9494

95-
prometheus:
96-
image: prom/prometheus
97-
ports:
98-
- 9090:9090
99-
command:
100-
- --config.file=/etc/prometheus/prometheus.yml
101-
volumes:
102-
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
103-
depends_on:
104-
- cadvisor
95+
# prometheus:
96+
# image: prom/prometheus
97+
# ports:
98+
# - 9090:9090
99+
# command:
100+
# - --config.file=/etc/prometheus/prometheus.yml
101+
# volumes:
102+
# - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
103+
# depends_on:
104+
# - cadvisor
105105

106-
cadvisor:
107-
image: google/cadvisor
108-
container_name: cadvisor
109-
volumes:
110-
- /:/rootfs:ro
111-
- /var/run:/var/run:rw
112-
- /sys:/sys:ro
113-
- /var/lib/docker/:/var/lib/docker:ro
114-
- /var/run/docker.sock:/var/run/docker.sock:ro
106+
# cadvisor:
107+
# image: google/cadvisor
108+
# container_name: cadvisor
109+
# volumes:
110+
# - /:/rootfs:ro
111+
# - /var/run:/var/run:rw
112+
# - /sys:/sys:ro
113+
# - /var/lib/docker/:/var/lib/docker:ro
114+
# - /var/run/docker.sock:/var/run/docker.sock:ro
115115

116116
volumes:
117117
postgres_data:
118118
staticfiles:
119119
mediafiles:
120-
flower_db:
120+
flower_db:

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ services:
2626
volumes:
2727
- postgres_data:/var/lib/postgresql/data/
2828
environment:
29-
- POSTGRES_DB=hello_django_dev
29+
- POSTGRES_DB=hello_django
3030
- POSTGRES_USER=hello_django
3131
- POSTGRES_PASSWORD=hello_django
3232

3333
redis:
34-
image: redis:6-alpine
34+
image: redis:7-alpine
3535

3636
celery_worker:
3737
build:

polls/consumers.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ def get_task_info(task_id):
1111
return task info according to the task_id
1212
"""
1313
task = AsyncResult(task_id)
14-
if task.state == 'FAILURE':
14+
state = task.state
15+
16+
if state == 'FAILURE':
1517
error = str(task.result)
1618
response = {
17-
'state': task.state,
19+
'state': state,
1820
'error': error,
1921
}
2022
else:
2123
response = {
22-
'state': task.state,
24+
'state': state,
2325
}
2426
return response
2527

@@ -30,6 +32,8 @@ def notify_channel_layer(task_id):
3032
3133
Since Celery now still not support `asyncio`, so we should use async_to_sync
3234
to make it synchronous
35+
36+
https://channels.readthedocs.io/en/stable/topics/channel_layers.html#using-outside-of-consumers
3337
"""
3438
channel_layer = get_channel_layer()
3539
async_to_sync(channel_layer.group_send)(

polls/factories.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from factory import LazyAttribute
22
from factory.django import DjangoModelFactory
3-
from factory.fuzzy import FuzzyText
3+
from factory import Faker
44

55
from django.contrib.auth.hashers import make_password
66
from django.contrib.auth.models import User
@@ -10,8 +10,9 @@ class UserFactory(DjangoModelFactory):
1010
class Meta:
1111
model = User
1212

13-
username = FuzzyText(length=6)
13+
username = Faker("user_name")
1414
email = LazyAttribute(lambda o: '%[email protected]' % o.username)
1515
password = LazyAttribute(lambda o: make_password(o.username))
16-
first_name = FuzzyText(length=6)
17-
last_name = FuzzyText(length=6)
16+
first_name = Faker("first_name")
17+
last_name = Faker("last_name")
18+

0 commit comments

Comments
 (0)