feat: monitor outcome reconcile

This commit is contained in:
Jonathan
2026-07-11 12:29:26 +02:00
parent 8f72568d87
commit 1f23aaa0e8
2 changed files with 119 additions and 0 deletions
+33
View File
@@ -139,3 +139,36 @@ def enqueue_due(conn: psycopg.Connection, cfg: MonitorConfig) -> int:
enqueued += 1
conn.commit()
return enqueued
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',
(job_id,),
)
row = cur.fetchone()
if row is None:
return
state, rel_id, artist, album = row
if rel_id is None or state != "imported":
return # needs_attention (or in-flight) leaves the release wanted
with conn.cursor() as cur:
cur.execute(
'SELECT "qualityClass" FROM "LibraryItem" WHERE artist = %s AND album = %s',
(artist, album),
)
lib = cur.fetchone()
quality = lib[0] if lib else None
new_state = "fulfilled" if (quality is not None and quality >= cfg.quality_cutoff) else "grabbed"
with conn.cursor() as cur:
cur.execute(
'UPDATE "MonitoredRelease" '
'SET "currentQualityClass" = %s, state = %s, '
' "firstGrabbedAt" = COALESCE("firstGrabbedAt", now()) WHERE id = %s',
(quality, new_state, rel_id),
)
conn.commit()