Invalidate movie/series/person detail cache on re-ingestion
CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s

cached_response() had no write-side invalidation, so re-ingesting an
entity kept serving its pre-ingestion cached response for up to
CACHE_DEFAULT_TTL. Found live: re-ingesting a movie to test the new
company-logo field still showed logo_path: None because the sliding
5-minute cache from earlier testing never expired.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
NixReaper
2026-09-05 07:56:00 -05:00
co-authored by Claude Sonnet 5
parent c03a75989d
commit 2138d4afc3
3 changed files with 146 additions and 9 deletions
+19 -1
View File
@@ -14,7 +14,9 @@ from arq import Retry
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import session_scope
from app.core.ids import aurora_id
from app.core.logging import get_logger
from app.core.redis import get_redis_pool
from app.models.enums import IngestionJobStatus
from app.models.provider import IngestionJob
from app.providers.base import MetadataProvider
@@ -30,6 +32,7 @@ from app.repositories.provider_repository import (
mark_job_succeeded,
)
from app.repositories.tv_repository import get_seasons_for_series, get_series_companies
from app.services.cache import build_key, cache_delete
from app.services.ingestion.movie_ingestion import ingest_movie
from app.services.ingestion.person_ingestion import ingest_person
from app.services.ingestion.series_ingestion import ingest_season, ingest_series
@@ -76,10 +79,17 @@ async def _run_ingestion(
job_id: uuid.UUID,
label: str,
work: Callable[[AsyncSession, MetadataProvider], Awaitable[uuid.UUID]],
cache_entity_type: str | None = None,
) -> uuid.UUID:
"""Shared job-lifecycle wrapper: mark running, run `work` in its own
session/transaction, mark succeeded/failed. `work` does the actual
fetch+normalize+upsert and returns the resulting Aurora entity id."""
fetch+normalize+upsert and returns the resulting Aurora entity id.
`cache_entity_type`, when given, invalidates that entity's cached
`GET .../{aurora_id}` response on success -- `cached_response`
(`app/api/deps.py`) has no invalidate-on-write wiring otherwise, so a
re-ingested movie/series/person would keep serving its pre-ingestion
snapshot for up to `CACHE_DEFAULT_TTL`."""
async with session_scope() as session:
job = await _require_job(session, job_id)
@@ -92,6 +102,11 @@ async def _run_ingestion(
async with session_scope() as session:
job = await _require_job(session, job_id)
await mark_job_succeeded(session, job, result_id)
if cache_entity_type is not None:
await cache_delete(
get_redis_pool(),
build_key(cache_entity_type, aurora_id(cache_entity_type, str(result_id))),
)
return result_id
except Exception as exc:
logger.error(f"{label}_failed", job_id=str(job_id), error=str(exc))
@@ -123,6 +138,7 @@ async def run_movie_ingestion(job_id: uuid.UUID, external_id: str) -> uuid.UUID:
lambda session, provider: ingest_movie(
session, provider, external_id, ingestion_job_id=job_id
),
cache_entity_type="movie",
)
@@ -133,6 +149,7 @@ async def run_person_ingestion(job_id: uuid.UUID, external_id: str) -> uuid.UUID
lambda session, provider: ingest_person(
session, provider, external_id, ingestion_job_id=job_id
),
cache_entity_type="person",
)
@@ -143,6 +160,7 @@ async def run_series_ingestion(job_id: uuid.UUID, external_id: str) -> uuid.UUID
lambda session, provider: ingest_series(
session, provider, external_id, ingestion_job_id=job_id
),
cache_entity_type="series",
)