<@ULVA73B9P> is there a way to get a view of all s...
# ask-marvin
s
@Marvin is there a way to get a view of all schedules that run under a given workspace?
m
You can view all schedules that run under a given workspace using the Prefect UI. You would typically associate schedules with deployments, so navigating through the deployments within the workspace should give an overview of all schedules. For more details, you can refer to the documentation links: - View related to workspaces: Prefect Workspaces - View related to scheduling concepts: Prefect Schedules
s
@Marvin can this be done programmatically via the API where I can list all deployments and their related schedules (if any)?
m
The Prefect API allows you to list deployments and view their associated schedules. Here’s a brief overview of how you can achieve this: 1. List Deployments: Use the following command to list all deployments:
Copy code
prefect 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:
Copy code
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.
s
@Marvin prefect deployment ls lists all deployments in a workspace, however, I need to supply the deployment name to get the schedules, can I use the prefect cli and do this in a bash shell script?
m
Certainly, you can use the Prefect CLI in a bash shell script to list all deployments and then retrieve the schedules for each deployment. Here's how you can do it:
Copy code
bash
#!/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.
n
i would use the python client for this instead of the CLI if you need to filter outputs
you can use the read_deployments method (with a filter if needed)
s
Thanks @Nate. I was looking at the SDK and was trying to find a method where I can get all the deployments for a given workspace.
👍 1
n
yep, read_deployments should be what you’re looking for
s
Will
get_scheduled_flow_runs_for_deployments
be useful to get the schedules for each deployment?
I can see
read_deployment_schedules
in the SDK
n
schedules live on the deployment object, so you can grab them like this
Copy code
In [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)]
👍 1
thank you 1
s
@Nate, I was hoping to filter on only active schedules but this seems to be bringing back all schedules, any idea what I have done wrong?
Copy code
async with get_client() as client:
    for deployment in await client.read_deployments(deployment_filter=DeploymentFilter(name=DeploymentFilterIsScheduleActive(eq_=True))):
        print(deployment.schedules)
This includes deployments with no schedules
[]
as well
n
i'm not sure that filter exists, but you could do the wrangling in python
Copy code
In [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 filter
thank you 1
s
It filters on is_schedule_active, however, this can be
True
and the
schedule=None
at the Deployment level.
n
ah i see, sorry. that must have been removed for some reason in 3.x, since I don't see it there
aha. linking here in case it becomes relevant https://github.com/PrefectHQ/prefect/pull/14794
👍 1
m
Here are some key benefits of using workers over agents in Prefect: 1. Fusion of Agent and Infrastructure Block: Workers combine the capabilities of agents and infrastructure blocks, providing a unified mechanism for managing execution infrastructure and starting flow runs. 2. Simplified Deployment: Deploying flows with workers simplifies the process by allowing infrastructure configurations to be specified in the work pool rather than individually on each flow deployment. 3. Enhanced Visibility: Workers provide improved visibility into their status, including when they were started and last polled, making it easier to monitor and manage work pools. 4. Typed Workers: Workers are specialized to work with specific types of infrastructure, ensuring better default configuration and more efficient execution environments. 5. Future-Proof: Prefect has shifted its focus to workers, making them the central mechanism for flow execution, which means they will receive more features and improvements moving forward. Reference: Upgrade Guide from Agents to Workers