简体   繁体   English

如何使用 redis 代理从 celery 中删除任务?

[英]how to remove task from celery with redis broker?

I Have add some wrong task to a celery with redis broker我用 redis brokercelery添加了一些错误的task

but now I want to remove the incorrect task and I can't find any way to do this但现在我想删除不正确的task ,我找不到任何方法来做到这一点

Is there some commands or some api to do this ?是否有一些命令或一些 api 来做到这一点?

I know two ways of doing so:我知道这样做的两种方法:

1) Delete queue directly from broker. 1) 直接从代理删除队列。 In your case it's Redis.在您的情况下,它是Redis。 There are two commands that could help you: llen (to find right queue) and del (to delete it).有两个命令可以帮助您: llen (查找正确的队列)和del (删除它)。

2) Start celery worker with --purge or --discard options. 2) 使用--purge--discard选项启动 celery worker。 Here is help:这是帮助:

--purge, --discard    Purges all waiting tasks before the daemon is started.
                      **WARNING**: This is unrecoverable, and the tasks will
                      be deleted from the messaging server.

The simplest way is to use the celery control revoke [id1 [id2 [... [idN]]]] (do not forget to pass the -A project.application flag too).最简单的方法是使用celery control revoke [id1 [id2 [... [idN]]]] (不要忘记传递-A project.application标志)。 Where id1 to idN are task IDs.其中 id1 到 idN 是任务 ID。 However, it is not guaranteed to succeed every time you run it, for valid reasons...但是,由于正当理由,不能保证每次运行它都会成功......

Sure Celery has API for it.当然 Celery 有 API。 Here is an example how to do it from a script: res = app.control.revoke(task_id, terminate=True)以下是如何从脚本执行此操作的示例: res = app.control.revoke(task_id, terminate=True)

In the example above app is an instance of the Celery application.在上面的示例中, app是 Celery 应用程序的一个实例。

In some rare ocasions the control command above will not work, in which case you have to instruct Celery worker to kill the worker process: res = app.control.revoke(task_id, terminate=True, signal='SIGKILL')在一些罕见的情况下,上面的控制命令将不起作用,在这种情况下,您必须指示 Celery worker res = app.control.revoke(task_id, terminate=True, signal='SIGKILL')工作进程: res = app.control.revoke(task_id, terminate=True, signal='SIGKILL')

I just had this problem so for future readers,我刚刚遇到了这个问题,所以对于未来的读者,

http://celery.readthedocs.org/en/latest/faq.html#i-ve-purged-messages-but-there-are-still-messages-left-in-the-queue http://celery.readthedocs.org/en/latest/faq.html#i-ve-purged-messages-but-there-are-still-messages-left-in-the-queue

so to properly purge the queue of waiting tasks you have to stop all the workers , and then purge the tasks using celery.control.purge().因此,要正确清除等待任务的队列,您必须停止所有工作人员,然后使用 celery.control.purge() 清除任务。

1. To properly purge the queue of waiting tasks you have to stop all the workers ( http://celery.readthedocs.io/en/latest/faq.html#i-ve-purged-messages-but-there-are-still-messages-left-in-the-queue ): 1.要正确清除等待任务的队列,您必须停止所有工作人员( http://celery.readthedocs.io/en/latest/faq.html#i-ve-purged-messages-but-there-are-仍然在队列中留下的消息):

2 ... and then purge the tasks from a specific queue: 2 ...然后从特定队列中清除任务:

$ cd <source_dir
$ celery amqp queue.purge <queue name>

3. Start workers again 3. 重新开始工作

尝试删除 .state 文件,如果您使用的是 beat worker(celery worker -B),那么也删除 schedule 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM