'Dynamically change celery beat schedule params

I get schedule values from .env file. And sometimes parameters in .env file change. Is it possible to change schedule values of already running celery beat tasks?

My celery.py:

import os
from celery import Celery
from celery.schedules import crontab
from dotenv import load_dotenv


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')

app = Celery('myproj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


load_dotenv()
orders_update_time = float(os.getenv("ORDERS_UPDATE_TIME"))
if not orders_update_time:
    orders_update_time = 60.0

orders_update_time = float(os.getenv("REMAINS_SEND_TIME"))
if not remains_send_time:
    remains_send_time = 60.0



app.conf.beat_schedule = {
    'wb_orders_autosaver': {
        'task': 'myapp.tasks.orders_autosave',
        'schedule': orders_update_time,
    },
    'wb_remains_autosender': {
        'task': 'myapp.tasks.remains_autosend',
        'schedule': remains_send_time,
    },
}


Solution 1:[1]

Yes, use django-celery-beat. That will allow you to save your schedule to the database and you can use the django admin ui to modify the schedule.


From django shell_plus, you can run the following commands to create your schedule:

schedule = CrontabSchedule(minute='0', hour='10')
schedule.save()
PeriodicTask.objects.create(
    crontab=schedule,
    task='myapp.tasks.orders_autosave',
    name='autosave orders',
)
schedule = CrontabSchedule(minute='15', hour='10')
schedule.save()
PeriodicTask.objects.create(
    crontab=schedule,
    task='myapp.tasks.remains_autosend',
    name='autosend remains',
)
PeriodicTasks.changed()

Or you can use the UI in the django admin panel:

  1. Select add periodic task

periodic tasks

  1. Enter in the information about your task and select save

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1