makefile
export DJANGO_CELERY_BEAT_CRON_SCHEDULE="0 * * * *"
### Step 2: Load the Environment Variable in Django Settings
Load the environment variable in your Django settings file using the `os.environ` dictionary. For example, you can add the following code to your settings file:
python
import os
DJANGO_CELERY_BEAT_CRON_SCHEDULE = os.environ.get('DJANGO_CELERY_BEAT_CRON_SCHEDULE')
### Step 3: Configure Celery Beat to Use the Cron Schedule
Configure Celery Beat to use the cron schedule by passing the `DJANGO_CELERY_BEAT_CRON_SCHEDULE` variable to the `schedule` argument of the `CeleryBeat` class. For example, you can add the following code to your Django settings file:
python
from celery import Celery
from celery.schedules import crontab
app = Celery('my_app')
if DJANGO_CELERY_BEAT_CRON_SCHEDULE:
app.conf.beat_schedule = {
'my_task': {
'task': 'my_app.tasks.my_task',
'schedule': crontab(DJANGO_CELERY_BEAT_CRON_SCHEDULE),
},
}
else:
app.conf.beat_schedule = {
'my_task': {
'task': 'my_app.tasks.my_task',
'schedule': 60.0, # run every 60 seconds for testing
},
}
In the above code, we first import the `Celery` class and the `crontab` function from the `celery` package. We then create an instance of the `Celery` class with the name `my_app`. We then check if the `DJANGO_CELERY_BEAT_CRON_SCHEDULE` variable is defined and set the `beat_schedule` attribute of the `app.conf` object accordingly.
If the `DJANGO_CELERY_BEAT_CRON_SCHEDULE` variable is defined, we use the `crontab` function to create a cron schedule object and pass it to the `schedule` argument of the `my_task` task. If the `DJANGO_CELERY_BEAT_CRON_SCHEDULE` variable is not defined, we set the `schedule` argument to `60.0`, which means the task will run every 60 seconds for testing purposes.
### Step 4: Run Django Celery Beat
Finally, you can run Django Celery Beat using the `celery beat` command. For example, you can add the following code to your Django manage.py file:
python
import os
from celery import Celery
from django.core.management.base import BaseCommand
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')
app = Celery('my_app')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
class Command(BaseCommand):
help = 'Start Celery beat'
def handle(self, *args, **options):
from celery.bin import beat
beat(app)
In the above code, we first import the `Celery` class, the `BaseCommand` class, and the `beat` function from the `celery.bin` module. We then set the `DJANGO_SETTINGS_MODULE` environment variable to the name of your Django settings file. We then create an instance of the `Celery` class with the name `my_app` and configure it to use the Django settings. We then autodiscover the Celery tasks using the `autodiscover_tasks` method.
We then define a custom `Command` class that inherits from the `BaseCommand` class and override the `handle` method to start Celery beat using the `beat` function. Finally, we register the `Command` class as a management command using the `Command` class from the `django.core.management.base` module.
You can now run Django Celery Beat using the `python manage.py celery beat` command.
Conclusion
----------
In this article, we discussed how to configure the Django Celery Beat cron schedule using an environment variable. By using an environment variable, you can make it easier to change the schedule without modifying your code. We also discussed how to load the environment variable in Django settings, configure Celery Beat to use the cron schedule, and run Django Celery Beat.
References
----------
*
*
Summary
-------
* Configuring Django Celery Beat cron schedule using environment variable
* Defining environment variable with name `DJANGO_CELERY_BEAT_CRON_SCHEDULE`
* Loading environment variable in Django settings file
* Configuring Celery Beat to use the cron schedule
* Running Django Celery Beat using `celery beat` command
References
----------
*
*