2

I have a Django site which allows users to create events that will start at a particular time on a particular date. Crucially, they can also change the start date/time of the event whenever they like, right up the time it is due to start. When the event starts, I need to run a particular task which will set up the next event.

Idea 1 - APScheduler

My initial thought was that I could use APScheduler for this but this isn't scalable because APScheduler runs in the same process as the web process so if I scale it up, the task is run more than once.

Idea 2 - celery/redis

Then I changed to using celery (and redis) but that has the problem that the only way to cancel an event (when the user changes the start time of an event, for instance) is to flag it as revoked in the celery worker, causing it not to be executed when redis kicks it off. This would be fine but it needs me to use the statedb argument to celery (to ensure the list of revoked tasks is maintained when the worker is restarted) and I can't do that as I'm using Heroku and their architecture doesn't allow it.

Idea 3 - Celery long term scheduler

There is an app called long term scheduler (https://github.com/ZeitOnline/celery_longterm_scheduler) which requires a cron job to regularly poll for tasks that need starting but this seems slightly flawed to me because the task is unlikely to run at the exact start time of the event, which is what I need.

Any other ideas?

Surely there is a way that I can cue up tasks (there are other tasks that need running at times based on the start time of the events) to be run at PRECISELY the time specified?

This seems like something that must be quite common but I can't seem to find a good way of doing it.

bodger
  • 1,112
  • 6
  • 24
  • Have you looked at the django-celery-beat extension? I think that might make it easier to manipulate the task queue. http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#beat-custom-schedulers – Håken Lid Nov 04 '18 at 19:03
  • Interesting - I thought celery beat was for periodic tasks - and that's what it seems to suggest it's for. But I guess you're suggesting I use it to schedule my single task (for each event) and then delete it once it's run or something, are you? – bodger Nov 04 '18 at 19:12

0 Answers0