Keep Jolpica schedules, pace the requests, never keep a failure

A Kometa YAML request asks Jolpica for every season at once; Jolpica
(4 requests a second, 500 an hour) answered 429 for 24 seasons on
2026-11-27 09:28, and lru_cache kept each failure as an empty schedule,
so every poster and thumb of those seasons answered 404 until a restart.
The service also ran two Jolpica clients, so the race lookup and the
posters held different schedules.

Now one client serves the whole service; a season that is over is kept
on disk in the cache volume for good, one not yet over is asked again
after six hours; requests are paced under the limit, a 429 is asked
again after its Retry-After; a failure is never kept. The Kometa
endpoint runs in the threadpool, as its first fill takes minutes.

Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
This commit is contained in:
2026-11-27 09:46:56 +01:00
parent e976f1d8f5
commit 1e2c1b898f
4 changed files with 139 additions and 15 deletions
+10 -3
View File
@@ -6,8 +6,8 @@ import logging
# Import our CircuitService
from circuits_service import CircuitService, Format
from ergast_service import ErgastService
from posters_service import PostersService, BackgroundStyle
from access_log import log_request
from models.track_layout import TrackLayout
# Initialize FastAPI app
@@ -30,9 +30,13 @@ app.add_middleware(
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# One line per request (uvicorn's own access log is off: --no-access-log)
app.middleware("http")(log_request)
# Initialize CircuitService
circuit_service = CircuitService()
ergast_service = ErgastService(circuit_service)
# One Jolpica client for the whole service, so its schedules and its pacing are shared
ergast_service = circuit_service.ergast_service
posters_service = PostersService(circuit_service, ergast_service)
def get_content_disposition(format: Format) -> str:
@@ -129,7 +133,7 @@ async def get_session_thumb(grand_prix_name: str, season: int, session_slug: str
@app.get("/kometa/formula-1-metadata.yml")
async def get_kometa_metadata():
def get_kometa_metadata():
"""Generate a Kometa-compatible metadata YAML for the Formula 1 library.
One entry per F1 season from 1950..current+1. Each entry:
@@ -140,6 +144,9 @@ async def get_kometa_metadata():
Kometa harmlessly skips shows / seasons that don't exist in the Plex
library, so we can emit the full schedule and let Kometa do the matching.
A plain def, not async: the first call asks Jolpica for every season, paced under its rate
limit (about 25 s), and runs in the threadpool instead of holding up every other request.
"""
yaml = posters_service.render_kometa_metadata()
return PlainTextResponse(