Checkpoint¶
Practical exercise¶
Exercise 1: checking the worker¶
docker compose up -d
# Look at the beat logs:
docker compose logs -f celery-beat
# You should see: "Sending due task send-reminders-every-minute"
Exercise 2: a manual test through the shell¶
# Create a reminder in the past
docker compose run --rm web python manage.py shell -c "
from django.utils import timezone
from datetime import timedelta
from notes_app.models import Note, Reminder
from django.contrib.auth.models import User
u = User.objects.filter(is_superuser=False).first()
n = Note.objects.filter(user=u).first()
r = Reminder.objects.create(
note=n,
remind_at=timezone.now() - timedelta(minutes=2),
message='Manual test!',
)
print('Created reminder id:', r.id)
"
# Run the task manually
docker compose run --rm web python manage.py shell -c "
from notes_app.tasks import send_reminder_notifications
send_reminder_notifications()
print('Done')
"
# Check that is_sent=True
docker compose run --rm web python manage.py shell -c "
from notes_app.models import Reminder
print('Sent:', Reminder.objects.filter(is_sent=True).count())
"
Exercise 3: browser toast¶
- Run
docker compose up -dand log in in the browser. - Do exercise 2 (create a reminder in the past).
- Open
/notes/: the Toast should appear at the bottom right within 3 seconds. - Open DevTools → Network → find the
/reminders/check/request → check the JSON response.
Exercise 4: understanding repeats¶
# In the Django shell:
from dateutil.relativedelta import relativedelta
from datetime import datetime
# What does timedelta(days=30) return for January 31?
d = datetime(2024, 1, 31)
print(d + relativedelta(months=1)) # 2024-02-29 (correct)
print(d + __import__('datetime').timedelta(days=30)) # 2024-03-01 (wrong!)
Explain why relativedelta is the better choice for a monthly repeat.
Exercise 5: scaling (research)¶
What happens if you run two instances of celery-beat? How can you protect against duplicate tasks? Hint: look up celery beat --max-interval and django-celery-beat.
Self-check checklist¶
- [ ]
notes_project/celery.pyexists and containsapp = Celery('notes_project') - [ ]
notes_project/__init__.pyimportscelery_app - [ ]
CELERY_BROKER_URLis configured insettings.py - [ ] Celery and Channels use different Redis DBs (0 and 1)
- [ ]
CELERY_BEAT_SCHEDULEcontainssend_reminder_notifications - [ ]
celery-workerandcelery-beatare indocker-compose.yml - [ ]
celery-beatruns as a single instance - [ ]
@shared_task(not@app.task) intasks.py - [ ]
select_related('note__user')in the pending reminders query - [ ]
save(update_fields=['is_sent']): updating only one field - [ ]
relativedeltafor the monthly repeat - [ ]
GET /reminders/check/returns{"reminders": [...]} - [ ]
@login_requiredon thereminders_checkview - [ ]
escapeHtml()in JS before inserting into the DOM - [ ]
sessionStoragededuplicates Toasts within one session - [ ]
aria-live="polite"on the toast container - [ ] Unit tests for
send_reminder_notifications:@override_settings(EMAIL_BACKEND=...) - [ ]
mail.outboxchecks the sent emails
Navigation¶
Previous: Step 7. Async Django: WebSocket, Channels, GroupChatConsumer
Next: Step 9. Deployment: Docker Compose in production, nginx, GitHub Actions CI