|
| 1 | +""" |
| 2 | +FastAPI + Conductor workers in one process. |
| 3 | +
|
| 4 | +Install (example-only deps): |
| 5 | + pip install fastapi uvicorn |
| 6 | +
|
| 7 | +Run (single web worker; TaskHandler will spawn one process per Conductor worker): |
| 8 | + export CONDUCTOR_SERVER_URL="http://localhost:8080/api" |
| 9 | + export CONDUCTOR_AUTH_KEY="..." |
| 10 | + export CONDUCTOR_AUTH_SECRET="..." |
| 11 | + uvicorn examples.fastapi_worker_service:app --host 0.0.0.0 --port 8081 --workers 1 |
| 12 | +
|
| 13 | +Trigger the workflow via API (waits up to 10s for completion): |
| 14 | + curl -s -X POST http://localhost:8081/v1/hello \\ |
| 15 | + -H 'content-type: application/json' \\ |
| 16 | + -d '{"name":"Ada","a":2,"b":3}' | jq . |
| 17 | +
|
| 18 | +Notes: |
| 19 | + - Do NOT run uvicorn with multiple web workers unless you explicitly want multiple independent TaskHandlers polling. |
| 20 | + - TaskHandler supervision is enabled by default (monitor + restart worker subprocesses). |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import os |
| 26 | +from contextlib import asynccontextmanager |
| 27 | +from typing import Optional |
| 28 | + |
| 29 | +from fastapi import FastAPI |
| 30 | +from fastapi.responses import JSONResponse |
| 31 | +from pydantic import BaseModel, Field |
| 32 | + |
| 33 | +from conductor.client.automator.task_handler import TaskHandler |
| 34 | +from conductor.client.configuration.configuration import Configuration |
| 35 | +from conductor.client.context.task_context import get_task_context |
| 36 | +from conductor.client.orkes_clients import OrkesClients |
| 37 | +from conductor.client.worker.worker_task import worker_task |
| 38 | +from conductor.client.workflow.conductor_workflow import ConductorWorkflow |
| 39 | +from conductor.client.workflow.executor.workflow_executor import WorkflowExecutor |
| 40 | + |
| 41 | + |
| 42 | +# --------------------------------------------------------------------------- |
| 43 | +# Example worker(s) |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | + |
| 46 | +@worker_task( |
| 47 | + task_definition_name="fastapi_normalize_name", |
| 48 | + poll_interval_millis=100, |
| 49 | + register_task_def=True, |
| 50 | + overwrite_task_def=False, |
| 51 | +) |
| 52 | +def normalize_name(name: str) -> str: |
| 53 | + # This shows how to access task context safely. |
| 54 | + _ = get_task_context() |
| 55 | + return name.strip().title() |
| 56 | + |
| 57 | + |
| 58 | +@worker_task( |
| 59 | + task_definition_name="fastapi_add_numbers", |
| 60 | + poll_interval_millis=100, |
| 61 | + register_task_def=True, |
| 62 | + overwrite_task_def=False, |
| 63 | +) |
| 64 | +def add_numbers(a: int, b: int) -> int: |
| 65 | + _ = get_task_context() |
| 66 | + return a + b |
| 67 | + |
| 68 | + |
| 69 | +@worker_task( |
| 70 | + task_definition_name="fastapi_build_message", |
| 71 | + poll_interval_millis=100, |
| 72 | + register_task_def=True, |
| 73 | + overwrite_task_def=False, |
| 74 | +) |
| 75 | +def build_message(normalized_name: str, total: int) -> dict: |
| 76 | + ctx = get_task_context() |
| 77 | + return { |
| 78 | + "message": f"Hello {normalized_name}! {total=}", |
| 79 | + "normalized_name": normalized_name, |
| 80 | + "total": total, |
| 81 | + "task_id": ctx.get_task_id(), |
| 82 | + "workflow_id": ctx.get_workflow_instance_id(), |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +def _build_hello_workflow(executor: WorkflowExecutor) -> ConductorWorkflow: |
| 87 | + workflow = ConductorWorkflow(executor=executor, name="fastapi_hello_workflow", version=1) |
| 88 | + |
| 89 | + t1 = normalize_name(task_ref_name="normalize_name_ref", name=workflow.input("name")) |
| 90 | + t2 = add_numbers(task_ref_name="add_numbers_ref", a=workflow.input("a"), b=workflow.input("b")) |
| 91 | + t3 = build_message( |
| 92 | + task_ref_name="build_message_ref", |
| 93 | + normalized_name=t1.output("result"), |
| 94 | + total=t2.output("result"), |
| 95 | + ) |
| 96 | + |
| 97 | + workflow >> t1 >> t2 >> t3 |
| 98 | + |
| 99 | + workflow.output_parameters( |
| 100 | + output_parameters={ |
| 101 | + "message": t3.output("message"), |
| 102 | + "normalized_name": t3.output("normalized_name"), |
| 103 | + "total": t3.output("total"), |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + return workflow |
| 108 | + |
| 109 | + |
| 110 | +class HelloRequest(BaseModel): |
| 111 | + name: str = Field(default="World", description="Name to greet") |
| 112 | + a: int = Field(default=1, description="First number") |
| 113 | + b: int = Field(default=2, description="Second number") |
| 114 | + |
| 115 | + |
| 116 | +# --------------------------------------------------------------------------- |
| 117 | +# FastAPI app + TaskHandler lifecycle |
| 118 | +# --------------------------------------------------------------------------- |
| 119 | + |
| 120 | +task_handler: Optional[TaskHandler] = None |
| 121 | +workflow_executor: Optional[WorkflowExecutor] = None |
| 122 | +api_config: Optional[Configuration] = None |
| 123 | + |
| 124 | + |
| 125 | +@asynccontextmanager |
| 126 | +async def lifespan(app: FastAPI): |
| 127 | + global task_handler, workflow_executor, api_config |
| 128 | + |
| 129 | + api_config = Configuration() |
| 130 | + clients = OrkesClients(configuration=api_config) |
| 131 | + workflow_executor = clients.get_workflow_executor() |
| 132 | + |
| 133 | + # scan_for_annotated_workers=True will pick up @worker_task functions in this module. |
| 134 | + task_handler = TaskHandler( |
| 135 | + workers=[], |
| 136 | + configuration=api_config, |
| 137 | + scan_for_annotated_workers=True, |
| 138 | + # Defaults are already True, but keeping these explicit in the example: |
| 139 | + monitor_processes=True, |
| 140 | + restart_on_failure=True, |
| 141 | + ) |
| 142 | + task_handler.start_processes() |
| 143 | + |
| 144 | + try: |
| 145 | + yield |
| 146 | + finally: |
| 147 | + if task_handler is not None: |
| 148 | + task_handler.stop_processes() |
| 149 | + task_handler = None |
| 150 | + workflow_executor = None |
| 151 | + api_config = None |
| 152 | + |
| 153 | + |
| 154 | +app = FastAPI(lifespan=lifespan) |
| 155 | + |
| 156 | + |
| 157 | +@app.get("/healthcheck") |
| 158 | +def healthcheck(): |
| 159 | + # 503 if worker processes aren't healthy; useful for container orchestrators. |
| 160 | + if task_handler is None: |
| 161 | + return JSONResponse({"ok": False, "detail": "workers_not_started"}, status_code=503) |
| 162 | + |
| 163 | + ok = task_handler.is_healthy() |
| 164 | + payload = { |
| 165 | + "ok": ok, |
| 166 | + "workers": task_handler.get_worker_process_status(), |
| 167 | + } |
| 168 | + return JSONResponse(payload, status_code=200 if ok else 503) |
| 169 | + |
| 170 | + |
| 171 | +@app.post("/v1/hello") |
| 172 | +def hello(req: HelloRequest): |
| 173 | + """ |
| 174 | + Expose a Conductor workflow as an API: |
| 175 | + - Builds an inline workflow definition with 3 SIMPLE tasks |
| 176 | + - Starts it and waits up to 10 seconds for completion |
| 177 | + - Returns workflow output as the HTTP response |
| 178 | + """ |
| 179 | + if task_handler is None or workflow_executor is None or api_config is None: |
| 180 | + return JSONResponse({"ok": False, "detail": "service_not_ready"}, status_code=503) |
| 181 | + if not task_handler.is_healthy(): |
| 182 | + return JSONResponse( |
| 183 | + {"ok": False, "detail": "workers_unhealthy", "workers": task_handler.get_worker_process_status()}, |
| 184 | + status_code=503, |
| 185 | + ) |
| 186 | + |
| 187 | + workflow = _build_hello_workflow(executor=workflow_executor) |
| 188 | + payload = req.model_dump() if hasattr(req, "model_dump") else req.dict() # pydantic v2/v1 |
| 189 | + |
| 190 | + try: |
| 191 | + run = workflow.execute(workflow_input=payload, wait_for_seconds=10) |
| 192 | + except Exception as e: |
| 193 | + return JSONResponse({"ok": False, "detail": "workflow_start_failed", "error": str(e)}, status_code=502) |
| 194 | + |
| 195 | + response = { |
| 196 | + "ok": run.status == "COMPLETED", |
| 197 | + "workflow_id": run.workflow_id, |
| 198 | + "status": run.status, |
| 199 | + "output": run.output, |
| 200 | + "ui_url": f"{api_config.ui_host}/execution/{run.workflow_id}", |
| 201 | + } |
| 202 | + return JSONResponse(response, status_code=200 if run.status == "COMPLETED" else 202) |
| 203 | + |
| 204 | + |
| 205 | +if __name__ == "__main__": |
| 206 | + import uvicorn |
| 207 | + |
| 208 | + uvicorn.run( |
| 209 | + "examples.fastapi_worker_service:app", |
| 210 | + host="0.0.0.0", |
| 211 | + port=int(os.getenv("PORT", "8081")), |
| 212 | + workers=1, |
| 213 | + ) |
0 commit comments