Repository navigation
docs: describe Celery worker shutdown - #638
Conversation
Explain what workers do on SIGTERM, which settings keep tasks from being lost on rollouts, how to set them, and what they cost: tasks run at least once, so they must be idempotent. No default changes. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Reviewer's GuideAdds a Worker shutdown section explaining Celery’s shutdown and acknowledgment behavior, recommended reliability settings, configuration location, at-least-once delivery trade-offs, RabbitMQ timeout considerations, and Kubernetes/Helm termination-grace-period requirements. Sequence diagram for Celery worker shutdown and task redeliverysequenceDiagram
participant Kubernetes
participant Worker as CeleryWorker
participant Broker as RabbitMQ
participant Task
Kubernetes->>Worker: SIGTERM
Worker->>Worker: Stop accepting new tasks
Worker->>Task: Finish running task
alt Task finishes within terminationGracePeriodSeconds
Worker-->>Kubernetes: Exit gracefully
else Worker exceeds terminationGracePeriodSeconds
Kubernetes->>Worker: SIGKILL
Worker--x Task: Process is lost
alt task_acks_late and task_reject_on_worker_lost enabled
Worker-->>Broker: Connection lost before acknowledgement
Broker->>Broker: Redeliver task
else Default acknowledgement behavior
Broker->>Broker: Task is not redelivered
end
end
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #638 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 56 56
Lines 7090 7090
=========================================
Hits 7090 7090
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="docs/configuration/jobs.md" line_range="46" />
<code_context>
+
+On `SIGTERM`, a worker shuts down warmly: it stops taking new tasks, waits for
+the running tasks to finish, and then exits. Messages it has fetched but not
+started go back to the queue. If the running tasks take longer than the
+platform allows (on Kubernetes, `terminationGracePeriodSeconds`), the worker
+gets a `SIGKILL`.
</code_context>
<issue_to_address>
**Fetched tasks are lost**
When a task is waiting in the worker pool when the worker receives `SIGKILL`, `task_acks_late=False` can acknowledge a task when it is dispatched to the pool, so a later `SIGKILL` cannot requeue it and the statement that fetched-but-not-started messages return to the queue is false.
Qualify the claim and explain that early-acknowledged tasks dispatched to the pool are lost on `SIGKILL` unless late acknowledgments are enabled.
</issue_to_address>
### Comment 2
<location path="docs/configuration/jobs.md" line_range="82-84" />
<code_context>
+ stay with the defaults, or record their progress and check it first.
+- **A task that kills its worker runs again and again.** With
+ `task_reject_on_worker_lost`, a task that runs out of memory, for example,
+ goes back to the queue each time. Bound this, e.g., with a [delivery
+ limit](https://www.rabbitmq.com/docs/quorum-queues#poison-message-handling)
+ on the queue.
+- **Long tasks need a longer broker timeout.** RabbitMQ closes the channel of
+ a consumer that hasn't acknowledged a message for 30 minutes by default
</code_context>
<issue_to_address>
**Delivery limit does not bound redelivery**
When an operator enables the suggested limit while using Celery's default classic RabbitMQ queue, rabbitMQ's delivery limit applies to quorum queues, not the default classic queue, so `task_reject_on_worker_lost` can keep requeuing a task that crashes its worker and cause a crash loop.
Explain that the delivery limit requires a quorum queue and recommend configuring one or another way to bound redelivery.
</issue_to_address>
### Comment 3
<location path="docs/configuration/jobs.md" line_range="56" />
<code_context>
+
+| Setting | Default | Effect |
+|---|---|---|
+| `task_acks_late` | `False` | Acknowledge after the task ran, so that the broker delivers it again if the worker's connection is lost before |
+| `task_reject_on_worker_lost` | `False` | With `task_acks_late`, put the task back on the queue if the process running it dies (by default, it is acknowledged and marked as failed) |
+| `worker_prefetch_multiplier` | `4` | Messages each worker process fetches ahead; `1` suits long tasks, as fewer tasks then wait behind a running one |
</code_context>
<issue_to_address>
**Redelivery condition is incomplete**
When a reader relies on the table to understand `task_acks_late`, the `task_acks_late` effect ends at “before” without saying what must happen before what, so readers cannot determine the condition under which the broker redelivers the task.
Complete the condition, for example by specifying that the connection is lost before the acknowledgment reaches the broker.
</issue_to_address>Linked issues
#605 docs: describe Celery worker shutdown · 6 of 6 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Document SIGTERM and warm shutdown | ✅ Met | The new section explains that workers stop taking tasks, finish running tasks, and return fetched but unstarted messages to the queue. |
| 2 | Cover task_acks_late, task_reject_on_worker_lost, and worker_prefetch_multiplier: 1 | ✅ Met | The table describes these settings and the example configures all three on the returned Celery app. |
| 3 | Explain the idempotent-task consequence | ✅ Met | The section explains at-least-once delivery and warns that tasks must be safe to repeat. |
| 4 | Document the chart's worker terminationGracePeriodSeconds | ✅ Met | The section identifies the Helm value and its 30-second default. |
| 5 | Make no default changes | ✅ Met | The change adds documentation only. |
| 6 | Pass mkdocs build --strict | ✅ Met | The PR's verification reports that mkdocs build --strict passes. |
Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: docs/configuration/jobs.md:46, docs/configuration/jobs.md:84
Fetched messages return to the queue only if not acknowledged, and a delivery limit needs a quorum queue. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Closes #605
Summary
Adds a "Worker shutdown" section to
docs/configuration/jobs.md: warm shutdown onSIGTERM,task_acks_late,task_reject_on_worker_lost,worker_prefetch_multiplier, where to set them (celery_app.conf.update(), asjobshas no field for them), the at-least-once consequences (idempotent tasks, tasks that kill their worker, RabbitMQ'sconsumer_timeout) and the chart'sworker.terminationGracePeriodSeconds. No default changes.Light lane: docs only; I reviewed the diff myself.
Verification
mkdocs build --strict: passesruff format --check(formats code blocks in docs): passesThe Celery and RabbitMQ behaviour is described from their documentation, not tested against a running worker.
🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Summary by Sourcery
Document how to configure Celery workers for safer shutdowns and at-least-once task processing.
Enhancements:
Documentation: