动态更改芹菜节拍时间表参数

发布于 2025-01-28 17:03:07 字数 917 浏览 4 评论 0原文

我从.env文件中获得计划值。有时在.env文件中更改。 是否可以更改已经运行的芹菜节拍任务的时间表值?

我的芹菜.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,
    },
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2025-02-04 17:03:07

是的,使用 django-celery-beat 。这将使您可以将时间表保存到数据库中,并且可以使用Django Admin UI修改时间表。


在Django Shell_plus中,您可以运行以下命令来创建您的时间表:

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()

或者您可以在Django Admin面板中使用UI:

  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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文