See docs here:
You can either provide
RRules strings or use the
rrules package to build the rules in code:
Note that: It is anticipated that the UI will be the primary interface for scheduling to make it easier and more flexible for users to define or adjust schedules without having to redeploy any code.
Here is an example to schedule your flow to run hourly but only during business days from 9 to 5:
from datetime import time, timedelta import pendulum from prefect.schedules import Schedule from prefect.schedules.clocks import IntervalClock from prefect.schedules.filters import between_times, is_weekday # respects DST clock = IntervalClock( start_date=pendulum.datetime(2021, 11, 12, 9, 0, tz="Europe/London"), interval=timedelta(hours=1), ) schedule = Schedule(clocks=[clock], filters=[is_weekday, between_times(time(9), time(17))]) for sched in schedule.next(20): print(sched)
Prefect 2.0 doesn’t require custom filters or adjustments as it relies on
rrule for complex scheduling. This makes it easier, as you can create and validate this schedule without Prefect!
import pendulum from dateutil.rrule import rrule, MO, TU, WE, TH, FR, HOURLY bus_hours = rrule( freq=HOURLY, interval=1, dtstart=pendulum.datetime(2022, 9, 29, 18, 0), byweekday=(MO, TU, WE, TH, FR), count=20, # add limit count for testing schedules - todo remove later byhour=list(range(9, 18)) ) for sched_date in bus_hours: print(sched_date)
Once you have validated that your schedule is working as intended, you may add that to your deployment as follows.
Generate rrule string:
Rrule string generated from the command above:
which you can pass to deployment CLI:
prefect deployment build your_flow.py:your_flow_fun -n prod --rrule 'DTSTART:20220929T180000\nRRULE:FREQ=HOURLY;COUNT=20;BYDAY=MO,TU,WE,TH,FR;BYHOUR=9,10,11,12,13,14,15,16,17'
rrule examples, check this documentation page: