How to inspect and cancel Celery tasks by task nam

2019-01-30 11:51发布

问题:

I'm using Celery (3.0.15) with Redis as a broker.

Is there a straightforward way to query the number of tasks with a given name that exist in a Celery queue?

And, as a followup, is there a way to cancel all tasks with a given name that exist in a Celery queue?

I've been through the Monitoring and Management Guide and don't see a solution there.

回答1:

# Retrieve tasks
# Reference: http://docs.celeryproject.org/en/latest/reference/celery.events.state.html
query = celery.events.state.tasks_by_type(your_task_name)

# Kill tasks
# Reference: http://docs.celeryproject.org/en/latest/userguide/workers.html#revoking-tasks
for uuid, task in query:
    celery.control.revoke(uuid, terminate=True)


回答2:

There is one issue that earlier answers have not addressed and may throw off people if they are not aware of it.

Among those solutions already posted, I'd use Danielle's with one minor modification: I'd import the task into my file and use its .name attribute to get the task name to pass to .tasks_by_type().

app.control.revoke(
    [uuid for uuid, _ in
     celery.events.state.State().tasks_by_type(task.name)])

However, this solution will ignore those tasks that have been scheduled for future execution. Like some people who commented on other answers, when I checked what .tasks_by_type() return I had an empty list. And indeed my queues were empty. But I knew that there were tasks scheduled to be executed in the future and these were my primary target. I could see them by executing celery -A [app] inspect scheduled but they were unaffected by the code above.

I managed to revoke the scheduled tasks by doing this:

app.control.revoke(
    [scheduled["request"]["id"] for scheduled in
     chain.from_iterable(app.control.inspect().scheduled()
                         .itervalues())])

app.control.inspect().scheduled() returns a dictionary whose keys are worker names and values are lists of scheduling information (hence, the need for chain.from_iterable which is imported from itertools). The task information is in the "request" field of the scheduling information and "id" contains the task id. Note that even after revocation, the scheduled task will still show among the scheduled tasks. Scheduled tasks that are revoked won't get removed from the list of scheduled tasks until their timers expire or until Celery performs some cleanup operation. (Restarting workers triggers such cleanup.)



回答3:

You can do this in one request:

app.control.revoke([
    uuid
    for uuid, _ in
    celery.events.state.State().tasks_by_type(task_name)
])


回答4:

It looks like flower provides monitoring:

https://github.com/mher/flower

Real-time monitoring using Celery Events

Task progress and history Ability to show task details (arguments, start time, runtime, and more) Graphs and statistics Remote Control

View worker status and statistics Shutdown and restart worker instances Control worker pool size and autoscale settings View and modify the queues a worker instance consumes from View currently running tasks View scheduled tasks (ETA/countdown) View reserved and revoked tasks Apply time and rate limits Configuration viewer Revoke or terminate tasks HTTP API

OpenID authentication