#!/usr/bin/env -S uv run --script --quiet # /// script # requires-python = ">=3.12" # dependencies = ["prefect>=3.0", "httpx"] # /// """ Real-workload smoke. NOT just "the API accepts requests" — actually: 1. Create a work pool of type `process` 2. Define a flow function in this script 3. Deploy it (associates with the work pool, no schedule) 4. Trigger one ad-hoc flow run (POST /api/deployments/{id}/create_flow_run) 5. Start a `prefect worker --pool ` subprocess that polls the server, picks up the run, and EXECUTES the flow body in a subprocess on this machine 6. Wait for the run to reach Completed 7. Assert the run's terminal state is COMPLETED + start_time/end_time are set (proves the orchestration / global-policy state-bookkeeping rules fired) This exercises the actual worker contract: - POST /work_pools/ — create pool - POST /work_pools/{name}/workers/heartbeat — worker keep-alive - POST /work_pools/{name}/get_scheduled_flow_runs — work polling - POST /flow_runs/{id}/set_state — orchestration roundtrip (server returns ACCEPT/etc.) Usage: docker compose up -d postgres redis webapp services PREFECT_API_URL=http://localhost:4200/api ./scripts/test-worker-execution """ import asyncio import os import subprocess import sys import tempfile import time from pathlib import Path from uuid import uuid4 # scripts/ holds the flow code; we set the worker's CWD here so the # deployment's entrypoint resolves as a sibling file rather than a # child-of-CWD path. SCRIPTS_DIR = Path(__file__).resolve().parent # Force prefect to talk to our zig server. API_URL = os.environ.get("PREFECT_API_URL", "http://localhost:4200/api") os.environ["PREFECT_API_URL"] = API_URL # Lower poll interval so the smoke completes fast. os.environ.setdefault("PREFECT_WORKER_QUERY_SECONDS", "2") sys.path.insert(0, str(SCRIPTS_DIR)) from prefect import Flow # noqa: E402 from prefect.client.orchestration import get_client # noqa: E402 from prefect.client.schemas.actions import WorkPoolCreate # noqa: E402 from prefect.exceptions import ObjectAlreadyExists # noqa: E402 POOL_NAME = "smoke-pool" async def main() -> int: # Per-invocation suffix gives us a unique deployment name (no DB # collisions) AND a unique marker path. The marker path is set as a # deployment-level parameter — if our server drops `deployment.parameters` # when materialising the flow run (regression #34), the flow body falls # back to its function default and our assertion fails. suffix = uuid4().hex[:8] deployment_name = f"smoke-{suffix}" marker_path = Path(f"/tmp/prefect-worker-smoke-{suffix}.marker") expected_name = f"deployment-param-{suffix}" if marker_path.exists(): marker_path.unlink() async with get_client() as client: # 1. Ensure work pool exists. try: await client.create_work_pool( WorkPoolCreate(name=POOL_NAME, type="process"), ) print(f"created work pool '{POOL_NAME}'") except ObjectAlreadyExists: print(f"reusing existing work pool '{POOL_NAME}'") # 2. Deploy via `Flow.from_source(, ...)` — that # binds the deployment's storage to an absolute filesystem path, # so when the worker downloads the flow code at run-time it # actually finds `_smoke_flow.py` next to wherever it landed. flow_obj: Flow = await Flow.afrom_source( source=str(SCRIPTS_DIR), entrypoint="_smoke_flow.py:hello", ) deployment = await flow_obj.to_deployment( name=deployment_name, work_pool_name=POOL_NAME, # Regression for #34: deployment parameters must be passed # through to the flow run. The marker path written by hello() # depends on these. parameters={"marker_file": str(marker_path), "name": expected_name}, ) deployment_id = await deployment.apply() print(f"deployed: id={deployment_id}, entrypoint={deployment.entrypoint}") # 3. Trigger one ad-hoc run — no per-run parameters override. run = await client.create_flow_run_from_deployment(deployment_id=deployment_id) print(f"queued run: {run.id} (state={run.state.type}/{run.state.name})") # Sanity: the run object the API returned should already have the # deployment's parameters merged in. if run.parameters != {"marker_file": str(marker_path), "name": expected_name}: print(f"❌ run.parameters is {run.parameters!r}, expected the deployment defaults") return 1 # 4. Start a worker that will pick it up. print("starting prefect worker subprocess...") worker_env = os.environ.copy() worker_env["PREFECT_API_URL"] = API_URL worker_env["PYTHONPATH"] = f"{SCRIPTS_DIR}:{worker_env.get('PYTHONPATH', '')}" worker_log = tempfile.NamedTemporaryFile(prefix="worker-", suffix=".log", delete=False) worker = subprocess.Popen( ["uv", "run", "--with", "prefect>=3.0", "prefect", "worker", "start", "--pool", POOL_NAME, "--name", f"smoke-worker-{suffix}"], env=worker_env, stdout=worker_log, stderr=subprocess.STDOUT, cwd=str(SCRIPTS_DIR), # match the deploy-time CWD ) print(f" pid={worker.pid}, log={worker_log.name}") try: # 5. Poll for terminal state. async with get_client() as client: deadline = time.monotonic() + 90 cur = None while time.monotonic() < deadline: cur = await client.read_flow_run(run.id) state = cur.state print(f" poll: {state.type}/{state.name}") if state.is_final(): break await asyncio.sleep(2) else: print("❌ flow run did not reach terminal state within 90s") return 1 assert cur is not None if not cur.state.is_completed(): print(f"❌ flow run reached non-success terminal state: {cur.state.type}/{cur.state.name}") if cur.state.message: print(f" message: {cur.state.message}") return 1 print(f"✅ flow run COMPLETED in {(cur.end_time - cur.start_time).total_seconds():.2f}s") print(f" id = {cur.id}") print(f" start_time = {cur.start_time}") print(f" end_time = {cur.end_time}") print(f" run_count = {cur.run_count}") failures: list[str] = [] if not marker_path.exists(): failures.append( f"flow body marker {marker_path} was never written — proves deployment.parameters" f" never reached the flow body (regression #34)" ) else: marker_contents = marker_path.read_text() if marker_contents != expected_name: failures.append( f"marker contents are {marker_contents!r}, expected {expected_name!r} — proves" f" deployment.parameters were partially dropped (`name` did not reach the flow)" ) if cur.start_time is None: failures.append("start_time should be non-null after Running (global policy)") if cur.end_time is None: failures.append("end_time should be non-null after terminal (global policy)") if (cur.run_count or 0) < 1: failures.append(f"run_count should be >= 1, got {cur.run_count}") if cur.parameters != {"marker_file": str(marker_path), "name": expected_name}: failures.append( f"final flow run parameters are {cur.parameters!r}, expected the deployment defaults" f" — proves /api/flow_runs/{{id}} is not returning the materialised parameters" ) if failures: for msg in failures: print(f"❌ {msg}") return 1 print("✅ marker present + content matches param + global-policy state-bookkeeping intact") return 0 finally: print("terminating worker...") worker.terminate() try: worker.wait(timeout=5) except subprocess.TimeoutExpired: worker.kill() worker_log.close() marker_path.unlink(missing_ok=True) # Dump tail of worker log on failure for visibility. if Path(worker_log.name).exists(): print(f"--- worker log tail ({worker_log.name}) ---") print(Path(worker_log.name).read_text()[-2000:]) if __name__ == "__main__": sys.exit(asyncio.run(main()))