"""Temporal worker: `python -m coworker worker`. Two workers in one process: fpt-main workflows + research/LLM/design/data/report activities (parallel, thread pool) fpt-browser execute_attempt (Playwright against the single in-memory mock): max concurrency 1""" from __future__ import annotations import asyncio import logging import threading from concurrent.futures import ThreadPoolExecutor from temporalio.client import Client from temporalio.worker import Worker from . import activities from .workflows import WORKFLOWS log = logging.getLogger("fpt.worker") def build_workers(client: Client, svc, main_queue: str, browser_queue: str, max_main: int = 8) -> list[Worker]: activities.set_services(svc) main = Worker(client, task_queue=main_queue, workflows=WORKFLOWS, activities=activities.ACTIVITIES_MAIN, activity_executor=ThreadPoolExecutor(max_main, thread_name_prefix="fpt-act"), max_concurrent_activities=max_main) browser = Worker(client, task_queue=browser_queue, activities=activities.ACTIVITIES_BROWSER, activity_executor=ThreadPoolExecutor(1, thread_name_prefix="fpt-browser"), max_concurrent_activities=1) return [main, browser] async def run_worker(svc, stop: asyncio.Event | None = None) -> None: cfg = svc.cfg client = await Client.connect(cfg.temporal_address, namespace=cfg.temporal_namespace) workers = build_workers(client, svc, cfg.temporal_task_queue, cfg.temporal_browser_queue) if not cfg.embedded_worker: # a standalone worker cleans up after its own crash; embedded: the API already swept from ..platform import liveview liveview.sweep(svc) log.info("worker started: queues %s, %s (browser max 1) on %s", cfg.temporal_task_queue, cfg.temporal_browser_queue, cfg.temporal_address) print(f"FPT Temporal worker started on {cfg.temporal_address} (queues {cfg.temporal_task_queue}, {cfg.temporal_browser_queue})", flush=True) async with workers[0], workers[1]: await (stop.wait() if stop else asyncio.Event().wait()) def start_embedded(svc) -> threading.Thread: """Worker inside the API process (FPT_EMBEDDED_WORKER=1; used by the desktop app and the web E2E).""" def _run(): asyncio.run(run_worker(svc)) t = threading.Thread(target=_run, name="fpt-embedded-worker", daemon=True) t.start() return t