Back off a failed Jolpica ask; render posters off the event loop
Review round 1 (two findings): - While Jolpica failed, every schedule read asked it again, up to four times per poster request, and the poster, thumb and grand-prix routes were async, so the pacing, a 429's pause and a timeout stalled every request. A failed ask is now remembered for ten minutes (the kept schedule, or none, is answered meanwhile), a schedule in memory is answered without waiting for an ask under way, and those routes are plain defs in the threadpool. A rendered picture is stored whole (written aside, then renamed), as two may now render it side by side. - Bytecode was tracked: tests/__pycache__ (added with the tests) and models/__pycache__ that the cleanup missed. Untracked; .gitignore already names __pycache__/. From the review's notes: a poster whose map or photo failed is sent with a five-minute cache instead of a year's; uvicorn trusts the proxy's forwarded headers, so /examples redirects to https, not http; stale comments naming CARTO and the zoom lookup's key are corrected. Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
This commit is contained in:
+26
-24
@@ -95,38 +95,44 @@ app.mount(
|
||||
)
|
||||
|
||||
|
||||
# The poster routes are plain defs, not async: a render fetches map tiles and a schedule and draws for
|
||||
# seconds, so it runs in the threadpool instead of holding up every other request on the event loop.
|
||||
|
||||
def poster_response(png: bytes, kept: bool) -> FastAPIResponse:
|
||||
"""A poster the service stored is kept by clients for a year; one whose map or photo failed only
|
||||
for five minutes, so they ask again once it can be rendered whole."""
|
||||
return FastAPIResponse(
|
||||
content=png,
|
||||
media_type="image/png",
|
||||
headers={"Cache-Control": "public, max-age=31536000" if kept else "public, max-age=300"},
|
||||
)
|
||||
|
||||
|
||||
@app.get("/posters/race/{grand_prix_name}/{season}.png")
|
||||
async def get_race_poster(grand_prix_name: str, season: int, bg: BackgroundStyle = BackgroundStyle.AERIAL):
|
||||
def get_race_poster(grand_prix_name: str, season: int, bg: BackgroundStyle = BackgroundStyle.AERIAL):
|
||||
"""Plex-ready 1000x1500 race poster (PNG). Cached on disk per (race, bg).
|
||||
|
||||
`?bg=` selects the backdrop:
|
||||
- aerial (default) — Esri World Imagery satellite at the circuit's coords
|
||||
- osm — OpenStreetMap Carto street-map at the circuit's coords
|
||||
- dark — Esri Dark Gray Canvas
|
||||
- light — Esri Light Gray Canvas
|
||||
- none — plain black background (skip tile fetch)
|
||||
"""
|
||||
png = posters_service.render_race_poster(grand_prix_name, season, bg=bg)
|
||||
if not png:
|
||||
rendered = posters_service.render_race_poster(grand_prix_name, season, bg=bg)
|
||||
if not rendered:
|
||||
raise HTTPException(status_code=404, detail=f"No poster available for {grand_prix_name} {season}")
|
||||
return FastAPIResponse(
|
||||
content=png,
|
||||
media_type="image/png",
|
||||
headers={"Cache-Control": "public, max-age=31536000"},
|
||||
)
|
||||
return poster_response(*rendered)
|
||||
|
||||
|
||||
@app.get("/posters/season/{season}.png")
|
||||
async def get_season_poster(season: int):
|
||||
def get_season_poster(season: int):
|
||||
"""Plex-ready 1000x1500 season poster (PNG). Cached on disk."""
|
||||
png = posters_service.render_season_poster(season)
|
||||
return FastAPIResponse(
|
||||
content=png,
|
||||
media_type="image/png",
|
||||
headers={"Cache-Control": "public, max-age=31536000"},
|
||||
)
|
||||
return poster_response(*posters_service.render_season_poster(season))
|
||||
|
||||
|
||||
@app.get("/posters/session/{grand_prix_name}/{season}/{session_slug}.png")
|
||||
async def get_session_thumb(grand_prix_name: str, season: int, session_slug: str):
|
||||
def get_session_thumb(grand_prix_name: str, season: int, session_slug: str):
|
||||
"""Plex episode-thumb (1920x1080) for a specific session of a race weekend.
|
||||
|
||||
`session_slug` is one of: fp1, fp2, fp3, pre-qualifying, qualifying,
|
||||
@@ -134,14 +140,10 @@ async def get_session_thumb(grand_prix_name: str, season: int, session_slug: str
|
||||
sprint, post-sprint, pre-sprint-qualifying, sprint-qualifying,
|
||||
post-sprint-qualifying, bonus.
|
||||
"""
|
||||
png = posters_service.render_session_thumb(grand_prix_name, season, session_slug)
|
||||
if not png:
|
||||
rendered = posters_service.render_session_thumb(grand_prix_name, season, session_slug)
|
||||
if not rendered:
|
||||
raise HTTPException(status_code=404, detail=f"No session thumb for {grand_prix_name} {season} {session_slug}")
|
||||
return FastAPIResponse(
|
||||
content=png,
|
||||
media_type="image/png",
|
||||
headers={"Cache-Control": "public, max-age=31536000"},
|
||||
)
|
||||
return poster_response(*rendered)
|
||||
|
||||
|
||||
@app.get("/kometa/formula-1-metadata.yml")
|
||||
@@ -241,7 +243,7 @@ async def get_track_by_layout(
|
||||
return get_file_response(circuit_service.get_layout_details(country_slug, city_slug, circuit_slug, layout_slug), image_format, response)
|
||||
|
||||
@app.get("/grand-prix/{grand_prix_name}/{season}/{image_format}")
|
||||
async def get_grand_prix_circuit(grand_prix_name: str, season: int, image_format: Format, response: Response):
|
||||
def get_grand_prix_circuit(grand_prix_name: str, season: int, image_format: Format, response: Response):
|
||||
layout = circuit_service.get_circuit_layout_by_ergast_data(grand_prix_name, season)
|
||||
|
||||
if not layout:
|
||||
|
||||
Reference in New Issue
Block a user