停止/清除 Django-Celery 中的定期任务 [英] Stopping/Purging Periodic Tasks in Django-Celery

查看:80
本文介绍了停止/清除 Django-Celery 中的定期任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法通过继承 PeriodicTask 让周期性任务在 django-celery 中工作.我试图创建一个测试任务并让它运行一些无用的东西.有用.

I have managed to get periodic tasks working in django-celery by subclassing PeriodicTask. I tried to create a test task and set it running doing something useless. It works.

现在我无法阻止它.我已阅读文档,但无法找到如何从执行队列中删除任务.我尝试过使用 celeryctl 和 shell,但是 registry.tasks() 是空的,所以我看不到如何删除它.

Now I can't stop it. I've read the documentation and I cannot find out how to remove the task from the execution queue. I have tried using celeryctl and using the shell, but registry.tasks() is empty, so I can't see how to remove it.

我看到了我应该撤销"它的建议,但为此我似乎需要一个任务 ID,而且我看不到如何找到任务 ID.

I have seen suggestions that I should "revoke" it, but for this I appear to need a task id, and I can't see how I would find the task id.

谢谢.

推荐答案

任务就是消息,周期性任务"会定期发送任务消息.每个发送的任务都将分配一个唯一的 ID.

A task is a message, and a "periodic task" sends task messages at periodic intervals. Each of the tasks sent will have an unique id assigned to it.

revoke 只会取消单个任务消息.要获取您必须保留的任务的 ID跟踪发送的 id,但您也可以在发送任务时指定自定义 id.

revoke will only cancel a single task message. To get the id for a task you have to keep track of the id sent, but you can also specify a custom id when you send a task.

我不确定您是要取消单个任务消息,还是要停止周期性任务发送更多消息,所以我将列出两者的答案.

I'm not sure if you want to cancel a single task message, or if you want to stop the periodic task from sending more messages, so I'll list answers for both.

没有内置的方法来保存与周期性任务一起发送的任务的 id,但是您可以将每个任务的 id 设置为周期性任务的名称,这样id 将引用与周期性任务一起发送的任何任务(通常是最后一个).您可以通过这种方式指定自定义 id,

There is no built-in way to keep the id of a task sent with periodic tasks, but you could set the id for each task to the name of the periodic task, that way the id will refer to any task sent with the periodic task (usually the last one). You can specify a custom id this way,

使用 @periodic_task 装饰器:

@periodic_task(options={"task_id": "my_periodic_task"})
def my_periodic_task():
    pass

或使用 CELERYBEAT_SCHEDULE 设置:

CELERYBEAT_SCHEDULE = {name: {"task": task_name,
                              "options": {"task_id": name}}}

如果您想删除周期性任务,只需从代码库中删除 @periodic_task,或从 CELERYBEAT_SCHEDULE 中删除条目.如果您使用的是 Django 数据库调度程序,则必须删除周期性任务从 Django 管理界面.

If you want to remove a periodic task you simply remove the @periodic_task from the codebase, or remove the entry from CELERYBEAT_SCHEDULE. If you are using the Django database scheduler you have to remove the periodic task from the Django Admin interface.

PS1:revoke 不会停止已经开始的任务.它只会取消尚未开始的任务.您可以使用终止正在运行的任务撤销(task_id, terminate=True).默认情况下,这会将 TERM 信号发送到该过程,如果您想发送另一个信号(例如 KILL),请使用revoke(task_id, terminate=True, signal="KILL").

PS1: revoke doesn't stop a task that has already been started. It only cancels tasks that haven't been started yet. You can terminate a running task using revoke(task_id, terminate=True). By default this will send the TERM signal to the process, if you want to send another signal (e.g. KILL) use revoke(task_id, terminate=True, signal="KILL").

PS2:revoke 是一个远程控制命令,所以它只被 RabbitMQ 支持和 Redis 代理传输.如果您希望您的任务支持取消,您应该通过存储 cancelled 来实现在数据库中标记并让任务在启动时检查该标记:

PS2: revoke is a remote control command so it is only supported by the RabbitMQ and Redis broker transports. If you want your task to support cancellation you should do so by storing a cancelled flag in a database and have the task check that flag when it starts:

from celery.task import Task

class RevokeableTask(Task):
    """Task that can be revoked.

    Example usage:

        @task(base=RevokeableTask)
        def mytask():
            pass
    """

    def __call__(self, *args, **kwargs):
        if revoke_flag_set_in_db_for(self.request.id):
            return
        super(RevokeableTask, self).__call__(*args, **kwargs)

这篇关于停止/清除 Django-Celery 中的定期任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆