Celery Task Management
Worker Shutdown
Task Notification
Software Development
Coding Best Practices

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.

Practice system design

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:

  • SIGTERM and SIGINT lead to a warm shutdown where the worker finishes all currently executing tasks.
  • SIGQUIT leads 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:

python
1import signal
2
3def handle_shutdown(signum, frame):
4    print("Handling shutdown...")
5    # Perform cleanup, release resources, etc.
6
7signal.signal(signal.SIGTERM, handle_shutdown)

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:

python
1from celery.signals import task_revoked
2
3@task_revoked.connect
4def task_revoked_handler(request, terminated, signum, expired, **kwargs):
5    print("Task was revoked!")
6    # Cleanup code here

3. Periodic Check

For long-running tasks, it can be practical to occasionally check if a shutdown event has been signalled:

python
1import time
2
3def long_running_task():
4    while not shutdown_signal_received():
5        time.sleep(1)
6        # Proceed with task
7    cleanup()

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 / SignalActionImpact
SIGTERMWarm shutdown; finish current task.Ensures tasks complete, but delay in shutdown.
SIGINTSame as SIGTERM.Same as SIGTERM.
SIGQUITCold shutdown; immediate.Fast shutdown but may lead to inconsistent state.
Task RevokedExecute 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.