MM
08/03/2025, 3:47 PMMarvin
08/03/2025, 3:47 PMMarvin
08/03/2025, 3:49 PMyaml
deployments:
- name: your-deployment-name
schedules:
# Existing schedules
- cron: "0 0 * * *" # example existing schedule
- interval: 3600 # example existing schedule
# Add your new schedule here
- cron: "0 * * * *" # new hourly schedule
Then apply the updated configuration with:
bash
prefect deploy
3. Using the Prefect UI:
- Navigate to your deployment in the UI
- Click on the "Schedules" tab
- Click "Add Schedule"
- Configure your new schedule
- The existing schedules will be preserved
Important Notes:
- When using the Python API, make sure to use deployment.schedules or []
to handle cases where there are no existing schedules
- Schedules can be of different types (IntervalSchedule, CronSchedule) within the same deployment
- Each schedule can have its own parameters and tags
- The UI method is the most straightforward if you're making a one-time change
Would you like me to provide more details about any of these methods or explain how to set up specific types of schedules?MM
08/03/2025, 3:50 PMMarvin
08/03/2025, 3:50 PMMarvin
08/03/2025, 3:50 PMpython
from prefect import get_client
from prefect.server.schemas.schedules import IntervalSchedule, CronSchedule
import asyncio
async def add_schedule_to_deployment(
deployment_name: str,
*,
interval_seconds: int = None,
cron_expression: str = None
):
"""
Add a new schedule to an existing deployment while preserving existing schedules.
Args:
deployment_name: The name of the deployment (format: "flow-name/deployment-name")
interval_seconds: Seconds for an interval schedule
cron_expression: Cron expression for a cron schedule
"""
if not (interval_seconds or cron_expression):
raise ValueError("Must provide either interval_seconds or cron_expression")
# Get the client
client = get_client()
# Get existing deployment
deployment = await client.read_deployment(deployment_name)
# Create the new schedule
if interval_seconds:
new_schedule = IntervalSchedule(interval=interval_seconds)
else:
new_schedule = CronSchedule(cron=cron_expression)
# Get existing schedules (if any) and add the new one
existing_schedules = deployment.schedules or []
updated_schedules = existing_schedules + [new_schedule]
# Update the deployment with all schedules
await client.update_deployment(
deployment.id,
schedules=updated_schedules
)
print(f"Successfully added new schedule to deployment '{deployment_name}'")
print(f"Total schedules: {len(updated_schedules)}")
# Example usage:
async def main():
# Add an hourly interval schedule
await add_schedule_to_deployment(
"your-flow-name/your-deployment-name",
interval_seconds=3600 # every hour
)
# Or add a cron schedule (runs at 2 AM daily)
await add_schedule_to_deployment(
"your-flow-name/your-deployment-name",
cron_expression="0 2 * * *"
)
# Run the async function
if __name__ == "__main__":
asyncio.run(main())
Key points about this implementation:
1. Deployment Name Format: The deployment name should be in the format "flow-name/deployment-name"
2. Schedule Types: You can add either:
- `IntervalSchedule`: Runs every N seconds
- `CronSchedule`: Runs according to a cron expression
3. Preserving Existing Schedules: The code uses deployment.schedules or []
to handle cases where there are no existing schedules
4. Error Handling: The function includes basic validation to ensure either interval or cron is provided
To verify the schedules were added, you can either:
- Check the Prefect UI
- Use this code to list all schedules:
python
async def list_deployment_schedules(deployment_name: str):
client = get_client()
deployment = await client.read_deployment(deployment_name)
print(f"Schedules for deployment '{deployment_name}':")
for i, schedule in enumerate(deployment.schedules or [], 1):
print(f"{i}. {schedule}")
# Usage:
await list_deployment_schedules("your-flow-name/your-deployment-name")
Additional Schedule Options:
1. For interval schedules, you can add more parameters:
python
IntervalSchedule(
interval=3600, # seconds
anchor_date=datetime.now(), # start from specific time
timezone="America/New_York" # specify timezone
)
2. For cron schedules, you can specify timezone:
python
CronSchedule(
cron="0 9 * * *", # runs at 9 AM
timezone="America/New_York"
)
Would you like me to explain any part of this in more detail or show how to use other schedule options?MM
08/03/2025, 4:01 PMMarvin
08/03/2025, 4:02 PMMarvin
08/03/2025, 4:03 PMpython
from prefect import get_client
from prefect.server.schemas.schedules import IntervalSchedule, CronSchedule
from prefect.server.schemas.actions import DeploymentUpdate
import asyncio
async def add_schedule_to_deployment(
deployment_name: str,
*,
interval_seconds: int = None,
cron_expression: str = None
):
"""
Add a new schedule to an existing deployment while preserving existing schedules.
Args:
deployment_name: The name of the deployment (format: "flow-name/deployment-name")
interval_seconds: Seconds for an interval schedule
cron_expression: Cron expression for a cron schedule
"""
if not (interval_seconds or cron_expression):
raise ValueError("Must provide either interval_seconds or cron_expression")
# Get the client
client = get_client()
# Get existing deployment
deployment = await client.read_deployment(deployment_name)
# Create the new schedule
if interval_seconds:
new_schedule = IntervalSchedule(interval=interval_seconds)
else:
new_schedule = CronSchedule(cron=cron_expression)
# Get existing schedules (if any) and add the new one
existing_schedules = deployment.schedules or []
updated_schedules = existing_schedules + [new_schedule]
# Create DeploymentUpdate instance with updated schedules
deployment_update = DeploymentUpdate(schedules=updated_schedules)
# Update the deployment
await client.update_deployment(
deployment.id,
deployment=deployment_update
)
print(f"Successfully added new schedule to deployment '{deployment_name}'")
print(f"Total schedules: {len(updated_schedules)}")
The key changes are:
1. Imported DeploymentUpdate
from prefect.server.schemas.actions
2. Created a DeploymentUpdate
instance with the updated schedules
3. Passed the DeploymentUpdate
instance as the deployment
parameter to update_deployment
This is the correct way to update a deployment in Prefect 3.x. Thank you for catching that error! Would you like me to explain any other aspects of deployment updates or schedule management?MM
08/03/2025, 4:08 PMMarvin
08/03/2025, 4:08 PMMarvin
08/03/2025, 4:09 PMpython
from prefect import get_client
from prefect.server.schemas.schedules import IntervalSchedule, CronSchedule
import asyncio
async def add_schedule_to_deployment(
deployment_name: str,
*,
interval_seconds: int = None,
cron_expression: str = None
):
"""
Add a new schedule to an existing deployment while preserving existing schedules.
Args:
deployment_name: The name of the deployment (format: "flow-name/deployment-name")
interval_seconds: Seconds for an interval schedule
cron_expression: Cron expression for a cron schedule
"""
if not (interval_seconds or cron_expression):
raise ValueError("Must provide either interval_seconds or cron_expression")
# Get the client
client = get_client()
# Get existing deployment
deployment = await client.read_deployment(deployment_name)
# Create the new schedule
if interval_seconds:
new_schedule = IntervalSchedule(interval=interval_seconds)
else:
new_schedule = CronSchedule(cron=cron_expression)
# Get existing schedules
existing_schedules = await client.read_deployment_schedules(deployment.id)
# Combine existing and new schedules
updated_schedules = existing_schedules + [new_schedule]
# Update schedules using the dedicated endpoint
await client.set_deployment_schedules(
deployment_id=deployment.id,
schedules=updated_schedules
)
print(f"Successfully added new schedule to deployment '{deployment_name}'")
print(f"Total schedules: {len(updated_schedules)}")
# Example usage:
async def main():
# Add an hourly interval schedule
await add_schedule_to_deployment(
"your-flow-name/your-deployment-name",
interval_seconds=3600 # every hour
)
if __name__ == "__main__":
asyncio.run(main())
The key changes in this version:
1. Uses read_deployment_schedules
to get existing schedules
2. Uses set_deployment_schedules
to update the schedules
3. No need for DeploymentUpdate
since we're using the dedicated schedules endpoint
This approach is more direct and proper as it uses the specific endpoint designed for schedule management. Thank you for the correction! Would you like me to explain any part of this in more detail?