Periodical Execution

Time based scheduling is the base for most schedulers. Often it is interpret that a task is set to run when a given time is reached and the task has not yet run at that time, or given time has passed from the previous run. Time based scheduling can be divided into two categories:

  • Floating periods: Run when given time has passed from previous run

  • Fixed period: Run at given time of day, week etc.

Floating Periods

Perhaps the simplest scheduling is to run a task when a given amount of time has passed. This can be done by:

from rocketry.conds import every

@app.task(every('10 seconds'))
def do_constantly():
    ...

@app.task(every('1 minute'))
def do_minutely():
    ...

@app.task(every('1 hour'))
def do_hourly():
    ...

@app.task(every('1 day'))
def do_daily():
    ...

@app.task(every('2 days 2 hours 20 seconds'))
def do_custom():
    ...

Note

The condition every is linked running the task

Fixed Periods

It is also common to have a task to run once in some agreed fixed time span. Such time spans are:

  • hour: starts at 0 minute and ends at 60 minute

  • day: starts at 00:00 and ends at 24:00

  • week: starts on Monday at 00:00 and ends on Sunday at 24:00

  • month: starts at 1st at 00:00 and ends 28rd-31st at 24:00

Running a task every hour is different than running a task hourly in Rocketry. The difference is that the former runs every time after 60 minutes has passed but the latter every full hour. If time is now 07:15, the former will run at 08:15 but the latter will run at 08:00.

from rocketry.conds import minutely, hourly, daily, weekly, monthly

@app.task(minutely)
def do_minutely():
    ...

@app.task(hourly)
def do_hourly():
    ...

@app.task(daily)
def do_daily():
    ...

@app.task(weekly)
def do_weekly():
    ...

@app.task(monthly)
def do_monthly():
    ...

Constrained

The fixed periods can also be constrained using before, after and between:

  • before: From the beginning of the fixed period till the given time

  • after: From the given time to the end of the fixed period

  • between: From given start time to the given end time

So what this means in practice? Here is an illustration for a day/daily:

  • before 14:00: From 00:00 (0 am) to 14:00 (2 pm)

  • after 14:00: From 14:00 (2 pm) to 24:00 (12 pm)

  • between 08:00 and 16:00: From 08:00 (8 am) to 16:00 (4 pm)

and some illustations what this means for a week/weekly:

  • before Friday: From Monday 00:00 (0 am) to Friday 24:00 (12 pm)

  • after Friday: From Friday 00:00 (0 am) to Sunday 24:00 (12 pm)

  • between Tuesday and Friday: From Tuesday 00:00 (0 am) to Friday 24:00 (12 pm)

There are also on/at and starting methods:

  • on/at: On a given subunit of the period. The method on is an alias for at.

  • starting: The fixed period starts on given time

The subunit of a day is hour, the subunit of a week is day of week, subunit of a month is a day of month etc. To illustrate these options, on Friday means Friday 00:00 (0 am) to Tuesday 00:00, at 11:00 (daily) means from 11:00 to 12:00 and starting Friday means the week is set to start on Friday.

from rocketry.conds import minutely, hourly, daily, weekly, monthly

@app.task(minutely.before("45"))
def do_before():
    ...

@app.task(hourly.after("45:00"))
def do_after():
    ...

@app.task(daily.between("08:00", "14:00"))
def do_between():
    ...

@app.task(daily.at("11:00"))
def do_at():
    ...

@app.task(weekly.on("Monday"))
def do_on():
    ...

@app.task(monthly.starting("3rd"))
def do_starting():
    ...

Note

The logic follows natural language. Statement between Monday and Friday means Monday at 00:00 (0 am) to Friday 24:00 (12 pm).

There are also time of … conditions check if the current time is within the given period.