如何向Celery Beat动态添加计划任务 [英] How to dynamically add a scheduled task to Celery beat
问题描述
我使用Celery版本3.1.23,试图将预定任务动态添加到芹菜节拍中.我有一名芹菜工人和一名芹菜节拍实例正在运行.
Using Celery ver.3.1.23, I am trying to dynamically add a scheduled task to celery beat. I have one celery worker and one celery beat instance running.
在运行task.delay()时触发标准的芹菜任务.当我将预定的定期任务定义为配置中的设置时,芹菜节拍将其运行.
Triggering a standard celery task y running task.delay() works ok. When I define a scheduled periodic task as a setting in configuration, celery beat runs it.
但是,我需要的是能够添加在运行时在指定的crontab上运行的任务.将任务添加到持久性调度程序后,芹菜节拍似乎无法检测到新添加的新任务.我可以看到celery-schedule文件中确实有一个包含新任务的条目.
However what I need is to be able to add a task that runs at specified crontab at runtime. After adding a task to persistent scheduler, celery beat doesn't seem to detect the newly added new task. I can see that the celery-schedule file does have an entry with new task.
代码:
scheduler = PersistentScheduler(app=current_app, schedule_filename='celerybeat-schedule')
scheduler.add(name="adder",
task="app.tasks.add",
schedule=crontab(minute='*/1'),
args=(1,2))
scheduler.close()
当我跑步时:
print(scheduler.schedule)
我得到:
{'celery.backend_cleanup': <Entry: celery.backend_cleanup celery.backend_cleanup() <crontab: 0 4 * * * (m/h/d/dM/MY)>,
'adder': <Entry: adder app.tasks.add(1, 2) <crontab: */1 * * * * (m/h/d/dM/MY)>}
请注意,app.tasks.add具有 @ celery.task
装饰器.
Note that app.tasks.add has the @celery.task
decorator.
推荐答案
我建议您切换到 Celery Redbeat .
这篇关于如何向Celery Beat动态添加计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!