<@ULVA73B9P> can I use automations to pause a sche...
# ask-marvin
a
@Marvin can I use automations to pause a schedule?
m
thought for 40.7 seconds
Yes, you can use automations to programmatically pause deployment schedules in Prefect. Here's how it works: 1. Create an automation that uses the
PauseDeployment
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:
Copy code
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-triggers
a
@Marvin how can I unpause a deployment via the api?
m
thought for 33.0 seconds
You can unpause a deployment using the Prefect API in two ways: 1. Using the Python client:
Copy code
python
from prefect.client import get_client

async with get_client() as client:
    await client.resume_deployment("deployment-id")
For synchronous code:
Copy 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:
Copy code
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-deployment
a
@Marvin how can I update a deployment’s schedule via the api to make it execute tomorrow at 11pm?
m
thought for 53.8 seconds
To update a deployment's schedule to run tomorrow at 11 PM, you can use either the Python client or the REST API. Here's how to do it both ways: 1. Using the Python client:
Copy code
python
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:
Copy code
PATCH /api/server/deployments/{deployment_id}/schedules/{schedule_id}
With the request body:
Copy code
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