Capture Heroku SIGTERM in Celery workers to shutdown worker gracefully Capture Heroku SIGTERM in Celery workers to shutdown worker gracefully python python

Capture Heroku SIGTERM in Celery workers to shutdown worker gracefully


Starting in version >= 4, Celery comes with a special feature, just for Heroku, that supports this functionality out of the box:

$ REMAP_SIGTERM=SIGQUIT celery -A proj worker -l info

source: https://devcenter.heroku.com/articles/celery-heroku#using-remap_sigterm


celery was unfortunately not designed to do clean shutdown. EVER. I mean it. celery workers respond to SIGTERM but if a task is incomplete, the worker processes will wait to finish the task and only then exit. In which case, you can send it SIGKILL if the workers don't shut down in a reasonable time but there will be a loss of information in this case i.e. you may not know which jobs remained incomplete.


You can use acks_late or task_acks_late.

Tasks will be acknowledged from queue after task finished execution and not just before. So task will respawn if worker shutdown gracefully.