feat(worker): share the MusicBrainz cache from scans/discovery
The worker's MB browser (discography + artist search, used by monitor sweeps, library scans, and Last.fm discovery) re-fetched every time. Route it through the same ApiCache table + logical keys the web app uses, so a discography one process fetched warms the other, and repeat lookups stop hitting MB. - apicache.py cached_api(): read-through, serve-stale-on-outage, never caches null, best-effort (a cache-DB error degrades to a live fetch, never breaks scan/sweep/discovery). Its own DEDICATED autocommit connection so it can't commit the worker's in-flight transaction, and autocommit avoids freezing Postgres now() (which would break TTL math). - CachingMbBrowser decorates MusicBrainzBrowser, serializing to the SAME camelCase JSON shape the web writes (round-trip tested). registry.build_browser wraps it when a DSN is present; the Last.fm source's browser rides it too. - Hourly prune drops ApiCache rows > 90d so the table stays bounded. - worker 213 tests / 7-skip. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,72 @@
|
||||
from lyra_worker.apicache import DAY, MONTH, cached_api, norm_key, open_cache_conn
|
||||
from lyra_worker.browser import ArtistHit, ReleaseGroupInfo
|
||||
|
||||
|
||||
# Serialize to the SAME camelCase JSON shape the web app stores (web/src/lib/musicbrainz.ts),
|
||||
# so a discography one process fetched warms the cache for the other.
|
||||
def _artist_hit_to_json(a: ArtistHit) -> dict:
|
||||
return {"mbid": a.mbid, "name": a.name, "disambiguation": a.disambiguation}
|
||||
|
||||
|
||||
def _artist_hit_from_json(d: dict) -> ArtistHit:
|
||||
return ArtistHit(mbid=d["mbid"], name=d.get("name", ""), disambiguation=d.get("disambiguation", ""))
|
||||
|
||||
|
||||
def _rg_to_json(r: ReleaseGroupInfo) -> dict:
|
||||
return {
|
||||
"rgMbid": r.rg_mbid,
|
||||
"title": r.title,
|
||||
"primaryType": r.primary_type or None,
|
||||
"secondaryTypes": list(r.secondary_types),
|
||||
"firstReleaseDate": r.first_release_date or None,
|
||||
}
|
||||
|
||||
|
||||
def _rg_from_json(d: dict) -> ReleaseGroupInfo:
|
||||
return ReleaseGroupInfo(
|
||||
rg_mbid=d["rgMbid"],
|
||||
title=d.get("title", "") or "",
|
||||
primary_type=d.get("primaryType") or "",
|
||||
secondary_types=tuple(d.get("secondaryTypes") or ()),
|
||||
first_release_date=d.get("firstReleaseDate") or "",
|
||||
)
|
||||
|
||||
|
||||
class CachingMbBrowser:
|
||||
"""Wraps an MbBrowser with the shared ApiCache read-through, keyed on the same logical
|
||||
strings the web side uses (`artist-search:{q}`, `artist-rgs:{mbid}`). Uses a dedicated,
|
||||
lazily (re)opened cache connection; if it can't connect, it just calls through uncached."""
|
||||
|
||||
def __init__(self, inner, dsn: str):
|
||||
self._inner = inner
|
||||
self._dsn = dsn
|
||||
self._conn = None
|
||||
|
||||
def _cache_conn(self):
|
||||
if self._conn is not None and not self._conn.closed:
|
||||
return self._conn
|
||||
self._conn = open_cache_conn(self._dsn)
|
||||
return self._conn
|
||||
|
||||
def search_artist(self, name: str) -> list[ArtistHit]:
|
||||
conn = self._cache_conn()
|
||||
if conn is None:
|
||||
return self._inner.search_artist(name)
|
||||
key = f"artist-search:{norm_key(name)}"
|
||||
rows = cached_api(conn, key, DAY,
|
||||
lambda: [_artist_hit_to_json(a) for a in self._inner.search_artist(name)])
|
||||
return [_artist_hit_from_json(d) for d in rows]
|
||||
|
||||
def browse_release_groups(self, artist_mbid: str) -> list[ReleaseGroupInfo]:
|
||||
conn = self._cache_conn()
|
||||
if conn is None:
|
||||
return self._inner.browse_release_groups(artist_mbid)
|
||||
key = f"artist-rgs:{artist_mbid}"
|
||||
rows = cached_api(conn, key, MONTH,
|
||||
lambda: [_rg_to_json(r) for r in self._inner.browse_release_groups(artist_mbid)])
|
||||
return [_rg_from_json(d) for d in rows]
|
||||
|
||||
|
||||
class MusicBrainzBrowser:
|
||||
"""Real MbBrowser via musicbrainzngs (imported lazily; not unit-tested offline)."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user