Add, modify, remove celery.schedules at run time

Is there any way to add, modify, remove celery.schedules at runtime. I need something that periodically reads the db table to find out a list of schedules.

The document says that to achieve what I want, you can use djcelery.schedulers.DatabaseScheduler , but not sure how to do it.

I read How to dynamically add / remove periodic jobs on Celery (celerybeat) , it is still unclear

thanks for the help

+8
python celery
source share
1 answer

When you set up your application:

 CELERYBEAT_SCHEDULER='djcelery.schedulers.DatabaseScheduler' 

celery beat checks the django PeriodicTask model to see which task should be performed.

You can add / modify / delete these tasks by changing it using the 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 PeriodicTask configurations using the django admin panel:
http://localhost:8000/admin/djcelery/crontabschedule/add/
http://localhost:8000/admin/djcelery/periodictask/

+11
source share

All Articles