Add, modify, remove celery.schedules at run time

2019-04-06 14:47发布

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

标签: python celery
1条回答
老娘就宠你
2楼-- · 2019-04-06 15:21

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/

查看更多
登录 后发表回答