按名称删除celery任务(使用通配符?)

时间:2019-01-03 23:09:52

标签: python django python-3.x celery

是否可以从Celery中删除一组特定的任务?也许使用通配符?像这样:

app.control.delete("foobar-only-*")

我知道我可以删除所有任务

from proj.celery import app
app.control.purge()

来自here,但这并不是很有用,因为我似乎无法使用该代码对其进行调整并执行我想要的操作。

1 个答案:

答案 0 :(得分:0)

回答我自己的问题。这是我实现目标的代码的一部分:

def stop_crawler(crawler_name):
    crawler = Crawler.objects.get(name=crawler_name)
    if crawler is None:
        logger.error(f"Can't find a crawler named {crawler_name}")
        return

    i = app.control.inspect()

    queue_name = f"collect_urls_{crawler_name}"

    # Iterate over all workers, and the queues of each worker, and stop workers
    # from consuming from the queue that belongs to the crawler we're stopping
    for worker_name, worker_queues in i.active_queues().items():
        for queue in worker_queues:
            if queue["name"] == queue_name:
                app.control.cancel_consumer(queue_name, reply=True)

    # Iterate over the different types of tasks and stop the ones that belong
    # to the crawler we're stopping
    for queue in [i.active, i.scheduled, i.reserved]:
        for worker_name, worker_tasks in queue().items():
            for task in worker_tasks:
                args = ast.literal_eval(task["args"])
                if "collect_urls" in task["name"] and args[0] == crawler_name:
                    app.control.revoke(task["id"], terminate=True)