I want to explicitly revoke a task from celery. This is how I'm currently doing:-
from celery.task.control import revoke revoke(task_id, terminate=True)
where task_id is string
(have also tried converting it into UUID uuid.UUID(task_id).hex)
.
After the above procedure, when I start celery again celery worker -A proj
it still consumes the same message and starts processing it. Why?
When viewed via flower
, the message is still there in the broker section. how do I delete the message so that it cant be consumed again?
revoke
works?When calling the revoke
method the task doesn't get deleted from the queue immediately, all it does is tell celery(not your broker!) to save the task_id
in a in-memory set
(look here if you like reading source code like me).
When the task gets to the top of the queue, Celery will check if is it in the revoked set, if it does, it won't execute it.
It works this way to prevent O(n) search for each revoke
call, where checking if the task_id is in the in-memory set is just O(1)
Understanding how things works, you now know that the set
is just a normal python set, that being saved in-memory - that means when you restart, you lose this set, but the task is(of course) persistence and when the tasks turn comes, it will be executed as normal.
You will need to have a persistence set, this is done by initial your worker like this:
celery worker -A proj --statedb=/var/run/celery/worker.state
This will save the set on the filesystem.
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With