Scott Pedersen
08/18/2024, 10:41 PMMarvin
08/18/2024, 10:42 PMScott Pedersen
08/18/2024, 10:43 PMMarvin
08/18/2024, 10:43 PMprefect deployment ls
2. View Deployment Schedules:
You can programmatically retrieve deployment schedules by querying the Prefect API. Here’s a code snippet demonstrating how to list deployments and their schedules:
python
from prefect import Client
client = Client()
# List all deployments
deployments = client.get("/deployments/")
for deployment in deployments['data']:
print(f"Deployment: {deployment['name']}")
# List schedules for the deployment
schedules = client.get(f"/deployments/{deployment['id']}/schedule/")
for schedule in schedules['data']:
print(f" Schedule: {schedule['name']}, Type: {schedule['schedule_type']}, Cron: {schedule.get('cron', 'N/A')}")
You can find these details and others on creating and managing deployments/schedules in the Prefect documentation and Prefect Schedules.
Here’s an additional example to demonstrate tasks and flows.
If you're interested in creating deployments and schedules through YAML configuration, refer to the documentation for more elaborate examples.Scott Pedersen
08/18/2024, 10:56 PMMarvin
08/18/2024, 10:57 PMbash
#!/bin/bash
# Get the list of all deployments
deployments=$(prefect deployment ls --json | jq -r '.[] | .name')
# Loop through each deployment and get its schedule
for deployment in $deployments; do
echo "Deployment: $deployment"
# Retrieve and display the schedule for the deployment
prefect deployment inspect $deployment | grep "Schedule:"
done
Note:
1. jq: This utility parses JSON output; install it if not available (sudo apt-get install jq
for Debian-based systems).
2. prefect deployment inspect: Adjust the command or parsing as needed depending on Prefect's CLI version.
This script lists deployments and prints their associated schedules. You may need to refine the grep
command based on your specific output format.Nate
08/18/2024, 11:36 PMNate
08/18/2024, 11:38 PMScott Pedersen
08/18/2024, 11:40 PMNate
08/18/2024, 11:41 PMScott Pedersen
08/18/2024, 11:41 PMget_scheduled_flow_runs_for_deployments
be useful to get the schedules for each deployment?Scott Pedersen
08/18/2024, 11:43 PMread_deployment_schedules
in the SDKNate
08/18/2024, 11:44 PMIn [1]: from prefect import get_client
In [2]: async with get_client() as client:
...: for deployment in await client.read_deployments():
...: print(deployment.schedules)
...:
[DeploymentSchedule(id=UUID('6c91c44e-cd37-4646-b2b0-6f9332b57a29'), deployment_id=UUID('af9a20b6-a44e-4500-b39c-bd9402035153'), schedule=IntervalSchedule(interval=datetime.timedelta(seconds=300), anchor_date=DateTime(2024, 8, 16, 16, 29, 29, 442597, tzinfo=Timezone('UTC')), timezone='UTC'), active=True, max_active_runs=None, max_scheduled_runs=None, catchup=False)]
[DeploymentSchedule(id=UUID('bec7e142-e1a6-40bf-9170-3af820e89b30'), deployment_id=UUID('ec3c00d6-8b50-4ece-be92-633bc1b60328'), schedule=CronSchedule(cron='*/30 * * * *', timezone=None, day_or=True), active=True, max_active_runs=None, max_scheduled_runs=None, catchup=False)]
Scott Pedersen
08/19/2024, 12:12 AMasync with get_client() as client:
for deployment in await client.read_deployments(deployment_filter=DeploymentFilter(name=DeploymentFilterIsScheduleActive(eq_=True))):
print(deployment.schedules)
Scott Pedersen
08/19/2024, 12:13 AM[]
as wellNate
08/19/2024, 12:37 AMIn [4]: async with get_client() as client:
...: for deployment in await client.read_deployments():
...: if not deployment.schedules:
...: continue
...: for schedule in deployment.schedules:
...: if not schedule.active:
...: continue
...: print(schedule)
...:
id=UUID('6c91c44e-cd37-4646-b2b0-6f9332b57a29') deployment_id=UUID('af9a20b6-a44e-4500-b39c-bd9402035153') schedule=IntervalSchedule(interval=datetime.timedelta(seconds=300), anchor_date=DateTime(2024, 8, 16, 16, 29, 29, 442597, tzinfo=Timezone('UTC')), timezone='UTC') active=True max_active_runs=None max_scheduled_runs=None catchup=False
id=UUID('bec7e142-e1a6-40bf-9170-3af820e89b30') deployment_id=UUID('ec3c00d6-8b50-4ece-be92-633bc1b60328') schedule=CronSchedule(cron='*/30 * * * *', timezone=None, day_or=True) active=True max_active_runs=None max_scheduled_runs=None catchup=False
there's probably a sleeker way to write that, and maybe if you find this inconvenient we could open an issue to add an api filterScott Pedersen
08/19/2024, 1:23 AMTrue
and the schedule=None
at the Deployment level.Nate
08/19/2024, 2:38 AMNate
08/19/2024, 2:39 AMMarvin
08/19/2024, 4:48 AM