0

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,
    },
}

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

I installed this package. Could you show a simple usage example. On the example of my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.