fix(worker): run discovery at most once per loop iteration

The scheduled-tick branch and the discover.requested one-shot branch
both read the same stale per-iteration config snapshot, so an enabled+due
sweep coinciding with a pending request ran discovery twice back-to-back
(idempotent but wasteful). Extract maybe_run_discovery, which unifies both
triggers into a single run and is now unit-tested.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-12 10:42:48 +02:00
parent 830de07997
commit 9ba587d136
2 changed files with 67 additions and 16 deletions
+23 -15
View File
@@ -45,6 +45,26 @@ def _run_discovery(conn, sources, browser, config=None) -> None:
print(f"worker: discovery done — {result.artists} artists, {result.albums} albums", flush=True)
def maybe_run_discovery(conn, sources, browser, config, dcfg, now, last_discover_tick,
tick_seconds: float = DISCOVER_TICK_SECONDS) -> float:
"""Run discovery at most once per loop iteration, whether triggered by the schedule
(enabled + interval elapsed) or a one-shot ``discover.requested`` flag. A single run
satisfies both, so the two triggers must not fire it twice off the same stale config
snapshot. Returns the (possibly advanced) last_discover_tick."""
requested = str(config.get("discover.requested", "")).strip().lower() in _TRUE
due = dcfg.enabled and now - last_discover_tick >= tick_seconds
if not (requested or due):
return last_discover_tick
try:
_run_discovery(conn, sources, browser, config) # also clears discover.requested
except Exception as e: # a discovery error must never kill the worker
print(f"worker: discovery run failed: {e}", flush=True)
conn.rollback()
if requested:
_set_config(conn, "discover.requested", "false")
return now if due else last_discover_tick
def run_forever() -> None:
conn = wait_for_db()
clear_staging_root(DEST_ROOT) # sweep any staging dirs orphaned by a prior crash
@@ -72,13 +92,9 @@ def run_forever() -> None:
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
last_discover_tick = maybe_run_discovery(
conn, similarity_sources, browser, config, dcfg, now, last_discover_tick
)
if str(config.get("scan.requested", "")).strip().lower() in _TRUE:
try:
@@ -88,14 +104,6 @@ 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)