Skip to content
Pyrula
Pre-release: the packages are not on PyPI yet, so pip install does not resolve. Build from source to try it today.

Serving over HTTP

pyrula.workflows.web serves runs over HTTP: status, an SSE stream and control endpoints. pyrula init --fastapi writes an app in this shape. WorkflowWorker runs the workflows, StoreControl binds the router to the store, mount_run_ui adds the run browser.

import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from pyrula.workflows import ValkeyStore, WorkflowContext, WorkflowWorker, workflow
from pyrula.workflows.web import RunRouter, StoreControl, mount_run_ui
@workflow(name="approval")
async def approval(ctx: WorkflowContext, order: str = "A-1") -> dict:
await ctx.step("prepare", lambda: {"order": order})
signal = await ctx.wait_for_signal("approved")
return await ctx.step("finish", lambda: {"order": order, "by": signal.payload})
store = ValkeyStore(url="redis://localhost:6379")
worker = WorkflowWorker(store=store)
control = StoreControl(store)
@asynccontextmanager
async def lifespan(app: FastAPI):
task = asyncio.create_task(worker.run())
try:
yield
finally:
worker.stop()
await task # a worker that crashed while serving raises here
app = FastAPI(lifespan=lifespan)
RunRouter(control=control, workflows=[approval]).attach(app, prefix="/runs")
mount_run_ui(app, prefix="/ui", control=control, title="Orders")

Run it with uvicorn myapp:app.

workflows=[...] adds POST /{name}/runs and is an allowlist: any other workflow returns 404. Without it the router is read and control only.

Terminal window
curl -X POST localhost:8000/runs/approval/runs \
-H 'content-type: application/json' \
-d '{"order": "A-1042"}'
{
"run_id": "1f9c…",
"status": "pending",
"stream_path": "/runs/approval/stream/1f9c…"
}

The body is validated against the workflow’s signature (422 on a bad argument). Success is 202.

An Idempotency-Key header makes a repeat return the original run. Reusing a key with a different body is a 409.

Terminal window
curl -X POST localhost:8000/runs/approval/runs \
-H 'Idempotency-Key: order-A-1042' \
-H 'content-type: application/json' \
-d '{"order": "A-1042"}'

With max_queue_depth set on RunRouterConfig, a submission arriving when that many runs are pending gets a 429 and Retry-After. Request bodies are capped at max_request_body_bytes (1 MiB default; 413 above it, None lifts the cap).

RunRouter(
control=control,
config=RunRouterConfig(max_queue_depth=1000),
workflows=[approval],
)

create_run_app builds a FastAPI app with a RunRouter plus /healthz and /readyz, for read and control.

from pyrula.workflows.web import RunAppConfig, create_run_app
app = create_run_app(
control=StoreControl(store),
app_config=RunAppConfig(health_path="/healthz", ready_path="/readyz"),
)

With the router attached at /runs, for a workflow named approval:

Method Path Purpose
POST /runs/approval/runs Start a run (only with workflows=[...])
POST /runs/{name}/events Send a trigger event (only with workflows=[...])
GET /runs/runs?name=approval List a workflow’s runs, paged
GET /runs/approval/runs/{run_id} Run status
GET /runs/approval/runs/{run_id}/events Stored events, paged
GET /runs/approval/stream/{run_id} Live SSE event stream
GET /runs/approval/runs/{run_id}/replay Replay the stored stream
POST /runs/approval/runs/{run_id}/signal Deliver a signal: {"name", "payload"}
POST /runs/approval/runs/{run_id}/resume Answer an interrupt
POST /runs/approval/runs/{run_id}/update {"update_name", "payload", "update_id", "timeout"}; waits up to timeout (default 30 s), else 504
POST /runs/approval/runs/{run_id}/query {"query", "args", "kwargs"}
POST /runs/approval/runs/{run_id}/retry Retry a failed run (202)
DELETE /runs/approval/stream/{run_id} Cancel; optional ?reason=
POST /runs/approval/runs/{run_id}/terminate End now (see Cancel and terminate)
Terminal window
curl -X POST localhost:8000/runs/approval/runs/$RUN_ID/signal \
-H 'content-type: application/json' \
-d '{"name": "approved", "payload": {"by": "mike"}}'

/resume answers a run:interrupted wait and needs the wait_id from that event.

Terminal window
curl -X POST localhost:8000/runs/approval/runs/$RUN_ID/resume \
-H 'content-type: application/json' \
-d '{"action": "approve", "wait_id": "b6f2...b0"}'

The SSE stream stops at a durable pause (sleeping, signal-waiting). Every frame has an id:, so a browser EventSource resumes from Last-Event-ID; other clients send it themselves.

The stream is capped by sse_max_streams (256, further opens get 503), sse_max_duration_s (3600, the client resumes) and sse_keepalive_interval (15 s) on RunRouterConfig.

Endpoints are open unless auth is configured. APIKeyAuth and JWTAuth plug in through RunRouterConfig:

from pyrula.contracts.web import AuthConfig
from pyrula.workflows.web import APIKeyAuth, RunRouter, RunRouterConfig
api_keys = APIKeyAuth({"billing": "sk-billing-secret", "reports": "sk-reports-secret"})
RunRouter(
control=control,
config=RunRouterConfig(auth_config=AuthConfig(provider=api_keys)),
).attach(app, prefix="/runs")

APIKeyAuth takes a mapping of caller name to key and authenticates a request as the name, never the key. The name is the run’s owner: billing can’t list or read a run reports submitted. Load keys from the environment or a secret store. JWT verification needs the auth extra.

Built without a provider, the router warns and never refuses. To run open on purpose: AuthConfig(provider=None).

With auth on, a run belongs to the caller who submitted it. Someone else’s run answers 404, and listing returns only your runs.

The run UI reads the store directly, so router auth doesn’t cover it. Give it its own provider:

mount_run_ui(app, prefix="/ui", control=control, title="Orders", auth=api_keys)

Without auth, anyone who can reach the port reads every run and payload, and mount_run_ui warns. auth=None is the deliberate local-only mount (what pyrula dev does).

  • Both providers read a Bearer header, which a browser can’t attach to a page load: put an authenticating reverse proxy in front or keep the UI on loopback.
  • The UI has no per-owner filtering, and failure text is withheld unless expose_error_details=True.

Point StoreControl at a store your workers write to for a read-only run browser, as pyrula dev --attach does (local dev UI).

View this page as Markdown