fix(worker): match library ownership by rgMbid + retire stale failed attempts
Two related monitor bugs surfaced by owned albums that never leave the press:
1. Ownership was matched on (artist, album) strings, but MusicBrainz canonicalizes
the artist on import ("Kanye West"->"Ye", "Ne-Yo"->"Ne‐Yo", "The Goo Goo Dolls"
->"Goo Goo Dolls"). The strings no longer matched, so reconcile/enqueue/backfill
never recognized the album as owned — it stayed "wanted" and re-pressed every
retry interval. Match on the release-group MBID when the library row has one
(falling back to artist+album for scanned rows without an MBID) across
_in_library_at_cutoff, fulfill_owned_releases, and reconcile.
2. Each failed attempt left a needs_attention Request behind, so a hard-to-match
album piled up phantom "needs attention" cards on the press even after a later
attempt imported it (e.g. Maroon 5 "V": 3 stale cards + 1 success). Retire prior
failed attempts when a release is fulfilled or re-attempted (_retire_failed_attempts,
monitor-owned requests only — manual requests have no monitoredReleaseId).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -86,16 +86,40 @@ def _set_state(conn: psycopg.Connection, rel_id: str, state: str) -> None:
|
||||
cur.execute('UPDATE "MonitoredRelease" SET state = %s WHERE id = %s', (state, rel_id))
|
||||
|
||||
|
||||
def _in_library_at_cutoff(conn: psycopg.Connection, artist: str, album: str, cutoff: int) -> bool:
|
||||
def _in_library_at_cutoff(
|
||||
conn: psycopg.Connection, artist: str, album: str, rg_mbid: str, cutoff: int
|
||||
) -> bool:
|
||||
"""True if the album is owned at/above `cutoff`. Matches on the release-group MBID when the
|
||||
library row has one — robust against MusicBrainz artist-name canonicalization (e.g. the
|
||||
followed 'Kanye West' imports as 'Ye') — and falls back to exact artist+album for scanned
|
||||
rows that predate MBID capture."""
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT 1 FROM "LibraryItem" WHERE artist = %s AND album = %s '
|
||||
'AND "qualityClass" >= %s LIMIT 1',
|
||||
(artist, album, cutoff),
|
||||
'SELECT 1 FROM "LibraryItem" WHERE "qualityClass" >= %s AND ('
|
||||
' ("rgMbid" IS NOT NULL AND "rgMbid" = %s) '
|
||||
' OR ("rgMbid" IS NULL AND artist = %s AND album = %s)) LIMIT 1',
|
||||
(cutoff, rg_mbid, artist, album),
|
||||
)
|
||||
return cur.fetchone() is not None
|
||||
|
||||
|
||||
def _retire_failed_attempts(conn: psycopg.Connection, rel_id: str) -> None:
|
||||
"""Delete prior failed (needs_attention) Request rows for this release (Job/Candidate cascade).
|
||||
|
||||
A hard-to-match release is re-pressed every retry interval; each failed attempt used to leave
|
||||
a needs_attention Request behind, piling up phantom "needs attention" cards on the press even
|
||||
after a later attempt imported the album. Retiring them when the release is fulfilled or
|
||||
re-attempted keeps the press showing one live attempt at a time. Manual (non-monitor) requests
|
||||
have no monitoredReleaseId and are never touched."""
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'DELETE FROM "Request" r USING "Job" j '
|
||||
'WHERE j."requestId" = r.id AND r."monitoredReleaseId" = %s '
|
||||
"AND j.state = 'needs_attention'",
|
||||
(rel_id,),
|
||||
)
|
||||
|
||||
|
||||
def _enqueue_one(conn: psycopg.Connection, rel_id: str, artist: str, album: str) -> None:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
@@ -118,7 +142,8 @@ def enqueue_due(conn: psycopg.Connection, cfg: MonitorConfig) -> int:
|
||||
cur.execute(
|
||||
'SELECT mr.id, mr."artistName", mr.album, mr.state, mr."currentQualityClass", '
|
||||
' (mr."firstGrabbedAt" IS NOT NULL '
|
||||
' AND mr."firstGrabbedAt" < now() - make_interval(days => %s)) AS window_expired '
|
||||
' AND mr."firstGrabbedAt" < now() - make_interval(days => %s)) AS window_expired, '
|
||||
' mr."rgMbid" '
|
||||
'FROM "MonitoredRelease" mr '
|
||||
'WHERE mr.monitored = true '
|
||||
' AND NOT mr.ignored ' # user-ignored releases are never enqueued
|
||||
@@ -134,8 +159,11 @@ def enqueue_due(conn: psycopg.Connection, cfg: MonitorConfig) -> int:
|
||||
rows = cur.fetchall()
|
||||
|
||||
enqueued = 0
|
||||
for rel_id, artist, album, state, quality, window_expired in rows:
|
||||
if _in_library_at_cutoff(conn, artist, album, cfg.quality_cutoff):
|
||||
for rel_id, artist, album, state, quality, window_expired, rg_mbid in rows:
|
||||
# A due release is about to be fulfilled or re-attempted; either way any earlier failed
|
||||
# attempt is superseded, so retire it before deciding.
|
||||
_retire_failed_attempts(conn, rel_id)
|
||||
if _in_library_at_cutoff(conn, artist, album, rg_mbid, cfg.quality_cutoff):
|
||||
_set_state(conn, rel_id, "fulfilled")
|
||||
continue
|
||||
if state == "grabbed" and (window_expired or (quality is not None and quality >= cfg.quality_cutoff)):
|
||||
@@ -154,12 +182,19 @@ def fulfill_owned_releases(conn: psycopg.Connection, cfg: MonitorConfig) -> int:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'UPDATE "MonitoredRelease" mr SET state = \'fulfilled\', '
|
||||
' "currentQualityClass" = li.q, '
|
||||
' "currentQualityClass" = owned.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",
|
||||
'FROM ('
|
||||
' SELECT mr2.id AS mid, max(li."qualityClass") AS q '
|
||||
' FROM "MonitoredRelease" mr2 JOIN "LibraryItem" li ON ('
|
||||
# match on rgMbid where the library row has one (survives artist-name canonicalization),
|
||||
# else fall back to exact artist+album for scanned rows lacking an MBID
|
||||
' (li."rgMbid" IS NOT NULL AND li."rgMbid" = mr2."rgMbid") '
|
||||
' OR (li."rgMbid" IS NULL AND li.artist = mr2."artistName" AND li.album = mr2.album)) '
|
||||
" WHERE mr2.monitored = true AND mr2.state <> 'fulfilled' "
|
||||
' GROUP BY mr2.id HAVING max(li."qualityClass") >= %s'
|
||||
') owned '
|
||||
'WHERE mr.id = owned.mid',
|
||||
(cfg.quality_cutoff,),
|
||||
)
|
||||
fixed = cur.rowcount
|
||||
@@ -171,21 +206,28 @@ 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:
|
||||
cur.execute(
|
||||
'SELECT j.state, r."monitoredReleaseId", r.artist, r.album '
|
||||
'FROM "Job" j JOIN "Request" r ON r.id = j."requestId" WHERE j.id = %s',
|
||||
'SELECT j.state, r."monitoredReleaseId", r.artist, r.album, mr."rgMbid" '
|
||||
'FROM "Job" j JOIN "Request" r ON r.id = j."requestId" '
|
||||
'LEFT JOIN "MonitoredRelease" mr ON mr.id = r."monitoredReleaseId" '
|
||||
'WHERE j.id = %s',
|
||||
(job_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
return
|
||||
state, rel_id, artist, album = row
|
||||
state, rel_id, artist, album, rg_mbid = row
|
||||
if rel_id is None or state != "imported":
|
||||
return # needs_attention (or in-flight) leaves the release wanted
|
||||
|
||||
# A successful import supersedes any earlier failed attempts for this release.
|
||||
_retire_failed_attempts(conn, rel_id)
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT "qualityClass" FROM "LibraryItem" WHERE artist = %s AND album = %s',
|
||||
(artist, album),
|
||||
'SELECT max("qualityClass") FROM "LibraryItem" WHERE '
|
||||
' ("rgMbid" IS NOT NULL AND "rgMbid" = %s) '
|
||||
' OR ("rgMbid" IS NULL AND artist = %s AND album = %s)',
|
||||
(rg_mbid, artist, album),
|
||||
)
|
||||
lib = cur.fetchone()
|
||||
quality = lib[0] if lib else None
|
||||
|
||||
Reference in New Issue
Block a user