Spida45
10/28/2024, 2:49 PMMarvin
10/28/2024, 2:50 PMschedules
list within the prefect.yaml
file, where each schedule is defined using RRule syntax.
Here's an example where you want to deploy a flow with multiple schedules:
1. Schedule on even days at 18:00, odd days at 9:30.
2. A one-time schedule tomorrow at 22:15.
3. And daily at 6:00.
### Configuration in `prefect.yaml`:
yaml
schedules:
- rrule: 'FREQ=WEEKLY;BYDAY=TU,TH,SA;BYHOUR=18;BYMINUTE=0'
- rrule: 'FREQ=WEEKLY;BYDAY=MO,WE,FR,SU;BYHOUR=9;BYMINUTE=30'
- rrule: 'FREQ=DAILY;UNTIL=20231102T235900;BYHOUR=22;BYMINUTE=15'
- rrule: 'FREQ=DAILY;BYHOUR=6;BYMINUTE=0'
### Using CLI:
You can also specify these schedules directly through the Prefect CLI:
bash
prefect deploy ... --rrule 'FREQ=WEEKLY;BYDAY=TU,TH,SA;BYHOUR=18;BYMINUTE=0' \
--rrule 'FREQ=WEEKLY;BYDAY=MO,WE,FR,SU;BYHOUR=9;BYMINUTE=30' \
--rrule 'FREQ=DAILY;UNTIL=20231102T235900;BYHOUR=22;BYMINUTE=15' \
--rrule 'FREQ=DAILY;BYHOUR=6;BYMINUTE=0'
For further details, check the Prefect documentation on schedules: Prefect 2.x Schedules. This feature was added to address the need for multiple schedules under a single deployment, as seen in issues and pull requests for Prefect 2.x PrefectHQ GitHub.Spida45
10/28/2024, 2:50 PMMarvin
10/28/2024, 2:51 PMDeployment
class or by using the flow.deploy()
method. Here's a simplified example of how you might accomplish this:
python
from prefect import Flow
from prefect.deployments import Deployment
from prefect.schedules import RRuleSchedule
from dateutil.rrule import rrule, WEEKLY, DAILY
from datetime import datetime, timedelta
# Define your flow
with Flow("example-flow") as flow:
# Add your tasks here
pass
# Define multiple RRule schedules
schedule_1 = RRuleSchedule(rrule(WEEKLY, byweekday=(0, 2, 4), dtstart=datetime.now())) # Monday, Wednesday, Friday
schedule_2 = RRuleSchedule(rrule(DAILY, byhour=9, byminute=30, dtstart=datetime.now())) # Every day at 09:30
# Create a deployment with the schedules
deployment = Deployment.build_from_flow(
flow=flow,
name="example-deployment",
schedules=[schedule_1, schedule_2]
)
# Apply the deployment
deployment.apply()
In this example:
- RRuleSchedule
objects are created using dateutil.rrule
.
- rrule(WEEKLY, byweekday=(0, 2, 4))
specifies running on Monday, Wednesday, and Friday.
- rrule(DAILY, byhour=9, byminute=30)
specifies running daily at 9:30 AM.
- These schedules are passed to the Deployment.build_from_flow
method, allowing you to attach multiple schedules to a single deployment.
You can find more information on creating schedules in the Prefect 2.x documentation under Schedules.Spida45
10/28/2024, 2:54 PMMarvin
10/28/2024, 2:54 PMdateutil.rrule
module.
Below is an example of how you can set it up in a Python script using Prefect:
python
from prefect.deployments import Deployment
from prefect.schedules import RRuleSchedule
from dateutil.rrule import rrule, HOURLY, SECONDLY, DAILY
from datetime import datetime, timedelta, time
# A function to create rules for different time segments
def create_schedule():
# Rule for every 30 seconds from 2 PM to 2 AM
rule_30_seconds = rrule(
freq=SECONDLY,
interval=30,
byhour=list(range(14, 24)) + list(range(0, 2)), # 14:00 to 23:59 and 00:00 to 01:59
dtstart=datetime.now()
)
# Rule for every hour from 2 AM to 2 PM
rule_hourly = rrule(
freq=HOURLY,
byhour=list(range(2, 14)), # 02:00 AM to 01:59 PM
dtstart=datetime.now()
)
return [RRuleSchedule(rule_30_seconds), RRuleSchedule(rule_hourly)]
# Example to attach schedules to a deployment
def create_deployment(flow):
deployment = Deployment.build_from_flow(
flow=flow,
name="example-deployment",
schedules=create_schedule()
)
deployment.apply()
# Example flow (assuming you have a flow defined elsewhere)
from prefect import Flow
with Flow("example-flow") as flow:
# define your tasks here
pass
create_deployment(flow)
Explanation:
- This script creates two different scheduling rules using RRules:
- 30-Second Interval: Runs every 30 seconds from 14:00 (2 PM) to 01:59 (2 AM).
- Hourly Interval: Runs every hour from 02:00 (2 AM) to 13:59 (1:59 PM).
These schedules are added to a deployment so that the flow will run according to the defined schedule. Be sure to adjust the dtstart
parameter to fit the start time for your specific requirements and time zone considerations.