feat: worker runs the library scan when Config scan.requested is set

This commit is contained in:
Jonathan
2026-07-11 17:08:51 +02:00
parent 4da0db4a1d
commit c31c8b5249
2 changed files with 75 additions and 2 deletions
+31 -2
View File
@@ -6,11 +6,30 @@ from lyra_worker.db import wait_for_db
from lyra_worker.library import clear_staging_root
from lyra_worker.monitor import MonitorConfig, reconcile, sweep
from lyra_worker.pipeline import run_pipeline
from lyra_worker.registry import build_adapters, build_browser, build_resolver, build_tagger
from lyra_worker.registry import build_adapters, build_browser, build_probe, build_resolver, build_tagger
from lyra_worker.scan import scan_library
IDLE_SLEEP = 2.0
MONITOR_TICK_SECONDS = 60.0
DEST_ROOT = "/music" # must match run_pipeline's default library root
_TRUE = {"1", "true", "yes", "on"}
def _set_config(conn, key: str, value: str) -> None:
with conn.cursor() as cur:
cur.execute(
'INSERT INTO "Config"(key, value, secret, "updatedAt") VALUES (%s, %s, false, now()) '
'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, "updatedAt" = now()',
(key, value),
)
conn.commit()
def _run_scan(conn, resolver, probe, dest_root: str = DEST_ROOT) -> None:
result = scan_library(conn, resolver, probe, dest_root)
_set_config(conn, "scan.result", f"imported {result.imported}, skipped {result.skipped}")
_set_config(conn, "scan.requested", "false")
print(f"worker: library scan done — {result.imported} imported, {result.skipped} skipped", flush=True)
def run_forever() -> None:
@@ -20,12 +39,14 @@ def run_forever() -> None:
resolver = build_resolver()
tagger = build_tagger()
browser = build_browser()
probe = build_probe()
print(f"worker: {len(adapters)} adapter(s) enabled: {[a.name for a in adapters]}", flush=True)
print("worker: waiting for jobs", flush=True)
last_tick = 0.0
try:
while True:
mcfg = MonitorConfig.from_config(get_config(conn))
config = get_config(conn)
mcfg = MonitorConfig.from_config(config)
now = time.monotonic()
if mcfg.enabled and now - last_tick >= MONITOR_TICK_SECONDS:
try:
@@ -35,6 +56,14 @@ def run_forever() -> None:
conn.rollback()
last_tick = now
if str(config.get("scan.requested", "")).strip().lower() in _TRUE:
try:
_run_scan(conn, resolver, probe)
except Exception as e: # a scan error must never kill the worker
print(f"worker: library scan failed: {e}", flush=True)
conn.rollback()
_set_config(conn, "scan.requested", "false")
job_id = claim_next(conn)
if job_id is None:
time.sleep(IDLE_SLEEP)