Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does CELERYBEAT_SCHEDULE go in your project?

I've read several pages of Google results multiple times and I'm very confused about how to layout my project. I have managed to get Celerybeat working using the periodic_task decorator, but that is depreciated and is being removed. From what I understand, the docs suggest that CELERYBEAT_SCHEDULE is the replacement. I've created a file exactly as shown, but haven't yet figured out:

  • How to name it?
  • Where to put it?

I find it very hard to find the right way to use Celery in a non-Django project.

like image 976
Bruno Bronosky Avatar asked Mar 03 '15 14:03

Bruno Bronosky


1 Answers

It's a bit confusing that the docs make it look like your CELERYBEAT_SCHEDULE is a separate file. In reality, it's an entry in your app (aka: Celery() instance) config as you can see it listed here. So however you get the configuration into your app, that's where it goes.

There are many ways to get the configuration into the app. If you believe "Explicit is better than implicit", then you probably want to:

  1. Put your config into a module, like celeryconfig.py
  2. Import the module where your app is define or used. import celeryconfig
  3. Apply the module object to your app. app.config_from_object(celeryconfig)

Example celeryconfig.py file

from datetime import timedelta

BROKER_URL = "redis://redis.local:6379/0"
BROKER_TRANSPORT_OPTIONS = {'fanout_prefix': True, 'fanout_patterns': True, 'visibility_timeout': 480}
CELERY_RESULT_BACKEND = BROKER_URL

CELERYBEAT_SCHEDULE = {
    'addrandom-to-16K-every-2-seconds': {
        'task': 'celery_test.tasks.addrandom', # notice that the complete name is needed
        'schedule': timedelta(seconds=2),
        'args': (16000, 42)
    },
}

CELERY_TIMEZONE = 'UTC'

Trying to put it into a file like beatschedule.py and then running celery -A beatschedule beat will get you AttributeError: 'module' object has no attribute 'celery'

I created this project to demo Celerybeat in action as described in the docs. I believe it does a better job of showing how it all works than reading the docs alone. I also created this Docker image to make getting it up and running as simple as possible.

like image 143
Bruno Bronosky Avatar answered Sep 28 '22 19:09

Bruno Bronosky