简体   繁体   English

Django celery redis 从队列中删除特定的周期性任务

[英]Django celery redis remove a specific periodic task from queue

There is a specific periodic task that needs to be removed from message queue.有一个特定的周期性任务需要从消息队列中删除。 I am using the configuration of Redis and celery here.我这里使用的是Redis和celery的配置。

tasks.py任务.py

@periodic_task(run_every=crontab(minute='*/6'))
def task_abcd():
    """
    some operations here
    """

There are other periodic tasks also in the project but I need to stop this specific task to stop from now on.项目中还有其他定期任务,但我需要停止此特定任务以从现在开始停止。

As explained in this answer , the following code will work?正如在这个答案中所解释的,下面的代码会起作用吗?

@periodic_task(run_every=crontab(minute='*/6'))
def task_abcd():
    pass

In this example periodic task schedule is defined directly in code, meaning it is hard-coded and cannot be altered dynamically without code change and app re-deploy.在这个例子中,周期性任务计划是直接在代码中定义的,这意味着它是硬编码的,不能在不更改代码和重新部署应用程序的情况下动态更改。

The provided code with task logic deleted or with simple return at the beginning - will work, but will not be the answer to the question - task will still run, there just is no code that will run with it.提供的带有删除任务逻辑或在开始时简单return代码 - 将起作用,但不会成为问题的答案 - 任务仍将运行,只是没有代码可以与它一起运行。

Also, it is recommended NOT to use @periodic_task :此外, 建议不要使用@periodic_task

"""Deprecated decorator, please use :setting: beat_schedule .""" """不推荐使用的装饰器,请使用 :setting: beat_schedule 。"""

so it is not recommended to use it.所以不推荐使用。


First, change method from being @periodic_task to just regular celery @task , and because you are using Django - it is better to go straightforward for @shared_task :首先,将方法从@periodic_task更改为普通的 celery @task ,并且因为您使用的是 Django - 最好直接使用@shared_task

from celery import shared_task

@shared_task
def task_abcd():
    ...

Now this is just one of celery tasks, which needs to be called explicitly.现在这只是 celery 任务之一,需要显式调用。 Or it can be run periodically if added to celery beat schedule.或者,如果添加到 celery 节拍计划中,它可以定期运行。

For production and if using multiple workers it is not recommended to run celery worker with embedded beat (-B) - run separate instance of celery beat scheduler.对于生产,如果使用多个工作人员,不建议使用嵌入式节拍 (-B) 运行 celery worker - 运行单独的 celery beat 调度程序实例。

Schedule can specified in celery.py or in django project settings ( settings.py ).附表可以指定celery.py或Django项目的设置( settings.py )。

It is still not very dynamic, as to re-read settings app needs to be reloaded.它仍然不是很动态,因为重新读取设置应用程序需要重新加载。

Then, use Database Scheduler which will allow dynamically creating schedules - which tasks need to be run and when and with what arguments.然后,使用允许动态创建计划的数据库调度程序 - 需要运行哪些任务以及何时以及使用什么参数。 It even provides nice django admin web views for administration!它甚至为管理提供了不错的 django 管理 web 视图!

That code will work but I'd go for something that doesn't force you to update your code every time you need to disable/enable the task.该代码将起作用,但我会选择不强制您在每次需要禁用/启用任务时更新代码的方法。

What you could do is to use a configurable variable whose value could come from an admin panel, a configuration file, or whatever you want, and use that to return before your code runs if the task is in disabled mode.您可以做的是使用一个可配置变量,其值可以来自管理面板、配置文件或您想要的任何内容,并在任务处于禁用模式时使用它在代码运行之前返回。

For instance:例如:

@periodic_task(run_every=crontab(minute='*/6'))
def task_abcd():
    config = load_config_for_task_abcd()

    if not config.is_enabled:
        return

    # some operations here

In this way, even if your task is scheduled, its operations won't be executed.这样,即使你的任务被调度,它的操作也不会被执行。

If you simply want to remove the periodic task, have you tried to remove the function and then restart your celery service.如果您只是想删除周期性任务,您是否尝试删除该功能,然后重新启动 celery 服务。 You can restart your Redis service as well as your Django server for safe measure.为了安全起见,您可以重新启动 Redis 服务和 Django 服务器。

Make sure that the function you removed is not referenced anywhere else.确保您删除的函数未在其他任何地方引用。

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

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