Skip to content

docs: describe Celery worker shutdown - #638

Merged
uniqueg merged 2 commits into
devfrom
docs/celery-shutdown
Nov 3, 2026
Merged

uniqueg merged 2 commits into
devfrom
docs/celery-shutdown

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Closes #605

Summary

Adds a "Worker shutdown" section to docs/configuration/jobs.md: warm shutdown on SIGTERM, task_acks_late, task_reject_on_worker_lost, worker_prefetch_multiplier, where to set them (celery_app.conf.update(), as jobs has no field for them), the at-least-once consequences (idempotent tasks, tasks that kill their worker, RabbitMQ's consumer_timeout) and the chart's worker.terminationGracePeriodSeconds. No default changes.

Light lane: docs only; I reviewed the diff myself.

Verification

  • mkdocs build --strict: passes
  • ruff format --check (formats code blocks in docs): passes

The 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:

  • Document Celery worker shutdown behavior, task delivery guarantees, and production configuration considerations.

Documentation:

  • Add guidance for configuring Celery acknowledgments, prefetching, broker timeouts, Kubernetes termination grace periods, and signal handling during worker shutdown.

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
@uniqueg uniqueg added this to the v0.20.0 milestone Nov 3, 2026
@uniqueg uniqueg added the type: docs Improvements or additions to documentation label Nov 3, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Adds 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 redelivery

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Adds production guidance for graceful Celery worker shutdowns and task redelivery.
  • Describes warm shutdown on SIGTERM, fetched-message handling, and Kubernetes termination grace periods.
  • Documents late acknowledgments, worker-loss rejection, and prefetch settings, including their defaults and effects.
  • Shows how to configure the settings on the Celery app returned by Foca rather than through JobsConfig.
  • Explains at-least-once delivery implications, including idempotency, poison-task redelivery, RabbitMQ acknowledgement timeouts, and broker delivery limits.
  • Documents Helm chart grace-period configuration and the requirement that workers receive signals directly.
docs/configuration/jobs.md

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d15bac8) to head (b9ec898).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #638   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           56        56           
  Lines         7090      7090           
=========================================
  Hits          7090      7090           
Flag Coverage Δ
test_unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 3, 2026 06:17

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread docs/configuration/jobs.md Resolved
Comment thread docs/configuration/jobs.md Resolved
Comment thread docs/configuration/jobs.md Resolved
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
@uniqueg
uniqueg merged commit 1122ead into dev Nov 3, 2026
56 checks passed
@uniqueg
uniqueg deleted the docs/celery-shutdown branch November 3, 2026 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: docs Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: describe Celery worker shutdown

1 participant