Celery on top of the logic extracted in 28. No solutions.md before submitting.
Exercise 1 — The local worker
- Bring up the stack: Redis (broker), worker (
celery -A ticketflow worker -l info --concurrency=2) and beat (celery -A ticketflow beat). Write the devdocker-compose.override.ymlif you use compose (40 formalizes it). - Create the
saludo(name)task returning "hola, {name}" and launch it withsaludo.delay("TicketFlow")from the shell. Verify the result with AsyncResult. - Kill the worker WHILE it processes a slow task (sleep 30) and bring it back up: with
acks_late, did the task re-execute? Paste the log evidence.
Exercise 2 — The expiration with a new transport
- Turn
expirar_reservas(28) into a task withautoretry_for=(OperationalError,),retry_backoff=True,retry_jitter=True,max_retries=5. - Schedule beat every minute. Verify across 3 consecutive minutes that only ONE run touches reservations (the logs must show refs
[]in the two minutes without expirations — no double-marking). - Test with eager mode:
override_settings(CELERY_TASK_ALWAYS_EAGER=True)and FakeClock — the task runs in the test's own process and the assert counts the expired refs.
Exercise 3 — on_commit or the ghost email
- Write the test that DEMONSTRATES the bug: enqueue
enviar_email_confirmacion.delay()inside a transaction that then fails (forced duplicate seat); in eager mode, the fake email went out anyway. Paste it red. - Switch to
transaction.on_commit(...)and verify: rollback → zero emails; commit → one email. - Apply on_commit to ALL the project's
.delay()calls:git grep -n "\.delay(" | grep -v on_commitmust return empty.
Exercise 4 — Task idempotency
- Add Redis dedup to
enviar_email_confirmacion(email:{ref}:confirmed, 7-day TTL,cache.add). Test: launch the task 3 times in a row (simulating the broker's at-least-once) → ONE email in the SMTP fake's outbox. - The charge task: pass
intent.idas theIdempotency-Keyto the fake gateway (14). Test: the fake gateway receives 2 calls with the same intent → 1 charge registered, the same response both times.
Exercise 5 — Task classification
- Build TicketFlow's task table: confirmation email, 24h-before reminder email, outgoing webhooks (17), expiration, monthly commission settlement, availability. Mark each: critical (infinite retry + alert) vs disposable (3 retries then DLQ), tolerated latency, and idempotency mechanism.
- Implement "infinite retry with alert" for the critical ones:
max_retries=None+ a task header that after 10 attempts publishes to thetask_retry_total{task}metric (46 formalizes the alert).
Submit
Paste the acks_late evidence, the on_commit red→green test, the dedup test and the classification table. Next: Lesson 30 — Message brokers.