Skip to content

Schedules

Scheduling is one of the primary reasons for using an orchestrator such as Prefect. Prefect allows you to use schedules to automatically create new flow runs for deployments.

Prefect Cloud can also schedule flow runs through event-driven automations.

Schedules tell the Prefect API how to create new flow runs for you automatically on a specified cadence.

You can add a schedule to any deployment. The Prefect Scheduler service periodically reviews every deployment and creates new flow runs according to the schedule configured for the deployment.

Support for multiple schedules

We are currently rolling out support for multiple schedules per deployment. You can now assign multiple schedules to deployments in the Prefect UI, the CLI via prefect deployment schedule commands, the Deployment class, and in block-based deployment YAML files.

Support for multiple schedules in flow.serve, flow.deploy, serve, and worker-based deployments with prefect deploy will arrive soon.

Schedule types

Prefect supports several types of schedules that cover a wide range of use cases and offer a large degree of customization:

  • Cron is most appropriate for users who are already familiar with cron from previous use.
  • Interval is best suited for deployments that need to run at some consistent cadence that isn't related to absolute time.
  • RRule is best suited for deployments that rely on calendar logic for simple recurring schedules, irregular intervals, exclusions, or day-of-month adjustments.

Schedules can be inactive

When you create or edit a schedule, you can set the active property to False in Python (or false in a YAML file) to deactivate the schedule. This is useful if you want to keep the schedule configuration but temporarily stop the schedule from creating new flow runs.

Cron

A schedule may be specified with a cron pattern. Users may also provide a timezone to enforce DST behaviors.

Cron uses croniter to specify datetime iteration with a cron-like format.

Cron properties include:

Property Description
cron A valid cron string. (Required)
day_or Boolean indicating how croniter handles day and day_of_week entries. Default is True.
timezone String name of a time zone. (See the IANA Time Zone Database for valid time zones.)

How the day_or property works

The day_or property defaults to True, matching the behavior of cron. In this mode, if you specify a day (of the month) entry and a day_of_week entry, the schedule will run a flow on both the specified day of the month and on the specified day of the week. The "or" in day_or refers to the fact that the two entries are treated like an OR statement, so the schedule should include both, as in the SQL statement SELECT * FROM employees WHERE first_name = 'Xiāng' OR last_name = 'Brookins';.

For example, with day_or set to True, the cron schedule * * 3 1 2 runs a flow every minute on the 3rd day of the month (whatever that is) and on Tuesday (the second day of the week) in January (the first month of the year).

With day_or set to False, the day (of the month) and day_of_week entries are joined with the more restrictive AND operation, as in the SQL statement SELECT * from employees WHERE first_name = 'Andrew' AND last_name = 'Brookins';. For example, the same schedule, when day_or is False, runs a flow on every minute on the 3rd Tuesday in January. This behavior matches fcron instead of cron.

Supported croniter features

While Prefect supports most features of croniter for creating cron-like schedules, we do not currently support "R" random or "H" hashed keyword expressions or the schedule jittering possible with those expressions.

Daylight saving time considerations

If the timezone is a DST-observing one, then the schedule will adjust itself appropriately.

The cron rules for DST are based on schedule times, not intervals. This means that an hourly cron schedule fires on every new schedule hour, not every elapsed hour. For example, when clocks are set back, this results in a two-hour pause as the schedule will fire the first time 1am is reached and the first time 2am is reached, 120 minutes later.

Longer schedules, such as one that fires at 9am every morning, will adjust for DST automatically.

Interval

An Interval schedule creates new flow runs on a regular interval measured in seconds. Intervals are computed using an optional anchor_date. For example, here's how you can create a schedule for every 10 minutes in a block-based deployment YAML file:

schedule:
  interval: 600
  timezone: America/Chicago 

Interval properties include:

Property Description
interval datetime.timedelta indicating the time between flow runs. (Required)
anchor_date datetime.datetime indicating the starting or "anchor" date to begin the schedule. If no anchor_date is supplied, the current UTC time is used.
timezone String name of a time zone, used to enforce localization behaviors like DST boundaries. (See the IANA Time Zone Database for valid time zones.)

Note that the anchor_date does not indicate a "start time" for the schedule, but rather a fixed point in time from which to compute intervals. If the anchor date is in the future, then schedule dates are computed by subtracting the interval from it. Note that in this example, we import the Pendulum Python package for easy datetime manipulation. Pendulum isn’t required, but it’s a useful tool for specifying dates.

Daylight saving time considerations

If the schedule's anchor_date or timezone are provided with a DST-observing timezone, then the schedule will adjust itself appropriately. Intervals greater than 24 hours will follow DST conventions, while intervals of less than 24 hours will follow UTC intervals.

For example, an hourly schedule will fire every UTC hour, even across DST boundaries. When clocks are set back, this will result in two runs that appear to both be scheduled for 1am local time, even though they are an hour apart in UTC time.

For longer intervals, like a daily schedule, the interval schedule will adjust for DST boundaries so that the clock-hour remains constant. This means that a daily schedule that always fires at 9am will observe DST and continue to fire at 9am in the local time zone.

RRule

An RRule scheduling supports iCal recurrence rules (RRules), which provide convenient syntax for creating repetitive schedules. Schedules can repeat on a frequency from yearly down to every minute.

RRule uses the dateutil rrule module to specify iCal recurrence rules.

RRules are appropriate for any kind of calendar-date manipulation, including simple repetition, irregular intervals, exclusions, week day or day-of-month adjustments, and more. RRules can represent complex logic like:

  • The last weekday of each month
  • The fourth Thursday of November
  • Every other day of the week

RRule properties include:

Property Description
rrule String representation of an RRule schedule. See the rrulestr examples for syntax.
timezone String name of a time zone. See the IANA Time Zone Database for valid time zones.

You may find it useful to use an RRule string generator such as the iCalendar.org RRule Tool to help create valid RRules.

For example, the following RRule schedule in a block-based deployment YAML file creates flow runs on Monday, Wednesday, and Friday until July 30, 2024.

schedule:
  rrule: 'FREQ=WEEKLY;BYDAY=MO,WE,FR;UNTIL=20240730T040000Z'

RRule restrictions

Note the max supported character length of an rrulestr is 6500 characters

Note that COUNT is not supported. Please use UNTIL or the /deployments/{id}/runs endpoint to schedule a fixed number of flow runs.

Daylight saving time considerations

Note that as a calendar-oriented standard, RRules are sensitive to the initial timezone provided. A 9am daily schedule with a DST-aware start date will maintain a local 9am time through DST boundaries. A 9am daily schedule with a UTC start date will maintain a 9am UTC time.

Creating schedules

There are several ways to create a schedule for a deployment:

  • Through the Prefect UI
  • Via the cron, interval, or rrule parameters if building your deployment via the serve method of the Flow object or the serve utility for managing multiple flows simultaneously
  • If using worker-based deployments
  • When you define a deployment with flow.serve or flow.deploy
  • Through the interactive prefect deploy command
  • With the deployments -> schedules section of the prefect.yaml file
  • If using block-based deployments - Deprecated
  • Through the schedules section of the deployment YAML file
  • By passing schedules into the Deployment class or Deployment.build_from_flow

Creating schedules in the UI

You can add schedules in the Schedules section on a Deployment page in the UI.

Locating the Schedules section

The Schedules section appears in the sidebar on the right side of the page on wider displays. On narrower displays, it appears on the Details tab of the page.

Adding a schedule

Under Schedules, select the + Schedule button. A modal dialog will open. Choose Interval or Cron to create a schedule.

Prefect UI with Interval button selected

What about RRule?

The UI does not support creating RRule schedules. However, the UI will display RRule schedules that you've created via the command line.

The new schedule will appear on the Deployment page where you created it. In addition, the schedule will be viewable in human-friendly text in the list of deployments on the Deployments page.

After you create a schedule, new scheduled flow runs will be visible in the Upcoming tab of the Deployment page where you created it.

Editing schedules

You can edit a schedule by selecting Edit from the three-dot menu next to a schedule on a Deployment page.

Creating schedules with a Python deployment creation file

When you create a deployment in a Python file with flow.serve(), serve, flow.deploy(), or deploy you can specify the schedule. Just add the keyword argument cron, interval, or rrule.

interval: An interval on which to execute the deployment. Accepts a number or a 
    timedelta object to create a single schedule. If a number is given, it will be 
    interpreted as seconds. Also accepts an iterable of numbers or timedelta to create 
    multiple schedules.
cron: A cron schedule string of when to execute runs of this deployment. 
    Also accepts an iterable of cron schedule strings to create multiple schedules.
rrule: An rrule schedule string of when to execute runs of this deployment.
    Also accepts an iterable of rrule schedule strings to create multiple schedules.
schedules: A list of schedule objects defining when to execute runs of this deployment.
    Used to define multiple schedules or additional scheduling options such as `timezone`.
schedule: A schedule object defining when to execute runs of this deployment. Used to
    define additional scheduling options like `timezone`.

Here's an example of creating a cron schedule with serve for a deployment flow that will run every minute of every day:

my_flow.serve(name="flowing", cron="* * * * *")

If using work pool-based deployments, the deploy method has the same schedule-based parameters.

Here's an example of creating an interval schedule with serve for a deployment flow that will run every 10 minutes with an anchor date and a timezone:

from datetime import timedelta, datetime
from prefect.client.schemas.schedules import IntervalSchedule

my_flow.serve(name="flowing", schedule=IntervalSchedule(interval=timedelta(minutes=10), anchor_date=datetime(2023, 1, 1, 0, 0), timezone="America/Chicago"))

Block and agent-based deployments with Python files are not a recommended way to create deployments. However, if you are using that deployment creation method you can create a schedule by passing a schedule argument to the Deployment.build_from_flow method.

Here's how you create the equivalent schedule in a Python deployment file.

from prefect.server.schemas.schedules import CronSchedule

cron_demo = Deployment.build_from_flow(
    pipeline,
    "etl",
    schedule=(CronSchedule(cron="0 0 * * *", timezone="America/Chicago"))
)

Creating schedules with the interactive prefect deploy command

If you are using worker-based deployments, you can create a schedule through the interactive prefect deploy command. You will be prompted to choose which type of schedule to create.

Creating schedules in the prefect.yaml file's deployments -> schedule section

If you save the prefect.yaml file from the prefect deploy command, you will see it has a schedules section for your deployment. Alternatively, you can create a prefect.yaml file from a recipe or from scratch and add a schedules section to it.

deployments:
  ...
  schedules:
    - cron: "0 0 * * *"
      timezone: "America/Chicago"
      active: false
    - cron: "0 12 * * *"
      timezone: "America/New_York"
      active: true
    - cron: "0 18 * * *"
      timezone: "Europe/London"
      active: true

The Scheduler service

The Scheduler service is started automatically when prefect server start is run and it is a built-in service of Prefect Cloud.

By default, the Scheduler service visits deployments on a 60-second loop, though recently-modified deployments will be visited more frequently. The Scheduler evaluates each deployment's schedules and creates new runs appropriately. For typical deployments, it will create the next three runs, though more runs will be scheduled if the next 3 would all start in the next hour.

More specifically, the Scheduler tries to create the smallest number of runs that satisfy the following constraints, in order:

  • No more than 100 runs will be scheduled.
  • Runs will not be scheduled more than 100 days in the future.
  • At least 3 runs will be scheduled.
  • Runs will be scheduled until at least one hour in the future.

These behaviors can all be adjusted through the relevant settings that can be viewed with the terminal command prefect config view --show-defaults:

PREFECT_API_SERVICES_SCHEDULER_DEPLOYMENT_BATCH_SIZE='100'
PREFECT_API_SERVICES_SCHEDULER_ENABLED='True'
PREFECT_API_SERVICES_SCHEDULER_INSERT_BATCH_SIZE='500'
PREFECT_API_SERVICES_SCHEDULER_LOOP_SECONDS='60.0'
PREFECT_API_SERVICES_SCHEDULER_MIN_RUNS='3'
PREFECT_API_SERVICES_SCHEDULER_MAX_RUNS='100'
PREFECT_API_SERVICES_SCHEDULER_MIN_SCHEDULED_TIME='1:00:00'
PREFECT_API_SERVICES_SCHEDULER_MAX_SCHEDULED_TIME='100 days, 0:00:00'

See the Settings docs for more information on altering your settings.

These settings mean that if a deployment has an hourly schedule, the default settings will create runs for the next 4 days (or 100 hours). If it has a weekly schedule, the default settings will maintain the next 14 runs (up to 100 days in the future).

The Scheduler does not affect execution

The Prefect Scheduler service only creates new flow runs and places them in Scheduled states. It is not involved in flow or task execution.

If you change a schedule, previously scheduled flow runs that have not started are removed, and new scheduled flow runs are created to reflect the new schedule.

To remove all scheduled runs for a flow deployment, you can remove the schedule via the UI.