Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery - How to update the state of a task after the worker's being shutdown?

Setup

Celery 3.0

broker=RabbitMQ

Scenario

Tasks have already been acknowledged and started processing, and have state=STARTED. Then I want to restart my worker (to update the worker to a newer version). After restart worker (using supervisorctl restart), those long running tasks are all been terminated. But their states remain in state=STARTED. How can I update their state to FAILURE or whatever other values? (And, I don't want these tasks being executed again after the worker restarts.)

Methods tried (but not working)

  • use track_started=True --- If with this option, the tasks stay at state=STARTED after the worker restarts. If without this option, the tasks stay at state=PENDING after the worker restarts.
  • use CELERY_ACKS_LATE=True --- The tasks stay at state=STARTED after the worker restarts. And tasks are executed again, not a desired behavior.
  • use signal(SIGTERM, handler) and a handler function to catch the signal. The handler can successfully be entered. However, no matter what thing I put inside the handler, it can't change the task's state. The states just stay as the same and won't change to FAILURE. Inside the handler I've tried
    • raise Exception
    • exit(0)
    • exit(1)

Is there any settings of Celery that could enable it to track the state of task being shutdown?

like image 362
AnnieFromTaiwan Avatar asked Aug 27 '19 14:08

AnnieFromTaiwan


1 Answers

You need to revoke the tasks on worker shutdown. Take a look at this issue for the actual code.

like image 163
Tomáš Linhart Avatar answered Sep 30 '22 06:09

Tomáš Linhart