All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
The account_snapshot cache fed /networth, /networth/history, and
/scenarios/{id}/progress. No CronJob populated it, so the cache had
drifted ~18 days behind the wealthfolio_sync mirror (last refresh
2026-05-09 via manual kubectl exec; Grafana reads wf_sync directly
and stayed fresh).
Switch to lazy refresh on read: each request to those endpoints now
checks MAX(account_snapshot.snapshot_date) — if it's older than
NETWORTH_CACHE_TTL_DAYS (default 1), pull fresh rows from wf_sync via
read_account_snapshots_from_pg and upsert. Idempotent under
concurrency (existing ON CONFLICT DO UPDATE).
Plumbing:
- Add get_wf_sync_session dependency that yields None when the wf_sync
factory isn't wired (keeps existing tests' behaviour: no refresh
attempted, they continue to seed account_snapshot directly).
- Wire wf_sync engine + session_factory in app.lifespan when
WEALTHFOLIO_SYNC_DB_CONNECTION_STRING is set.
- Centralise the staleness check in refresh_account_snapshots_if_stale.
Tests:
- 271 existing tests still green.
- Three new tests in test_api_networth_refresh.py covering: empty cache
triggers refresh, stale cache triggers refresh, fresh cache skips
refresh (asserts the wf_sync value is NOT served).
37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
"""Shared FastAPI dependencies — DB session per request."""
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import AsyncIterator
|
|
|
|
from fastapi import Request
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
|
|
|
|
|
async def get_session(request: Request) -> AsyncIterator[AsyncSession]:
|
|
"""Yield an AsyncSession bound to the engine on app.state.
|
|
|
|
The engine + session factory are wired up in `app.lifespan`. Tests
|
|
swap them out via dependency_overrides.
|
|
"""
|
|
factory: async_sessionmaker[AsyncSession] = request.app.state.session_factory
|
|
async with factory() as session:
|
|
yield session
|
|
|
|
|
|
async def get_wf_sync_session(request: Request) -> AsyncIterator[AsyncSession | None]:
|
|
"""Yield an AsyncSession bound to the wealthfolio_sync mirror.
|
|
|
|
Powers the lazy refresh of `account_snapshot` (the disk cache for
|
|
/networth). Yields `None` when the factory is not wired so endpoints
|
|
can fall back to whatever is already cached — relevant for the in-memory
|
|
SQLite test runs that don't wire wf_sync. Tests that DO want to exercise
|
|
the refresh override this with their own factory via
|
|
`app.dependency_overrides`.
|
|
"""
|
|
factory: async_sessionmaker[AsyncSession] | None = getattr(
|
|
request.app.state, "wf_sync_session_factory", None)
|
|
if factory is None:
|
|
yield None
|
|
return
|
|
async with factory() as session:
|
|
yield session
|