8

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

Community
  • 1
  • 1
com.iavian
  • 377
  • 1
  • 5
  • 13

1 Answers1

14

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/

daniula
  • 6,898
  • 4
  • 32
  • 49