Aaron Gonzalez
07/02/2025, 11:23 AMMarvin
07/02/2025, 11:23 AMMarvin
07/02/2025, 11:24 AMPauseDeployment
action
2. Define a trigger that specifies when the automation should execute
3. When the trigger conditions are met, the automation will automatically pause the deployment's schedule
Here's a practical example that pauses a deployment when a flow run fails:
python
from prefect.server.schemas.actions import PauseDeployment
from prefect.server.schemas.triggers import EventTrigger
from prefect.server.schemas.automations import Automation
automation = Automation(
name="Pause on Failure",
trigger=EventTrigger(
event="prefect.flow-run.Failed",
resource_id="your-deployment-id" # Optional: specify a particular deployment
),
actions=[PauseDeployment(deployment_id="your-deployment-id")]
)
automation.save()
You can also create automations through the Prefect UI, which provides a user-friendly interface for setting up triggers and actions.
Some key points about using automations with schedules:
- The pause action affects the entire deployment schedule, not individual runs
- You can create complex trigger conditions based on various events
- The automation can be re-enabled later when needed
- Multiple automations can be created to handle different scenarios
For more details, you can check out:
- The deployment pause API documentation: https://docs-3.prefect.io/v3/api-ref/rest-api/server/deployments/pause-deployment
- The automation triggers guide: https://docs-3.prefect.io/v3/how-to-guides/automations/creating-deployment-triggersAaron Gonzalez
07/02/2025, 11:27 AMMarvin
07/02/2025, 11:27 AMMarvin
07/02/2025, 11:28 AMpython
from prefect.client import get_client
async with get_client() as client:
await client.resume_deployment("deployment-id")
For synchronous code:
python
from prefect.client import get_client
with get_client() as client:
client.resume_deployment("deployment-id")
2. Using the REST API directly:
Make a POST request to:
POST /api/server/deployments/{deployment_id}/resume-deployment
The deployment will be unpaused and its schedule will become active again, allowing scheduled runs to resume.
Note that you'll need appropriate authentication and permissions to perform this action. If you're using the REST API directly, make sure to include your API key in the request headers if required.
The endpoint documentation is available at: https://docs-3.prefect.io/v3/api-ref/rest-api/server/deployments/resume-deploymentAaron Gonzalez
07/02/2025, 11:29 AMMarvin
07/02/2025, 11:29 AMMarvin
07/02/2025, 11:30 AMpython
from prefect.client import get_client
from prefect.client.schemas.schedules import CronSchedule
from datetime import datetime, timedelta
# Calculate tomorrow's date
tomorrow = datetime.now() + timedelta(days=1)
tomorrow_date = tomorrow.strftime("%Y-%m-%d")
# Create a cron schedule for 11 PM tomorrow
schedule = CronSchedule(
cron=f"0 23 {tomorrow_date.split('-')[2]} {tomorrow_date.split('-')[1]} *",
timezone="UTC" # Adjust timezone as needed
)
async with get_client() as client:
# First get the current schedule ID
deployment = await client.read_deployment("your-deployment-id")
schedule_id = deployment.schedule.id if deployment.schedule else None
# Update the schedule
await client.update_deployment_schedule(
deployment_id="your-deployment-id",
schedule_id=schedule_id,
schedule=schedule
)
2. Using the REST API directly:
PATCH /api/server/deployments/{deployment_id}/schedules/{schedule_id}
With the request body:
json
{
"schedule": {
"cron": "0 23 DD MM *", // Replace DD and MM with tomorrow's day and month
"timezone": "UTC"
}
}
A few important notes:
- Make sure to adjust the timezone in the schedule to match your desired timezone
- The schedule will only execute once at the specified time
- You'll need the deployment ID and existing schedule ID (if any) to update the schedule
- If you want the deployment to continue running on a different schedule after tomorrow's execution, you'll need to update it again with the desired recurring schedule
The endpoint documentation is available at: https://docs-3.prefect.io/v3/api-ref/rest-api/server/deployments/update-deployment-schedule