from __future__ import annotations from typing import Any from app.db.platform_store import get_platform_store from app.modules.compute_gateway.client import ComputeNodeClient def _node_for_task(task: dict[str, Any]) -> dict[str, Any] | None: return next((node for node in get_platform_store().compute_nodes() if node["id"] == task.get("compute_node_id")), None) async def poll_compute_jobs_once() -> dict[str, Any]: store = get_platform_store() synced: list[dict[str, Any]] = [] failed: list[dict[str, str]] = [] for task in store.running_compute_tasks(): node = _node_for_task(task) if not node: failed.append({"task_id": task["id"], "error": "compute node not found"}) continue try: job = await ComputeNodeClient(node["api_base_url"]).get_job(task["compute_job_id"]) synced.append(store.apply_compute_job(task["id"], job)) except Exception as exc: # noqa: BLE001 - keep polling other jobs failed.append({"task_id": task["id"], "error": str(exc)}) return {"synced": len(synced), "failed": failed, "items": synced}