skip to Main Content

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

3

Answers


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

    Login or Signup to reply.
  2. 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!

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search