Hi all! I need to retrieve the upcoming runs for a...
# ask-community
k
Hi all! I need to retrieve the upcoming runs for a specific work_pool and a specific work_queue. First of all I tried the code below:
Copy code
from prefect.client.orchestration import get_client
from datetime import datetime, timezone, timedelta
import asyncio

async def list_upcoming_runs_for_month(work_pool_name, work_queue_names=None):
    scheduled_before = datetime.now(timezone.utc) + timedelta(days=1)

    async with get_client() as client:
        runs = await client.get_scheduled_flow_runs_for_work_pool(
            work_pool_name=work_pool_name,
            work_queue_names=work_queue_names,
            scheduled_before=scheduled_before
        )

    if not runs:
        print("No upcoming runs.")
    else:
        print(f"Upcoming runs:")
        for item in runs:
            fr = item["flow_run"]
            print(f"- {fr['name']} | deployment_id={fr['deployment_id']} | queue={item['work_queue_name']} | start={fr['expected_start_time']}")

async def main():
    work_pool = "my_work_pool"
    queue_names = ["default"]
    await list_upcoming_runs_for_month(work_pool, queue_names)

if __name__ == "__main__":
    asyncio.run(main())
before i run the code i export env vars:
Copy code
export PREFECT_API_KEY=<PREFECT_API_KEY>
export PREFECT_API_URL=<https://api.prefect.cloud/api/accounts/><ACCOUNT_ID>/workspaces/<WORKSPACE_ID>
And even if in UI I can see upcoming runs I get 'No upcoming runs.' I also tried to use API with logic:
Copy code
import requests

from datetime import datetime, timezone

# === [🔐 CONFIG ] ===
PREFECT_API_TOKEN = ""
PREFECT_ACCOUNT_ID = ""
PREFECT_WORKSPACE_ID = ""
WORK_POOL_NAME = ""
# =====================

API_URL = f"<https://api.prefect.cloud/api/accounts/{PREFECT_ACCOUNT_ID}/workspaces/{PREFECT_WORKSPACE_ID}>"
HEADERS = {"Authorization": f"Bearer {PREFECT_API_TOKEN}", "Content-Type": "application/json"}

queue_resp = <http://requests.post|requests.post>(
    f"{API_URL}/work_pools/{WORK_POOL_NAME}/queues/filter",
    headers=HEADERS,
    json={"name": {"any_": ["default"]}}
)
queue_resp.raise_for_status()
queues = queue_resp.json()
if not queues:
    raise ValueError("Queue not found in given work pool")
queue_id = queues[0]["id"]

runs_resp = <http://requests.post|requests.post>(
    f"{API_URL}/flow_runs/filter",
    headers=HEADERS,
    json={
        "work_queue_id": {"any_": [queue_id]},
        "state_type": {"any_": ["SCHEDULED"]},
        "expected_start_time": {"after_": datetime.now(timezone.utc).isoformat()},
        "deployment_id": {"not_null_": True}
    }
)
runs_resp.raise_for_status()
runs = runs_resp.json()

deployment_ids = list({r["deployment_id"] for r in runs})

if deployment_ids:
    deployments_resp = <http://requests.post|requests.post>(
        f"{API_URL}/deployments/filter",
        headers=HEADERS,
        json={"id": {"any_": deployment_ids}}
    )
    deployments_resp.raise_for_status()
    deployments = deployments_resp.json()
    print("Upcoming deployments:")
    for d in deployments:
        print(f" - {d['name']} (id={d['id']})")
else:
    print("No upcoming deployments in given work pool queue.")
However, this way I receive all deployments from all pools, even when the schedule is disabled. Does anyone know what the problem is with extracting this data correctly? Thank you in advance for any help!
I have also used below endpoint:
Copy code
<https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_id}/work_queues/{id}/get_runs>
However this returns me no flow runs at all (I have checked for all my work-pools/queues)