Stop album art from blocking page loads
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m39s
CI / test (push) Successful in 3m38s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m39s
CI / test (push) Successful in 3m38s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
Root cause: gunicorn runs only 2 sync workers, and GET /api/v1/art/album did its MusicBrainz + Cover Art Archive work synchronously inside the request (2-12+ seconds per call, measured). A page with several uncached albums could tie up the entire application - every endpoint, every user - for as long as that took. Separately, even an already-resolved album re-streamed its image bytes live from Cover Art Archive on every single page view, by the original v1 design. The endpoint is now cache-only: it reads the album_art table and, if resolved, a local file, and never touches the network. A cache miss records a "pending" row and returns 404 immediately - the frontend already treats 404 as "show the placeholder," so no frontend changes are needed. A new background worker task (the same claim-with- FOR-UPDATE-SKIP-LOCKED pattern already used by imports and federation deliveries) does the actual resolving and downloads the image bytes to a shared volume, completely decoupled from any request. No migration needed: AlbumArt.status just gains a new "pending" value. Verified against the real docker compose stack (not just the test suite): cold lookups for 5 real albums all returned 404 in well under 0.2s (previously 2-12+s each), the worker resolved all 5 within ~10s in the background, and resolved art then served in single-digit milliseconds from the volume, including after a full container restart (no re-download). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Hn2QwS1HRT8tFAzo8ew63
This commit is contained in:
1 parent
79193fbc8e
commit
83b27d7ba1
11 files changed
+345
-68
No files matched your search
+157
-20
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import pathlib
|
||||
import threading
|
||||
import urllib.parse
|
||||
from datetime import UTC, datetime, timedelta
|
||||
@@ -14,7 +15,8 @@ from scrobbler.services import art
|
||||
_RELEASE_GROUPS = {
|
||||
"In Rainbows": "rg-found", # has art
|
||||
"No Art Album": "rg-no-art", # MB match, but Cover Art Archive has nothing
|
||||
"Boom Album": "rg-boom", # MB match, but Cover Art Archive 500s
|
||||
"Boom Album": "rg-boom", # MB match, but Cover Art Archive 500s on HEAD
|
||||
"Download Fail Album": "rg-dl-fail", # HEAD succeeds, but the GET download 500s
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +46,13 @@ def _fake_musicbrainz_caa(environ, start_response):
|
||||
start_response("500 Internal Server Error", [("Content-Type", "text/plain")])
|
||||
return [b"boom"]
|
||||
|
||||
if path == "/release-group/rg-dl-fail/front-500":
|
||||
if method == "HEAD":
|
||||
start_response("200 OK", [("Content-Type", "image/jpeg")])
|
||||
return []
|
||||
start_response("500 Internal Server Error", [("Content-Type", "text/plain")])
|
||||
return [b"boom"]
|
||||
|
||||
start_response("404 Not Found", [("Content-Type", "text/plain")])
|
||||
return [b"not found"]
|
||||
|
||||
@@ -58,12 +67,13 @@ def fake_art_api():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def art_config(app, fake_art_api):
|
||||
def art_config(app, fake_art_api, tmp_path):
|
||||
keys = (
|
||||
"MUSICBRAINZ_API_URL",
|
||||
"COVERART_API_URL",
|
||||
"MUSICBRAINZ_REQUEST_INTERVAL",
|
||||
"MUSICBRAINZ_RETRIES",
|
||||
"ART_STORAGE_DIR",
|
||||
)
|
||||
saved = {k: app.config[k] for k in keys}
|
||||
app.config.update(
|
||||
@@ -71,6 +81,7 @@ def art_config(app, fake_art_api):
|
||||
COVERART_API_URL=fake_art_api,
|
||||
MUSICBRAINZ_REQUEST_INTERVAL=0,
|
||||
MUSICBRAINZ_RETRIES=0,
|
||||
ART_STORAGE_DIR=str(tmp_path),
|
||||
)
|
||||
yield
|
||||
app.config.update(saved)
|
||||
@@ -98,7 +109,7 @@ def insert(artist, album, status, days_ago=0.0, image_url=None):
|
||||
db.session.commit()
|
||||
|
||||
|
||||
# --- lookup() ------------------------------------------------------------------------------
|
||||
# --- lookup() (still live-resolving: used by ActivityPub posting, out of scope here) -----------
|
||||
|
||||
|
||||
def test_lookup_finds_art(art_config):
|
||||
@@ -142,9 +153,6 @@ def test_lookup_is_case_insensitive_and_cached(app, art_config):
|
||||
|
||||
def test_lookup_fresh_cache_hit_skips_the_network(app, art_config, fake_art_api):
|
||||
insert("Fresh Artist", "Fresh Album", "not_found", days_ago=0)
|
||||
# If this weren't a cache hit, the (nonexistent) search term would still return "not_found"
|
||||
# from the live fake server too — so instead prove no network call happened by pointing at
|
||||
# a port nothing listens on and confirming the result is still served from the cache.
|
||||
app.config.update(
|
||||
MUSICBRAINZ_API_URL="http://127.0.0.1:1/", COVERART_API_URL="http://127.0.0.1:1/"
|
||||
)
|
||||
@@ -187,7 +195,7 @@ def test_found_row_is_never_rechecked(app, art_config):
|
||||
assert result.image_url == "https://example.com/old.jpg"
|
||||
|
||||
|
||||
# --- fetch_image() ---------------------------------------------------------------------------
|
||||
# --- fetch_image() -------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_fetch_image_streams_the_bytes(art_config):
|
||||
@@ -202,14 +210,146 @@ def test_fetch_image_raises_on_failure(art_config):
|
||||
art.fetch_image("http://127.0.0.1:1/nope.jpg")
|
||||
|
||||
|
||||
# --- GET /api/v1/art/album ---------------------------------------------------------------------
|
||||
# --- get_cached() / want() / requeue() / read_file() ----------------------------------------
|
||||
|
||||
|
||||
def test_get_cached_returns_none_for_unseen_album(art_config):
|
||||
assert art.get_cached("Nobody", "Nothing") is None
|
||||
|
||||
|
||||
def test_get_cached_is_case_insensitive(art_config):
|
||||
insert("Radiohead", "In Rainbows", "found", image_url="https://example.com/a.jpg")
|
||||
assert art.get_cached("radiohead", "in rainbows").image_url == "https://example.com/a.jpg"
|
||||
|
||||
|
||||
def test_want_creates_a_pending_row(art_config):
|
||||
art.want("New Artist", "New Album")
|
||||
assert row("New Artist", "New Album").status == "pending"
|
||||
|
||||
|
||||
def test_want_is_a_noop_if_a_row_already_exists(art_config):
|
||||
insert("Radiohead", "In Rainbows", "found", image_url="https://example.com/a.jpg")
|
||||
art.want("Radiohead", "In Rainbows")
|
||||
assert row("Radiohead", "In Rainbows").status == "found" # untouched, not reset to pending
|
||||
|
||||
|
||||
def test_read_file_returns_none_when_not_downloaded(art_config):
|
||||
assert art.read_file(999_999) is None
|
||||
|
||||
|
||||
def test_read_file_returns_the_bytes(app, art_config):
|
||||
path = pathlib.Path(app.config["ART_STORAGE_DIR"]) / "123.jpg"
|
||||
path.write_bytes(b"hello")
|
||||
assert art.read_file(123) == b"hello"
|
||||
|
||||
|
||||
def test_requeue_marks_a_row_pending(art_config):
|
||||
insert("Someone", "Old Album", "found", image_url="https://example.com/a.jpg")
|
||||
art.requeue(row("Someone", "Old Album"))
|
||||
assert row("Someone", "Old Album").status == "pending"
|
||||
|
||||
|
||||
# --- resolve_one_pending() (the worker task) ------------------------------------------------
|
||||
|
||||
|
||||
def test_resolve_one_pending_resolves_and_downloads(art_config):
|
||||
art.want("Radiohead", "In Rainbows")
|
||||
assert art.resolve_one_pending() is True
|
||||
cached = row("Radiohead", "In Rainbows")
|
||||
assert cached.status == "found"
|
||||
assert art.read_file(cached.id) == b"\xff\xd8\xff\xe0fake-jpeg-bytes"
|
||||
|
||||
|
||||
def test_resolve_one_pending_handles_not_found(art_config):
|
||||
art.want("Nobody", "Nothing")
|
||||
assert art.resolve_one_pending() is True
|
||||
assert row("Nobody", "Nothing").status == "not_found"
|
||||
|
||||
|
||||
def test_resolve_one_pending_handles_upstream_error(art_config):
|
||||
art.want("Someone", "Boom Album")
|
||||
assert art.resolve_one_pending() is True
|
||||
cached = row("Someone", "Boom Album")
|
||||
assert cached.status == "error"
|
||||
assert cached.error_code
|
||||
|
||||
|
||||
def test_resolve_one_pending_download_failure_is_cached_as_error(art_config):
|
||||
art.want("Someone", "Download Fail Album")
|
||||
assert art.resolve_one_pending() is True
|
||||
cached = row("Someone", "Download Fail Album")
|
||||
assert cached.status == "error" # not left "found" with no file on disk
|
||||
assert art.read_file(cached.id) is None
|
||||
|
||||
|
||||
def test_resolve_one_pending_returns_false_when_nothing_to_do(art_config):
|
||||
assert art.resolve_one_pending() is False
|
||||
|
||||
|
||||
def test_resolve_one_pending_picks_up_stale_not_found(art_config):
|
||||
insert("Radiohead", "In Rainbows", "not_found", days_ago=31)
|
||||
assert art.resolve_one_pending() is True
|
||||
assert row("Radiohead", "In Rainbows").status == "found"
|
||||
|
||||
|
||||
def test_resolve_one_pending_picks_up_stale_error(art_config):
|
||||
db.session.add(
|
||||
AlbumArt(
|
||||
artist="Radiohead",
|
||||
album="In Rainbows",
|
||||
status="error",
|
||||
error_code="TimeoutError",
|
||||
checked_at=datetime.now(UTC) - timedelta(hours=2),
|
||||
)
|
||||
)
|
||||
db.session.commit()
|
||||
assert art.resolve_one_pending() is True
|
||||
assert row("Radiohead", "In Rainbows").status == "found"
|
||||
|
||||
|
||||
def test_resolve_one_pending_ignores_fresh_not_found(art_config):
|
||||
insert("Radiohead", "In Rainbows", "not_found", days_ago=1)
|
||||
assert art.resolve_one_pending() is False
|
||||
|
||||
|
||||
# --- GET /api/v1/art/album -------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_album_art_requires_auth(client):
|
||||
assert client.get("/api/v1/art/album?artist=A&album=B").status_code == 401
|
||||
|
||||
|
||||
def test_album_art_returns_the_image(client, auth, art_config):
|
||||
def test_album_art_requires_artist_and_album(client, auth):
|
||||
assert client.get("/api/v1/art/album?artist=A", headers=auth).status_code == 422
|
||||
assert client.get("/api/v1/art/album?album=B", headers=auth).status_code == 422
|
||||
|
||||
|
||||
def test_album_art_404s_and_queues_an_unseen_album(client, auth, art_config):
|
||||
response = client.get("/api/v1/art/album?artist=New+Artist&album=New+Album", headers=auth)
|
||||
assert response.status_code == 404
|
||||
assert response.get_json()["error"]["code"] == "no_album_art"
|
||||
assert row("New Artist", "New Album").status == "pending"
|
||||
|
||||
|
||||
def test_album_art_never_calls_the_network(client, auth, app, art_config):
|
||||
# Point at a port nothing listens on: the endpoint must still respond (fast, 404)
|
||||
# without ever trying to reach it — proof it's a cache-only read.
|
||||
app.config.update(
|
||||
MUSICBRAINZ_API_URL="http://127.0.0.1:1/", COVERART_API_URL="http://127.0.0.1:1/"
|
||||
)
|
||||
response = client.get("/api/v1/art/album?artist=Another&album=Album", headers=auth)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_album_art_404s_while_pending(client, auth, art_config):
|
||||
art.want("Radiohead", "In Rainbows")
|
||||
response = client.get("/api/v1/art/album?artist=Radiohead&album=In+Rainbows", headers=auth)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_album_art_returns_the_image_once_resolved(client, auth, art_config):
|
||||
art.want("Radiohead", "In Rainbows")
|
||||
art.resolve_one_pending()
|
||||
response = client.get("/api/v1/art/album?artist=Radiohead&album=In+Rainbows", headers=auth)
|
||||
assert response.status_code == 200
|
||||
assert response.data == b"\xff\xd8\xff\xe0fake-jpeg-bytes"
|
||||
@@ -217,18 +357,15 @@ def test_album_art_returns_the_image(client, auth, art_config):
|
||||
assert response.headers["Cache-Control"] == "private, max-age=604800"
|
||||
|
||||
|
||||
def test_album_art_404s_when_not_found(client, auth, art_config):
|
||||
def test_album_art_404s_when_resolved_not_found(client, auth, art_config):
|
||||
art.want("Nobody", "Nothing")
|
||||
art.resolve_one_pending()
|
||||
response = client.get("/api/v1/art/album?artist=Nobody&album=Nothing", headers=auth)
|
||||
assert response.status_code == 404
|
||||
assert response.get_json()["error"]["code"] == "no_album_art"
|
||||
|
||||
|
||||
def test_album_art_502s_on_upstream_error(client, auth, art_config):
|
||||
response = client.get("/api/v1/art/album?artist=Someone&album=Boom+Album", headers=auth)
|
||||
assert response.status_code == 502
|
||||
assert response.get_json()["error"]["code"] == "art_upstream_unavailable"
|
||||
|
||||
|
||||
def test_album_art_requires_artist_and_album(client, auth):
|
||||
assert client.get("/api/v1/art/album?artist=A", headers=auth).status_code == 422
|
||||
assert client.get("/api/v1/art/album?album=B", headers=auth).status_code == 422
|
||||
def test_album_art_requeues_a_found_row_with_a_missing_file(client, auth, art_config):
|
||||
insert("Legacy", "Album", "found", image_url="https://example.com/old.jpg")
|
||||
response = client.get("/api/v1/art/album?artist=Legacy&album=Album", headers=auth)
|
||||
assert response.status_code == 404
|
||||
assert row("Legacy", "Album").status == "pending" # requeued for the worker to redo
|
||||
Reference in new issue
Block a user