Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django + celery - How do I set up a crontab schedule for celery in my django app?

I am following the instructions here:

http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html#crontab-schedules

I'm supposed to be able to do the following: from celery.schedules import crontab

In my settings.py I have:

from kombu import serialization
serialization.registry._decoders.pop("application/x-python-serialize")
import djcelery
djcelery.setup_loader()
from celery.schedules import crontab

...

CELERYBEAT_SCHEDULE = {
    'first_task': {
        'task': 'apps.icecream.tasks.sync_flavors',
        'schedule': crontab(minute='*/30', hour='1, 3, 6, 8-20, 22')
    },
    'second_task': {
        'task': 'apps.robots.tasks.run_robots',
        'schedule': crontab(minute='*/6')
    }
}

However, I'm getting an error: "No module named schedules"

If I switch to the other way of scheduling, using timedelta, then everything is fine and I can get my periodic tasks to run:

CELERYBEAT_SCHEDULE = {
    'first_task': {
        'task': 'apps.icecream.tasks.sync_flavors',
        'schedule': timedelta(minutes=30)
    },
    'second_task': {
        'task': 'apps.robots.tasks.run_robots',
        'schedule': timedelta(minutes=6)
    }
}

Why can't I use the crontab approach?

like image 958
tadasajon Avatar asked May 28 '14 17:05

tadasajon


People also ask

How do I schedule a task in Django Celery?

First, open a new shell or window. In that shell, set up the same Django development environment - activate your virtual environment, or add things to your Python path, whatever you do so that you could use runserver to run your project.

How do you run a Celery scheduler?

To begin, let's first set up the Flask framework with Celery. Save this python script as app.py in the main project directory, and in your terminal, run: python ~/celery-scheduler/app.py . Now go to http://localhost:5000/. If everything is running fine, you should see “Hello, Flask is up and running!”

What is Celery job scheduler?

Introduction. celery beat is a scheduler; It kicks off tasks at regular intervals, that are then executed by available worker nodes in the cluster. By default the entries are taken from the beat_schedule setting, but custom stores can also be used, like storing the entries in a SQL database.


1 Answers

This happens because you have a celery.py file in the same package as your settings.py, which shadows the global celery package.

To get around this, insert the following string at the beginning of the settings.py:

from __future__ import absolute_import

Hope it helps!

like image 63
Albert Tugushev Avatar answered Sep 28 '22 04:09

Albert Tugushev