<@ULVA73B9P> how do I specify a schedule trigger i...
# ask-marvin
y
@Marvin how do I specify a schedule trigger in my flow? Can it be done in the Flow, or deployment?
m
Ah, the sands of time have revealed the answer. Schedules in Prefect tell the API how to create new flow runs for you automatically on a specified cadence. You can add a schedule to any flow deployment. The Prefect Scheduler service periodically reviews every deployment and creates new flow runs according to the schedule configured for the deployment. There are four recommended ways to create a schedule for a deployment: 1. Use the Prefect UI 2. Use the cron, interval, or rrule flags with the CLI deployment build command 3. Use the schedule parameter with a Python deployment file 4. Manually edit the deployment YAML file's schedule section Here's an example of how to create a schedule in a Python deployment file, with a timezone specified:
Copy code
python
from prefect.server.schemas.schedules import CronSchedule

cron_demo = Deployment.build_from_flow(
    pipeline, 
    "etl", 
    schedule=(CronSchedule(cron="0 0 * * *", timezone="America/Chicago"))
)
In this example,
CronSchedule(cron="0 0 * * *", timezone="America/Chicago")
sets the schedule to create flow runs every day at midnight in the America/Chicago timezone. 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, update the deployment YAML with no schedule value, or remove the schedule via the UI. For more details, you can refer to the Prefect documentation on schedules. Remember, time and tide wait for no man, but at least Prefect waits for your schedule.
y
@Marvin, what if I am using
prefect deploy myflow.py:myentrypoint
m
Ah, the documents have spoken. When you deploy a flow from the CLI, you can indeed use the
--schedule
flag to set a schedule. The command would look something like this:
Copy code
bash
prefect deploy myflow.py:myentrypoint --schedule "0 0 * * *"
This command will create flow runs for your deployment every day at midnight. The
--schedule
flag accepts cron syntax for specifying the schedule. Remember, 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 update the deployment with no schedule value. For more details, you can refer to the Prefect documentation on schedules. Ah, the beauty of automation. It's like a well-oiled machine, unlike us humans who need sleep and coffee.
n
you can also define an event trigger like this, if thats helpful
y
@Nate nice thanks! is there a way to add schedules to the Flow itself similar to Prefect 1?
n
nope! by design in prefect 2, the deployment and the flow are separate (no deployment config on the flow object itself) schedules are explicitly a thing that you specify on the Deployment of a given flow
1