0

I schedule repeat tasks for my Django app via including them in the CELERYBEAT_SCHEDULE dictionary in my settings.py. For instance:

CELERYBEAT_SCHEDULE = {
    'tasks.rank_photos': {
        'task': 'tasks.rank_photos',
        'schedule': timedelta(seconds=5*60),
    },
    'tasks.trim_whose_online': {
        'task': 'tasks.trim_whose_online',
        'schedule': timedelta(seconds=10*60),
    },
}

These tasks periodically run (for the life of the app).

I was wondering whether there's a way for a regular user of my app to kick off a periodic task? I.e. is there a way to control this kind of scheduling from views.py? If not, why not? And if yes, an illustrative example would be great. Thanks in advance.

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
  • You could use django-celery-beat package(https://pypi.python.org/pypi/django_celery_beat). It defines several models (e.g. PeriodicTask) and it will allow you to schedule tasks in your views by simply using those models to create or edit periodic tasks. It is mentioned in the official docs (http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html) `There’s also the django-celery-beat extension that stores the schedule in the Django database, and presents a convenient admin interface to manage periodic tasks at runtime.` – mateuszb Feb 11 '17 at 15:45
  • 1
    @mateuszb: thanks for the assist. Going through the docs once again - missed this one. You can add it as an answer and I'll accept it. – Hassan Baig Feb 11 '17 at 15:49

1 Answers1

1

You could use django-celery-beat package. It defines several models (e.g. PeriodicTask) and it will allow you to schedule tasks in your views by simply using those models to create or edit periodic tasks. It is mentioned in the official docs.

There’s also the django-celery-beat extension that stores the schedule in the Django database, and presents a convenient admin interface to manage periodic tasks at runtime.

mateuszb
  • 1,083
  • 10
  • 20