Assemble complete modular LAB recordings on Worker before readiness
This commit is contained in:
+35
-17
@@ -597,6 +597,11 @@ def _m48_recorded_camera_playback_source(
|
||||
def _canonical_lab_recording_source(session_id: str) -> tuple[Path, str] | None:
|
||||
"""Read a ready operator recording for the existing portable result viewer."""
|
||||
snapshot = session_recording_preparation_manager.status(session_id)
|
||||
if snapshot is None:
|
||||
# Restore an already published source after a Core restart. This never
|
||||
# schedules source conversion, media processing or a local renderer.
|
||||
command = operator_session_store.prepare_replay(session_id, speed=1.0, loop=False)
|
||||
snapshot = session_recording_preparation_manager.restore_published(command)
|
||||
if snapshot is None or snapshot.state != "ready" or snapshot.recording is None:
|
||||
return None
|
||||
if snapshot.command.map_version is not None:
|
||||
@@ -747,6 +752,9 @@ async def _portable_result_publication_reconciler() -> None:
|
||||
await asyncio.to_thread(service.run_once)
|
||||
with suppress(OSError, ValueError):
|
||||
await asyncio.to_thread(service.artifact_transport.collect_source_copies)
|
||||
if replay_finalization is not None:
|
||||
with suppress(OSError, ValueError):
|
||||
await asyncio.to_thread(replay_finalization.run_once)
|
||||
await asyncio.sleep(15.0)
|
||||
|
||||
|
||||
@@ -1012,25 +1020,34 @@ lab_deletion_service = LabDeletionCoordinator(
|
||||
)
|
||||
app.include_router(build_local_storage_router(local_presentation_copies))
|
||||
|
||||
replay_finalization = None
|
||||
if session_artifact_gateway is not None and _ffmpeg is not None:
|
||||
app.include_router(
|
||||
build_portable_replay_router(
|
||||
PortableReplayService(
|
||||
view=PortableResultViewService(
|
||||
sessions=session_store, artifacts=session_artifact_gateway.store
|
||||
),
|
||||
data_dir=session_store.data_dir,
|
||||
media=session_recorded_media_inspector,
|
||||
recording_source=_canonical_lab_recording_source,
|
||||
ffmpeg_path=_ffmpeg,
|
||||
local_copies=local_presentation_copies,
|
||||
remote_replays=WorkerReplayRestore(laboratory_worker_gateway.configuration)
|
||||
if laboratory_worker_gateway.configuration is not None else None,
|
||||
),
|
||||
composition_runs=OBSERVATORY_AI_COMPOSITION_RUNS,
|
||||
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
||||
)
|
||||
replay_service = PortableReplayService(
|
||||
view=PortableResultViewService(
|
||||
sessions=session_store, artifacts=session_artifact_gateway.store
|
||||
),
|
||||
data_dir=session_store.data_dir, media=session_recorded_media_inspector,
|
||||
recording_source=_canonical_lab_recording_source, ffmpeg_path=_ffmpeg,
|
||||
local_copies=local_presentation_copies,
|
||||
remote_replays=WorkerReplayRestore(laboratory_worker_gateway.configuration)
|
||||
if laboratory_worker_gateway.configuration is not None else None,
|
||||
worker_required=lambda key: (
|
||||
OBSERVATORY_AI_COMPOSITION_RUNS.get(key).replay_policy == "worker-ready-v1"
|
||||
if OBSERVATORY_AI_COMPOSITION_RUNS is not None else False
|
||||
),
|
||||
)
|
||||
if (laboratory_worker_gateway.configuration is not None
|
||||
and OBSERVATORY_AI_COMPOSITION_RUNS is not None
|
||||
and OBSERVATORY_RECORDED_JOB_QUEUE is not None):
|
||||
from k1link.observatory.replay_finalization import ReplayFinalization
|
||||
replay_finalization = ReplayFinalization(
|
||||
OBSERVATORY_AI_COMPOSITION_RUNS, OBSERVATORY_RECORDED_JOB_QUEUE,
|
||||
replay_service, laboratory_worker_gateway.configuration,
|
||||
)
|
||||
app.include_router(build_portable_replay_router(
|
||||
replay_service, composition_runs=OBSERVATORY_AI_COMPOSITION_RUNS,
|
||||
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
||||
))
|
||||
|
||||
for legacy_router in plugin_environment.legacy_routers:
|
||||
app.include_router(legacy_router)
|
||||
@@ -1112,6 +1129,7 @@ if (
|
||||
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
||||
view_profiles=OBSERVATORY_LAB_VIEW_PROFILES,
|
||||
deletion_service=lab_deletion_service,
|
||||
replay_finalization=replay_finalization,
|
||||
)
|
||||
)
|
||||
if OBSERVATORY_WORKER_DISPATCH_READY:
|
||||
|
||||
@@ -37,6 +37,7 @@ from k1link.observatory.recorded_jobs import (
|
||||
ObservatoryRecordedQueueError,
|
||||
ObservatoryRecordedQueueNotFoundError,
|
||||
)
|
||||
from k1link.observatory.replay_finalization import ReplayFinalization
|
||||
from k1link.observatory.source_admission import PortableSourceNotPreparedError
|
||||
from k1link.sessions import SessionNotFoundError, SessionStore
|
||||
|
||||
@@ -122,6 +123,7 @@ def build_modular_observatory_router(
|
||||
queue: ObservatoryRecordedJobQueue | None = None,
|
||||
view_profiles: LabViewProfileStore | None = None,
|
||||
deletion_service=None,
|
||||
replay_finalization: ReplayFinalization | None = None,
|
||||
) -> APIRouter:
|
||||
router = APIRouter()
|
||||
|
||||
@@ -311,12 +313,13 @@ def build_modular_observatory_router(
|
||||
setup_ids=tuple(setup_ids),
|
||||
job_ids=tuple(job.job_id for job in jobs),
|
||||
idempotency_key=request.idempotency_key,
|
||||
replay_policy="worker-ready-v1" if replay_finalization is not None else None,
|
||||
created_at_utc=datetime.now(UTC)
|
||||
.isoformat(timespec="milliseconds")
|
||||
.replace("+00:00", "Z"),
|
||||
)
|
||||
presentation = ontology.project_composition(composition)
|
||||
run_projection = {**run.as_dict(), "presentation": presentation}
|
||||
run_projection = {**public_run(run), "presentation": presentation}
|
||||
except StopIteration:
|
||||
pass
|
||||
except (CompositionRunError, ObservatoryOntologyError) as exc:
|
||||
@@ -343,6 +346,11 @@ def build_modular_observatory_router(
|
||||
},
|
||||
}
|
||||
|
||||
def public_run(run: CompositionRun) -> dict[str, object]:
|
||||
value = run.as_dict()
|
||||
value.pop("replay_policy", None)
|
||||
return value
|
||||
|
||||
def project_run(run: CompositionRun) -> dict[str, object]:
|
||||
if ontology is None:
|
||||
raise CompositionRunError("composition ontology is unavailable")
|
||||
@@ -359,8 +367,14 @@ def build_modular_observatory_router(
|
||||
for job in jobs
|
||||
)
|
||||
failed = any(job.state == "failed" or job.publication_state == "failed" for job in jobs)
|
||||
replay_state = None
|
||||
if exact.replay_policy == "worker-ready-v1":
|
||||
replay_state = (replay_finalization.status(exact.run_id)
|
||||
if replay_finalization else {"state": "pending"})
|
||||
published = published and replay_state["state"] == "ready"
|
||||
failed = failed or replay_state["state"] == "failed"
|
||||
return {
|
||||
**exact.as_dict(),
|
||||
**public_run(exact),
|
||||
"state": "ready" if published else "failed" if failed else "running",
|
||||
"configuration_label": (
|
||||
f"{store.get_session(exact.source_session_id).summary.display_name} · "
|
||||
@@ -372,6 +386,15 @@ def build_modular_observatory_router(
|
||||
"result_ids": [job.result_id for job in jobs if job.result_id is not None],
|
||||
}
|
||||
|
||||
@router.post("/api/v1/observatory/ai-composition-runs/{run_id}/retry-replay")
|
||||
def retry_replay(run_id: str) -> dict[str, Any]:
|
||||
if replay_finalization is None:
|
||||
raise HTTPException(503, "Сборщик записи недоступен.")
|
||||
try:
|
||||
return replay_finalization.retry(run_id)
|
||||
except (ValueError, OSError) as exc:
|
||||
raise HTTPException(409, "Не удалось повторить сборку записи.") from exc
|
||||
|
||||
@router.get("/api/v1/observatory/ai-composition-runs")
|
||||
def composition_run_list(
|
||||
source_session_id: str = Query(
|
||||
|
||||
Reference in New Issue
Block a user