Something went wrong. Try again.
prefect server in zig
Something went wrong. Try again.
5.5 kB · 171 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172#!/usr/bin/env -S uv run --script --quiet# /// script# requires-python = ">=3.12"# dependencies = ["prefect>=3.0", "httpx"]# ///"""Smoke test the docket-driven loop services end-to-end via the realPython prefect client against a running zig prefect-server.
What it exercises: - POST /api/flows/ — flow creation - POST /api/deployments/ — deployment with interval schedule - scheduler.evaluate perpetual (docket task) — creates SCHEDULED flow_runs - late_runs.mark perpetual (docket task) — marks them Late after threshold
Run: just dev # in another terminal (memory broker is default) ./scripts/test-docket-loop
Or against redis-backed prefect-server: PREFECT_BROKER_BACKEND=redis just dev ./scripts/test-docket-loop"""
import asyncioimport osimport sysimport timefrom datetime import datetime, timedelta, timezone
import httpx
def auth() -> httpx.BasicAuth | None: """Auth for the raw httpx calls below.
The prefect client reads PREFECT_API_AUTH_STRING itself; direct httpx calls do not, so an authenticated server 401s every request and the failure surfaces as whatever this script was measuring. """ raw = os.environ.get("PREFECT_API_AUTH_STRING") if not raw: return None user, sep, password = raw.partition(":") if not sep: raise RuntimeError("PREFECT_API_AUTH_STRING must be user:password") return httpx.BasicAuth(user, password)
BASE = os.environ.get("PREFECT_API_URL", "http://localhost:4200/api")
async def main() -> int: async with httpx.AsyncClient(base_url=BASE, timeout=10.0, auth=auth()) as client: await wait_ready(client)
flow_id = await create_flow(client, "docket-smoke-flow") print(f"created flow {flow_id}")
deployment_id = await create_interval_deployment( client, flow_id=flow_id, name="docket-smoke-deployment", interval_seconds=2, ) print(f"created deployment {deployment_id}")
# The scheduler perpetual fires every 5s by default; with an # interval of 2s per schedule it should backfill a handful of # SCHEDULED runs on its first tick. print("waiting 8s for scheduler.evaluate to create SCHEDULED runs...") await asyncio.sleep(8)
scheduled = await count_runs_by_state(client, deployment_id, "SCHEDULED") print(f"scheduled flow runs: {scheduled}") if scheduled == 0: print("FAIL: scheduler.evaluate didn't create any runs") return 1
# late_runs.mark perpetual fires every 5s; threshold defaults to 15s # past scheduled time. The runs we just created are scheduled at # 2s / 4s / 6s / ... into the future, so wait until enough have # passed their scheduled_time + 15s. print( "waiting 25s for late_runs.mark to mark some past-due runs as Late..." ) await asyncio.sleep(25)
late = await count_runs_by_state(client, deployment_id, "SCHEDULED", state_name="Late") all_scheduled = await count_runs_by_state(client, deployment_id, "SCHEDULED") print(f"flow runs Late: {late} / {all_scheduled} SCHEDULED") if late == 0: print("FAIL: late_runs.mark didn't mark any runs Late") return 1
print(f"OK — docket loop services drove {late} runs to Late state end-to-end") return 0
async def wait_ready(client: httpx.AsyncClient, deadline_s: float = 10.0) -> None: """Poll /api/health until the server is up.""" started = time.monotonic() while time.monotonic() - started < deadline_s: try: r = await client.get("/health") if r.status_code == 200: return except httpx.HTTPError: pass await asyncio.sleep(0.5) raise RuntimeError(f"prefect-server didn't become ready in {deadline_s}s at {BASE}")
async def create_flow(client: httpx.AsyncClient, name: str) -> str: r = await client.post("/flows/", json={"name": name}) r.raise_for_status() return r.json()["id"]
async def create_interval_deployment( client: httpx.AsyncClient, flow_id: str, name: str, interval_seconds: int,) -> str: anchor = datetime.now(timezone.utc).isoformat() body = { "name": name, "flow_id": flow_id, "schedules": [ { "schedule": { "interval": interval_seconds, "anchor_date": anchor, "timezone": "UTC", }, "active": True, } ], "parameters": {}, } r = await client.post("/deployments/", json=body) r.raise_for_status() return r.json()["id"]
async def count_runs_by_state( client: httpx.AsyncClient, deployment_id: str, state_type: str, state_name: str | None = None,) -> int: r = await client.post( "/flow_runs/filter", json={ "deployments": {"id": {"any_": [deployment_id]}}, "flow_runs": { "state": { "type": {"any_": [state_type]}, **({"name": {"any_": [state_name]}} if state_name else {}), }, }, "limit": 200, }, ) r.raise_for_status() return len(r.json())
if __name__ == "__main__": sys.exit(asyncio.run(main()))