简体   繁体   中英

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.

tasks.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.

Also, it is recommended NOT to use @periodic_task :

"""Deprecated decorator, please use :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 :

from celery import shared_task

@shared_task
def task_abcd():
    ...

Now this is just one of celery tasks, which needs to be called explicitly. Or it can be run periodically if added to celery beat schedule.

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.

Schedule can specified in celery.py or in django project settings ( 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!

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. You can restart your Redis service as well as your Django server for safe measure.

Make sure that the function you removed is not referenced anywhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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