fix(worker): reconcile every job + backfill owned releases

Wanted releases pressed while the monitor is off never got reconciled, so
they lingered on the wanted list. Extract finish_job() and call it for every
finished job (not just when monitor.enabled) so the MonitoredRelease
transitions to fulfilled. Add fulfill_owned_releases(), run once at startup,
to backfill releases already owned at/above the quality cutoff (self-heals
items stuck from before this fix). Both idempotent + tested.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-12 15:08:24 +02:00
parent 50949f747b
commit 082365005c
3 changed files with 76 additions and 8 deletions
+20
View File
@@ -146,6 +146,26 @@ def enqueue_due(conn: psycopg.Connection, cfg: MonitorConfig) -> int:
return enqueued
def fulfill_owned_releases(conn: psycopg.Connection, cfg: MonitorConfig) -> int:
"""Mark fulfilled any monitored, not-yet-fulfilled release already owned at/above the
quality cutoff. Run once at startup so releases pressed while the monitor was off (and
therefore never reconciled) drop off the wanted list. Idempotent. Returns #rows fixed."""
with conn.cursor() as cur:
cur.execute(
'UPDATE "MonitoredRelease" mr SET state = \'fulfilled\', '
' "currentQualityClass" = li.q, '
' "firstGrabbedAt" = COALESCE(mr."firstGrabbedAt", now()) '
'FROM (SELECT artist, album, max("qualityClass") AS q FROM "LibraryItem" '
' GROUP BY artist, album) li '
'WHERE li.artist = mr."artistName" AND li.album = mr.album '
" AND mr.monitored = true AND mr.state <> 'fulfilled' AND li.q >= %s",
(cfg.quality_cutoff,),
)
fixed = cur.rowcount
conn.commit()
return fixed
def reconcile(conn: psycopg.Connection, job_id: str, cfg: MonitorConfig) -> None:
"""Feed a finished job's outcome back into its MonitoredRelease (if any)."""
with conn.cursor() as cur: