Faster startup by loading existing index without any checks, then incremental scanning to update.

This commit is contained in:
2026-05-29 22:25:13 +00:00
parent 25e9bab57b
commit 550f67531a
8 changed files with 153 additions and 93 deletions
+13 -9
View File
@@ -34,7 +34,6 @@ from mediahive.hivescan.images import close_image_client
from mediahive.hivescan.scanner import RootScanner
from mediahive.hivescan.tmdb_client import close_http_client
from mediahive.models.protocol import (
MsgspecResponse,
OpenFolderRequest,
PlayMediaRequest,
RootsRequest,
@@ -285,7 +284,7 @@ async def _attach_scanners() -> None:
"""Ensure every active root context has a running scanner."""
async with _attach_scanners_lock:
for ctx in supervisor.all_contexts().values():
if ctx.scanner is None and ctx.status in {"ready", "loading"}:
if ctx.scanner is None and ctx.status == "ready":
try:
scanner = RootScanner(ctx.root_id, ctx.root_path, ctx.send_event)
await scanner.start()
@@ -296,6 +295,13 @@ async def _attach_scanners() -> None:
)
async def _attach_scanners_loop() -> None:
"""Periodically attach scanners as roots transition to ready."""
while True:
await _attach_scanners()
await asyncio.sleep(1)
async def _activate_all_roots() -> None:
"""Background task: validate and activate all configured roots.
@@ -359,6 +365,7 @@ async def lifespan(_app: FastAPI):
# Defer root activation to a background task so the server starts
# immediately and macOS permission dialogs do not block startup.
activation_task = asyncio.create_task(_activate_all_roots())
scanner_attach_task = asyncio.create_task(_attach_scanners_loop())
logger.info("Server ready; waiting for root activation")
@@ -369,6 +376,10 @@ async def lifespan(_app: FastAPI):
with suppress(asyncio.CancelledError):
await activation_task
scanner_attach_task.cancel()
with suppress(asyncio.CancelledError):
await scanner_attach_task
await supervisor.shutdown()
with suppress(Exception):
@@ -434,13 +445,6 @@ async def put_roots(request: Request):
}
@app.get("/api/roots/{root_id}/index")
async def get_root_index(root_id: str):
"""Return the full index for a single root."""
ctx = _get_context(root_id)
return MsgspecResponse(ctx.store.get_full_index())
@app.get("/api/roots/{root_id}/status")
async def get_root_status(root_id: str):
"""Return status for a single root."""