Add, modify, remove celery.schedules at run time

让人想犯罪 __ 提交于 2019-12-21 10:53:08

问题


is there a way to Add, modify, remove celery.schedules at run time. I need something that reads a db table periodically to know list of schedules.

Document says one can use djcelery.schedulers.DatabaseScheduler to achieve what I want, but not sure how to do it.

I read How to dynamically add / remove periodic tasks to Celery (celerybeat), still not clear

Thanks for help


回答1:


When you set in your app settings:

CELERYBEAT_SCHEDULER='djcelery.schedulers.DatabaseScheduler'

celery beat proces checks django PeriodicTask model to see what task should be executed.

You can add / modify / remove those tasks by modifying it using django model:

from djcelery.models import PeriodicTask, CrontabSchedule

every_hours_crontab = CrontabSchedule(minute=0)
every_hours_crontab.save()

periodic_task = PeriodicTask(
    name='Call my task every hour',
    task='myproject.tasks.mytask',
    crontab=every_hours_crontab,
    args=json.dump([arg1, arg2]),
    kwargs=json.dump({'foo': 'bar'})
)
periodic_task.save()

You can also test various configuration of PeriodicTask using django admin panel:
http://localhost:8000/admin/djcelery/crontabschedule/add/
http://localhost:8000/admin/djcelery/periodictask/



来源:https://stackoverflow.com/questions/23740880/add-modify-remove-celery-schedules-at-run-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!