Here we show how to select all records that occur in an hour. This is useful for cases where you are running an async task at some interval smaller than a day. My first attempts at doing this type of filtering involved providing a start and end datetime. But there is a way to do it more simply by leveraging some database functions.
I needed to schedule microservice calls based on some business logic. We know with async tasks its better to add a field to your model with a time to be acted on rather than delaying tasks (5. Using a Long countdown or an eta in the Far Future.). So with an hourly task, its pretty natural to want to find all records that should be acted upon at that hour. The following example shows running some operation when an Event starts ๐ค.
This basically truncates the datetime start to the nearest hour. And because we run the task every hour we donโt have to worry about the minutes and seconds.
The crontab(minute=15) bit makes the task run at the 15th minute of every hour.
The celery crontab api provides an asterisk for the default values of args like hour
,
day_of_week
, etc. So an invocation like
is evaluated like
which is analagous to 15 * * * *
. See in crontab.
My original approach involved tracking start and end times
but you have to worry more about edgecases and this might be slower.
If you need help solving your business problems with software read how to hire me.