Capture Heroku SIGTERM in Celery workers to shutdown worker gracefully
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Heroku is a popular cloud platform that enables developers to deploy, manage, and scale modern apps. When using Celery, an asynchronous task queue based on distributed message passing, on Heroku, special considerations need to be made to handle the way Heroku terminates processes. This article discusses how to capture Heroku's SIGTERM signal in Celery workers to ensure they shutdown gracefully.
Why Capture SIGTERM?
Heroku can shut down a process for various reasons: scaling down, cycling, or releasing newer versions of the app. In these cases, Heroku sends a SIGTERM signal to the process before terminating it after a short grace period (usually 30 seconds). Following the SIGTERM signal, if the process has not terminated, Heroku sends a SIGKILL, which unconditionally forces the process to terminate. Handling SIGTERM gracefully ensures that Celery workers finish their current tasks and stop accepting new ones, which prevents task interruption and potential data loss.
Handling SIGTERM in Celery
To handle SIGTERM signals gracefully in a Celery environment, you need to configure both the Celery worker and your deployment setup to cooperate with Heroku's lifecycle events. Below is a detailed explanation and example of implementing graceful shutdown in Celery workers running on Heroku:
Step 1: Configure Celery Worker to Handle SIGTERM
Celery does not automatically handle the SIGTERM signal for graceful shutdown. Thus, you need to capture this signal and initiate the shutdown process using Celery's built-in support for warm shutdowns. Here's how you can do it:
In this script, when a SIGTERM signal is received, the graceful_shutdown function broadcasts a shutdown signal to all Celery workers and raises a SystemExit exception to terminate the worker process.
Step 2: Modify Procfile for Heroku
The Procfile tells Heroku how to run the processes for your application. Modify the Procfile to ensure that the worker starts with the Python script that includes our SIGTERM handling logic.
Testing Graceful Shutdown
To ensure that your changes work properly, you can test the setup by manually scaling down the worker dynos or by pushing updates. Observing the logs will help ensure that the SIGTERM handling is working as expected, and tasks are not abruptly terminated.
Best Practices
- Monitoring and Logging: Integrate monitoring and ensure your logs capture the start and completion of tasks. This will help in auditing that tasks complete successfully before shutdown.
- Job Checkpoints: For long-running tasks, implement periodic checkpoints so that progress can be saved and tasks can resume efficiently from the last checkpoint in case of interruptions.
Summary Table
| Key Component | Description |
| SIGTERM handling | Capture and respond to SIGTERM signals in Celery to prevent abrupt termination of tasks. |
| Procfile configuration | Proper Procfile setup on Heroku to start workers with the modified script. |
| Testing | Manually scale down or update apps to test graceful shutdowns. |
| Best practices | Implement monitoring, logging, and job checkpoints to ensure robust operation. |
Conclusion
Handling SIGTERM gracefully in Celery workers running on Heroku is crucial for maintaining data integrity and ensuring that tasks complete successfully without interruption. By implementing the described steps and considering best practices, developers can ensure that their background tasks handle abrupt shutdowns more gracefully, ultimately leading to more resilient applications.

