feat: worker runs the library scan when Config scan.requested is set
This commit is contained in:
@@ -6,11 +6,30 @@ from lyra_worker.db import wait_for_db
|
|||||||
from lyra_worker.library import clear_staging_root
|
from lyra_worker.library import clear_staging_root
|
||||||
from lyra_worker.monitor import MonitorConfig, reconcile, sweep
|
from lyra_worker.monitor import MonitorConfig, reconcile, sweep
|
||||||
from lyra_worker.pipeline import run_pipeline
|
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
|
IDLE_SLEEP = 2.0
|
||||||
MONITOR_TICK_SECONDS = 60.0
|
MONITOR_TICK_SECONDS = 60.0
|
||||||
DEST_ROOT = "/music" # must match run_pipeline's default library root
|
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:
|
def run_forever() -> None:
|
||||||
@@ -20,12 +39,14 @@ def run_forever() -> None:
|
|||||||
resolver = build_resolver()
|
resolver = build_resolver()
|
||||||
tagger = build_tagger()
|
tagger = build_tagger()
|
||||||
browser = build_browser()
|
browser = build_browser()
|
||||||
|
probe = build_probe()
|
||||||
print(f"worker: {len(adapters)} adapter(s) enabled: {[a.name for a in adapters]}", flush=True)
|
print(f"worker: {len(adapters)} adapter(s) enabled: {[a.name for a in adapters]}", flush=True)
|
||||||
print("worker: waiting for jobs", flush=True)
|
print("worker: waiting for jobs", flush=True)
|
||||||
last_tick = 0.0
|
last_tick = 0.0
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
mcfg = MonitorConfig.from_config(get_config(conn))
|
config = get_config(conn)
|
||||||
|
mcfg = MonitorConfig.from_config(config)
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
if mcfg.enabled and now - last_tick >= MONITOR_TICK_SECONDS:
|
if mcfg.enabled and now - last_tick >= MONITOR_TICK_SECONDS:
|
||||||
try:
|
try:
|
||||||
@@ -35,6 +56,14 @@ def run_forever() -> None:
|
|||||||
conn.rollback()
|
conn.rollback()
|
||||||
last_tick = now
|
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)
|
job_id = claim_next(conn)
|
||||||
if job_id is None:
|
if job_id is None:
|
||||||
time.sleep(IDLE_SLEEP)
|
time.sleep(IDLE_SLEEP)
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
from lyra_worker.main import _run_scan
|
||||||
|
from lyra_worker.types import MBTarget
|
||||||
|
|
||||||
|
|
||||||
|
class FakeProbe:
|
||||||
|
def probe(self, path):
|
||||||
|
from lyra_worker.probe import ProbeResult
|
||||||
|
return ProbeResult(artist="", album="", fmt="FLAC", quality_class=3)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeResolver:
|
||||||
|
def resolve(self, artist, album):
|
||||||
|
return MBTarget(artist=artist, album=album, rg_mbid="rg1", artist_mbid="a1")
|
||||||
|
|
||||||
|
|
||||||
|
def _set(conn, key, value):
|
||||||
|
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 _get(conn, key):
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute('SELECT value FROM "Config" WHERE key = %s', (key,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
return row[0] if row else None
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_scan_writes_result_and_clears_flag(conn, tmp_path):
|
||||||
|
(tmp_path / "John Mayer" / "Continuum (2006)").mkdir(parents=True)
|
||||||
|
(tmp_path / "John Mayer" / "Continuum (2006)" / "01.flac").write_bytes(b"")
|
||||||
|
_set(conn, "scan.requested", "true")
|
||||||
|
|
||||||
|
_run_scan(conn, FakeResolver(), FakeProbe(), dest_root=str(tmp_path))
|
||||||
|
|
||||||
|
assert _get(conn, "scan.requested") == "false" # flag cleared
|
||||||
|
assert "imported" in (_get(conn, "scan.result") or "") # summary written
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute('SELECT count(*) FROM "LibraryItem"')
|
||||||
|
assert cur.fetchone()[0] == 1
|
||||||
Reference in New Issue
Block a user