- bench/ FastAPI server: fixed 9-stage pipeline parsed from the base workflow, serial render jobs, WS progress mirroring ComfyUI's push events, /api/view + ComfyUI-compatible /view, /upload/image, /easy-media/* aliases for the ported editor - dual backend: self-contained mock renderer (numpy+ffmpeg, deterministic per prompt/seed, native + RTX-upscaled mp4 branches) and a ComfyUI bridge (substitutes the plan into base_workflow.json, converts to the /prompt API format, polls history, downloads outputs) - timeline port: frontend/src/bench mounts the unmodified MultiTrack editor against a comfyAPI shim (fetchApi/toast/dialog/custom events); build.bench.ts emits bench/ui-dist served by the server - packaging: PyInstaller onedir spec + Tauri2 sidecar docs - tests: bench/tests contract suite + scripts/test_bench_pipeline.py e2e (API+WS+render); prebuilt bench UI committed
119 lines
4.1 KiB
Python
119 lines
4.1 KiB
Python
"""Render job queue: one worker thread, progress events, WS broadcast."""
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import time
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Callable
|
|
|
|
|
|
@dataclass
|
|
class Job:
|
|
id: str
|
|
created_at: float = field(default_factory=time.time)
|
|
status: str = "queued" # queued | running | done | error
|
|
backend: str = "mock"
|
|
stage: str = ""
|
|
progress: float = 0.0
|
|
message: str = "queued"
|
|
result: dict[str, Any] | None = None
|
|
error: str | None = None
|
|
events: list[dict[str, Any]] = field(default_factory=list)
|
|
plan: dict[str, Any] | None = None
|
|
|
|
def snapshot(self) -> dict[str, Any]:
|
|
return {
|
|
"id": self.id,
|
|
"status": self.status,
|
|
"backend": self.backend,
|
|
"stage": self.stage,
|
|
"progress": round(self.progress, 1),
|
|
"message": self.message,
|
|
"result": self.result,
|
|
"error": self.error,
|
|
"plan": self.plan,
|
|
}
|
|
|
|
|
|
BroadcastFn = Callable[[dict[str, Any]], None]
|
|
|
|
|
|
class JobManager:
|
|
"""Serial render queue. Events mirror ComfyUI's push model so the UI shim
|
|
can forward them onto app.api as execution events."""
|
|
|
|
def __init__(self, broadcast: BroadcastFn, max_workers: int = 1) -> None:
|
|
self._pool = ThreadPoolExecutor(max_workers=max_workers)
|
|
self._jobs: dict[str, Job] = {}
|
|
self._order: list[str] = []
|
|
self._lock = threading.Lock()
|
|
self._broadcast = broadcast
|
|
|
|
# -- public -------------------------------------------------------------
|
|
def create(self, backend: str, plan_summary: dict) -> Job:
|
|
job = Job(id=uuid.uuid4().hex[:12], backend=backend, plan=plan_summary)
|
|
with self._lock:
|
|
self._jobs[job.id] = job
|
|
self._order.append(job.id)
|
|
self._push(job, event="job_queued")
|
|
return job
|
|
|
|
def submit(self, job: Job, runner: Callable[[Job, BroadcastFn], dict]) -> None:
|
|
self._pool.submit(self._run, job, runner)
|
|
|
|
def get(self, job_id: str) -> Job | None:
|
|
return self._jobs.get(job_id)
|
|
|
|
def list(self, limit: int = 20) -> list[dict[str, Any]]:
|
|
with self._lock:
|
|
ids = list(reversed(self._order[-limit:]))
|
|
return [self._jobs[i].snapshot() for i in ids if i in self._jobs]
|
|
|
|
# -- internals ------------------------------------------------------------
|
|
def _run(self, job: Job, runner: Callable[[Job, BroadcastFn], dict]) -> None:
|
|
job.status = "running"
|
|
self._push(job, event="execution_start")
|
|
|
|
def progress(stage: str, pct: float, message: str, data: dict | None) -> None:
|
|
job.stage = stage
|
|
job.progress = max(0.0, min(100.0, pct))
|
|
job.message = message
|
|
evt = {
|
|
"stage": stage,
|
|
"progress": job.progress,
|
|
"message": message,
|
|
**({"data": data} if data else {}),
|
|
}
|
|
with self._lock:
|
|
job.events.append({"time": time.time(), **evt})
|
|
self._push(job, event="job_progress", **evt)
|
|
|
|
try:
|
|
result = runner(job, progress)
|
|
job.result = result
|
|
job.status = "done"
|
|
job.progress = 100.0
|
|
job.message = "done"
|
|
self._push(job, event="execution_success", outputs=result.get("outputs", {}))
|
|
except Exception as exc: # noqa: BLE001 - surface everything to the UI
|
|
job.status = "error"
|
|
job.error = f"{type(exc).__name__}: {exc}"
|
|
job.message = job.error
|
|
self._push(job, event="execution_error", error=job.error)
|
|
|
|
def _push(self, job: Job, event: str, **extra: Any) -> None:
|
|
try:
|
|
self._broadcast({
|
|
"type": event,
|
|
"job_id": job.id,
|
|
"status": job.status,
|
|
"stage": job.stage,
|
|
"progress": round(job.progress, 1),
|
|
"message": job.message,
|
|
**extra,
|
|
})
|
|
except Exception: # never let a dead WS client break a render
|
|
pass
|