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

Removing celery tasks by name (with wildcard?)

有没有办法可以从 Celery 中删除一组特定的任务?也许使用通配符?类似于:

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

我知道我可以使用

删除所有任务
from proj.celery import app
app.control.purge()

来自 here,但这不是很有用,因为我似乎无法使用该代码来调整它并做我想做的事。

回答我自己的问题。这是我实现目标的代码的摘录:

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)