Cron Scheduling

Rocketry also natively supports cron-like scheduling.

Cron

Condition cron checks whether the current time is inside specified the cron interval and the task has not run in this interval. It is useful for running a task the same way as cron would schedule it.

You can specify the cron statement using string:

from rocketry.conds import cron

@app.task(cron('* * * * *'))
def do_minutely():
    ...

@app.task(cron('*/2 12-18 * Oct Fri'))
def do_complex():
    "Run at every 2nd minute past every hour from 12 through 18 on Friday in October."
    ...

Or you can use named arguments:

from rocketry.conds import cron

@app.task(cron(minute="*/5"))
def do_simple():
    "Run at every 5th minute"
    ...


@app.task(cron(minute="*/2", hour="7-18", day_of_month="1,2,3", month="Feb-Aug/2"))
def do_complex():
    """Run at:
        - Every second minute
        - Between 07:00 (7 a.m.) - 18:00 (6 p.m.)
        - On 1st, 2nd and 3rd day of month
        - From February to August every second month
    """
    ...

See more from the official documentation of cron or test using crontab.

Note

Unlike most of the condition, the cron condition checks whether the task has run on the period (as standard with cron schedulers) and not whether the task has finished on the given period.

Crontime

Condition crontime is similar to cron but it is not tied to a task and it only checks whether the current time is inside the specified cron interval. It is useful if you want to augment the cron scheduling by adding retries or else.

from rocketry.conds import crontime

@app.task(crontime('* * * * *'))
def do_minutely():
    ...

@app.task(crontime('*/2 12-18 * Oct Fri'))
def do_complex():
    "Run at every 2nd minute past every hour from 12 through 18 on Friday in October."
    ...
from rocketry.conds import crontime

@app.task(crontime(minute="*/5"))
def do_simple():
    "Run at every 5th minute"
    ...


@app.task(crontime(minute="*/2", hour="7-18", day_of_month="1,2,3", month="Feb-Aug/2"))
def do_complex():
    """Run at:
        - Every second minute
        - Between 07:00 (7 a.m.) - 18:00 (6 p.m.)
        - On 1st, 2nd and 3rd day of month
        - From February to August every second month
    """
    ...