Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revoke a task from celery

Tags:

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?

like image 290
PythonEnthusiast Avatar asked Aug 28 '16 12:08

PythonEnthusiast


1 Answers

How does 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)

Why after restarting celery, your revoked tasks executed?

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.

What can you do?

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:

  • Celery source code of the in-memory set
  • Revoke doc
  • Persistent revokes docs
like image 165
Or Duan Avatar answered Sep 17 '22 22:09

Or Duan