Search code examples
rabbitmqcelerydjango-celery

Celery task schedule (Celery, Django and RabbitMQ)


I want to have a task that will execute every 5 minutes, but it will wait for last execution to finish and then start to count this 5 minutes. (This way I can also be sure that there is only one task running) The easiest way I found is to run django application manage.py shell and run this:

while True:
    result = task.delay()
    result.wait()
    sleep(5)

but for each task that I want to execute this way I have to run it's own shell, is there an easy way to do it? May be some king custom ot django celery scheduler?


Solution

  • All you need is specify in celery conf witch task you want to run periodically and with which interval.

    Example: Run the tasks.add task every 30 seconds

    from datetime import timedelta
    
    CELERYBEAT_SCHEDULE = {
        "runs-every-30-seconds": {
            "task": "tasks.add",
            "schedule": timedelta(seconds=30),
            "args": (16, 16)
         },
    }
    

    Remember that you have to run celery in beat mode with the -B option

    manage celeryd -B
    

    You can also use the crontab style instead of time interval, checkout this:

    http://ask.github.com/celery/userguide/periodic-tasks.html

    If you are using django-celery remember that you can also use tha django db as scheduler for periodic tasks, in this way you can easily add trough the django-celery admin panel new periodic tasks. For do that you need to set the celerybeat scheduler in settings.py in this way

    CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"