|
| 1 | +"""Deferred selection of the v2 workflow host (warm PROD registry vs DB-backed). |
| 2 | +
|
| 3 | +Which host serves ``POST /api/v2/workflows`` depends on ``settings.prod`` |
| 4 | +(``LANGFLOW_PROD``). The problem: ``langflow.__main__`` imports the router module |
| 5 | +(and therefore builds this router) *before* it runs ``load_dotenv(--env-file)``, so |
| 6 | +reading the env — or the settings service — at import time would miss any value |
| 7 | +supplied via ``--env-file``. That is exactly the ordering trap the extensions router |
| 8 | +documents for ``LANGFLOW_ENABLE_EXTENSION_RELOAD``. |
| 9 | +
|
| 10 | +``DeferredWorkflowHost`` defers the choice to first use. By the time any workflow |
| 11 | +route (or capability flag) is exercised, ``setup_app``/lifespan has initialized the |
| 12 | +settings service from the fully-loaded environment, so ``settings.prod`` is correct. |
| 13 | +The route *structure* is identical for both hosts (the shared router only reads |
| 14 | +``supports_*`` at request time, and ``auto_register_job_routes=False`` neutralizes |
| 15 | +the one mount-time read), so binding this single proxy at import changes nothing |
| 16 | +structurally — only which concrete host each request lands on. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from typing import TYPE_CHECKING, Any |
| 22 | + |
| 23 | +from lfx.workflow.host import WorkflowHostBase |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + from collections.abc import AsyncIterator |
| 27 | + |
| 28 | + from fastapi import BackgroundTasks, Request, Response |
| 29 | + from lfx.schema.workflow import ( |
| 30 | + ParsedWorkflowRun, |
| 31 | + WorkflowExecutionResponse, |
| 32 | + WorkflowJobResponse, |
| 33 | + WorkflowStopResponse, |
| 34 | + ) |
| 35 | + from lfx.workflow.host import ResolvedFlow, WorkflowAction |
| 36 | + |
| 37 | + |
| 38 | +class DeferredWorkflowHost(WorkflowHostBase): |
| 39 | + """A ``WorkflowHost`` that picks the concrete host lazily from ``settings.prod``. |
| 40 | +
|
| 41 | + Every member delegates to the resolved host. Resolution is cached only once the |
| 42 | + settings service is initialized, so an access during module import (before |
| 43 | + ``--env-file`` is loaded) never force-initializes settings nor caches a stale |
| 44 | + choice — it falls back transiently and re-resolves on the next call. |
| 45 | + """ |
| 46 | + |
| 47 | + def __init__(self) -> None: |
| 48 | + self._host: WorkflowHostBase | None = None |
| 49 | + |
| 50 | + def _resolve(self) -> WorkflowHostBase: |
| 51 | + if self._host is not None: |
| 52 | + return self._host |
| 53 | + from langflow.api.v2.warm_workflow_host import WarmWorkflowHost |
| 54 | + from langflow.api.v2.workflow_host import LangflowWorkflowHost |
| 55 | + from langflow.services.deps import get_settings_service, is_settings_service_initialized |
| 56 | + |
| 57 | + # Do NOT force-initialize settings from a half-loaded environment at import |
| 58 | + # time (the router is built before ``load_dotenv(--env-file)``). Until the |
| 59 | + # settings service exists, fall back to the DB host WITHOUT caching so the |
| 60 | + # real choice is still made on the first post-startup call. |
| 61 | + if not is_settings_service_initialized(): |
| 62 | + return LangflowWorkflowHost() |
| 63 | + host: WorkflowHostBase = WarmWorkflowHost() if get_settings_service().settings.prod else LangflowWorkflowHost() |
| 64 | + self._host = host |
| 65 | + return host |
| 66 | + |
| 67 | + @property |
| 68 | + def supports_background(self) -> bool: |
| 69 | + return self._resolve().supports_background |
| 70 | + |
| 71 | + @property |
| 72 | + def supports_request_overrides(self) -> bool: |
| 73 | + return self._resolve().supports_request_overrides |
| 74 | + |
| 75 | + async def resolve_caller(self, request: Request) -> Any: |
| 76 | + return await self._resolve().resolve_caller(request) |
| 77 | + |
| 78 | + async def get_flow(self, flow_id: str, caller: Any) -> ResolvedFlow: |
| 79 | + return await self._resolve().get_flow(flow_id, caller) |
| 80 | + |
| 81 | + async def authorize(self, caller: Any, flow: ResolvedFlow, action: WorkflowAction) -> None: |
| 82 | + return await self._resolve().authorize(caller, flow, action) |
| 83 | + |
| 84 | + def session(self) -> AsyncIterator[Any | None]: |
| 85 | + # Returns the concrete host's async context manager (used as ``async with``). |
| 86 | + return self._resolve().session() |
| 87 | + |
| 88 | + async def run_sync( |
| 89 | + self, |
| 90 | + parsed: ParsedWorkflowRun, |
| 91 | + flow: ResolvedFlow, |
| 92 | + caller: Any, |
| 93 | + *, |
| 94 | + http_request: Request, |
| 95 | + background_tasks: BackgroundTasks, |
| 96 | + ) -> WorkflowExecutionResponse: |
| 97 | + return await self._resolve().run_sync( |
| 98 | + parsed, flow, caller, http_request=http_request, background_tasks=background_tasks |
| 99 | + ) |
| 100 | + |
| 101 | + def stream_response( |
| 102 | + self, |
| 103 | + parsed: ParsedWorkflowRun, |
| 104 | + flow: ResolvedFlow, |
| 105 | + caller: Any, |
| 106 | + *, |
| 107 | + stream_protocol: str, |
| 108 | + http_request: Request, |
| 109 | + background_tasks: BackgroundTasks, |
| 110 | + ) -> Response: |
| 111 | + return self._resolve().stream_response( |
| 112 | + parsed, |
| 113 | + flow, |
| 114 | + caller, |
| 115 | + stream_protocol=stream_protocol, |
| 116 | + http_request=http_request, |
| 117 | + background_tasks=background_tasks, |
| 118 | + ) |
| 119 | + |
| 120 | + async def submit_background( |
| 121 | + self, |
| 122 | + parsed: ParsedWorkflowRun, |
| 123 | + flow: ResolvedFlow, |
| 124 | + caller: Any, |
| 125 | + *, |
| 126 | + stream_protocol: str, |
| 127 | + ) -> WorkflowJobResponse: |
| 128 | + return await self._resolve().submit_background(parsed, flow, caller, stream_protocol=stream_protocol) |
| 129 | + |
| 130 | + async def get_job_status( |
| 131 | + self, job_id: str, caller: Any, session: Any |
| 132 | + ) -> WorkflowExecutionResponse | WorkflowJobResponse: |
| 133 | + return await self._resolve().get_job_status(job_id, caller, session) |
| 134 | + |
| 135 | + async def stop_job(self, job_id: str, caller: Any) -> WorkflowStopResponse: |
| 136 | + return await self._resolve().stop_job(job_id, caller) |
0 commit comments