Notify celery task of worker shutdown
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Celery is a powerful asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well. One of the essential aspects of managing Celery in production environments is handling worker shutdown gracefully. This allows currently running tasks to finish executing or to handle the shutdown in such a manner that tasks can be resumed or restarted later safely.
Understanding Worker Shutdown
When a Celery worker receives a signal to shut down (e.g., SIGTERM or SIGINT), it should not immediately stop its operations but should instead complete certain tasks to ensure consistency and reliability of the application. The shutdown process involves several steps, including completing currently processing tasks, saving the necessary state, and properly managing connections to other services like databases or message brokers.
Different Signal Handling in Celery
Celery by default tries to handle different signals intelligently:
SIGTERMandSIGINTlead to a warm shutdown where the worker finishes all currently executing tasks.SIGQUITleads to a cold shutdown where the worker stops immediately, even if tasks are running.
Notifying Tasks of Shutdown
Sometimes, tasks need to be aware of a shutdown event to properly clean up resources, commit final data changes, or notify other systems of the interruption. Here are several approaches to notify tasks about a potential shutdown:
1. Catching Shutdown Signals Within Tasks
You can write task code to catch shutdown signals during its execution:
2. Using on_task_revoked Callback
Celery provides a signal task_revoked, which can be connected to tasks, allowing custom code execution when a task is revoked:
3. Periodic Check
For long-running tasks, it can be practical to occasionally check if a shutdown event has been signalled:
Key Challenges
Handling worker shutdowns in Celery has certain challenges:
- Timely Execution: Ensuring the task checks for shutdown signals at regular intervals can introduce complexity.
- Resource Management: Properly managing resources (like database connections) during a shutdown is essential.
- Task Recovery: Post-shutdown, the system needs to decide whether to retry tasks, ignore them, or log them as failures.
Summary Table
| Feature / Signal | Action | Impact |
| SIGTERM | Warm shutdown; finish current task. | Ensures tasks complete, but delay in shutdown. |
| SIGINT | Same as SIGTERM. | Same as SIGTERM. |
| SIGQUIT | Cold shutdown; immediate. | Fast shutdown but may lead to inconsistent state. |
| Task Revoked | Execute task_revoked handler. | Enables cleanup or completion steps per task. |
Conclusion
Managing Celery worker shutdown properly is crucial for maintaining the reliability and consistency of applications. Various techniques, such as signal handling, periodical checks, and task customization via Celery signals, can be employed to ensure tasks are aware of the shutdown and can act accordingly. While these methods increase the robustness of the task execution framework, they also add a layer of complexity that requires careful implementation and testing.
Related reading
- NServiceBus and Rabbit MQ or Kafka
- nservicebus send command object with list type property using msmq transport
- NServiceBus vs MassTransit
- Number of commits and offset in each partition of a kafka topic
- Number of Partitions vs Producer Throughput in Apache Kafka
- object kafka is not a member of package org.apache
- Object not serializable (org.apache.kafka.clients.consumer.ConsumerRecord) in Java spark kafka streaming
- Offset missing from Kafka logs - Simple Consumer unable to proceed

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.