feat(discovery): build_similarity_sources + worker sweep/trigger wiring

Adds registry.build_similarity_sources() (always constructs
ListenBrainzSource; reachability is a per-run health() concern, not a
startup gate) and wires discovery into the worker's main loop: a
scheduled sweep gated on discover.enabled + DISCOVER_TICK_SECONDS, and
a one-shot discover.requested trigger mirroring the existing
scan.requested pattern.

Also hardens run_discovery's health-check pass: a source whose
health() raises is now logged and skipped instead of aborting the
whole sweep (accepted finding from the Task-3 review).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-12 00:22:34 +02:00
parent 6f504a7c04
commit a15f4ae8bb
6 changed files with 119 additions and 2 deletions
+14 -1
View File
@@ -48,6 +48,19 @@ class DiscoveryResult:
albums: int
def _healthy_sources(sources):
"""Return sources whose health() is truthy; a source whose health() raises
is logged and skipped, never aborting the sweep."""
live = []
for s in sources:
try:
if s.health():
live.append(s)
except Exception as e: # a bad health check must not abort the sweep
print(f"worker: discovery source {getattr(s, 'name', '?')} health check failed: {e}", flush=True)
return live
def _seed_artists(conn: psycopg.Connection, cfg: DiscoveryConfig):
with conn.cursor() as cur:
cur.execute(
@@ -134,7 +147,7 @@ def run_discovery(conn: psycopg.Connection, sources: list[SimilaritySource],
"""Aggregate similar artists across the library's seed artists; upsert suggestions."""
seeds = _seed_artists(conn, cfg)
followed = _followed_mbids(conn)
live = [s for s in sources if s.health()]
live = _healthy_sources(sources)
agg: dict[str, dict] = {}
for seed_mbid, _seed_name in seeds:
+33 -1
View File
@@ -3,14 +3,19 @@ import time
from lyra_worker.claim import claim_next
from lyra_worker.config import get_config
from lyra_worker.db import wait_for_db
from lyra_worker.discovery import DiscoveryConfig, run_discovery
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_probe, build_resolver, build_tagger
from lyra_worker.registry import (
build_adapters, build_browser, build_probe, build_resolver,
build_similarity_sources, build_tagger,
)
from lyra_worker.scan import scan_library
IDLE_SLEEP = 2.0
MONITOR_TICK_SECONDS = 60.0
DISCOVER_TICK_SECONDS = 300.0 # check the discovery interval every 5 min (interval itself is hours)
DEST_ROOT = "/music" # must match run_pipeline's default library root
_TRUE = {"1", "true", "yes", "on"}
@@ -32,6 +37,14 @@ def _run_scan(conn, resolver, probe, browser, dest_root: str = DEST_ROOT) -> Non
print(f"worker: library scan done — {result.imported} imported, {result.skipped} skipped", flush=True)
def _run_discovery(conn, sources, browser, config=None) -> None:
cfg = DiscoveryConfig.from_config(config if config is not None else get_config(conn))
result = run_discovery(conn, sources, browser, cfg)
_set_config(conn, "discover.result", f"artists {result.artists}, albums {result.albums}")
_set_config(conn, "discover.requested", "false")
print(f"worker: discovery done — {result.artists} artists, {result.albums} albums", flush=True)
def run_forever() -> None:
conn = wait_for_db()
clear_staging_root(DEST_ROOT) # sweep any staging dirs orphaned by a prior crash
@@ -40,9 +53,11 @@ def run_forever() -> None:
tagger = build_tagger()
browser = build_browser()
probe = build_probe()
similarity_sources = build_similarity_sources(get_config(conn))
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
last_discover_tick = 0.0
try:
while True:
config = get_config(conn)
@@ -56,6 +71,15 @@ def run_forever() -> None:
conn.rollback()
last_tick = now
dcfg = DiscoveryConfig.from_config(config)
if dcfg.enabled and now - last_discover_tick >= DISCOVER_TICK_SECONDS:
try:
_run_discovery(conn, similarity_sources, browser, config)
except Exception as e: # a discovery error must never kill the worker
print(f"worker: discovery sweep failed: {e}", flush=True)
conn.rollback()
last_discover_tick = now
if str(config.get("scan.requested", "")).strip().lower() in _TRUE:
try:
_run_scan(conn, resolver, probe, browser)
@@ -64,6 +88,14 @@ def run_forever() -> None:
conn.rollback()
_set_config(conn, "scan.requested", "false")
if str(config.get("discover.requested", "")).strip().lower() in _TRUE:
try:
_run_discovery(conn, similarity_sources, browser, config)
except Exception as e: # a discovery error must never kill the worker
print(f"worker: discovery run failed: {e}", flush=True)
conn.rollback()
_set_config(conn, "discover.requested", "false")
job_id = claim_next(conn)
if job_id is None:
time.sleep(IDLE_SLEEP)
+8
View File
@@ -12,6 +12,8 @@ from lyra_worker.adapters.youtube import YouTubeAdapter
from lyra_worker.browser import MbBrowser
from lyra_worker.probe import AudioProbe
from lyra_worker.resolver import MbResolver
from lyra_worker.similarity._listenbrainz import ListenBrainzSource
from lyra_worker.similarity.base import SimilaritySource
from lyra_worker.tagger import Tagger
@@ -47,3 +49,9 @@ def build_browser() -> MbBrowser:
def build_probe() -> AudioProbe:
"""The audio probe used by the library scan."""
return MutagenProbe()
def build_similarity_sources(config: dict) -> list[SimilaritySource]:
"""Similarity sources for discovery. ListenBrainz needs no credentials, so it is
always constructed; per-run reachability is checked via each source's health()."""
return [ListenBrainzSource(base_url=config.get("discover.listenBrainzUrl") or "")]