How can I run a celery task at a given time, but only once?
I read the documentation and couldn't find any example of this.
To run a task at a specified time, in Celery you would normally use a periodic task, which conventionally is a recurring task.
However, you may create a periodic task with a very specific schedule and condition that happens only once so effectively it runs only once.
Unfortunately we can only specify so much, e.g. we can specify hour
, minute
, day_of_month
and month_of_year
but we can't specify year
However with that, your task would run at most 1 time for per year, so below are some workarounds:
Unschedule it after it is ran
It should be relatively easy to unschedule it once it is ran (you have 1 year to do so!)
Use a "DONE" flag when the task completes
With a flag written somewhere (disk or DB), you can first check if the task has run before or not, i.e. if done: exit
Exit if not proper year
or you want to be safe, just add code into the task that checks the year, e.g. if year != 2017: exit
.
You may also skip Celery altogether and use some OS level facility like cron for UNIX-like systems, more on that here.
The general idea remains the same.