Add background catalog cache refresh to health check loop
Resolves #1 — the health checker now refreshes catalog cache for all online Evotor connections when cache is missing or older than CATALOG_REFRESH_INTERVAL_SECONDS (default: 3600s). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ class Settings(BaseSettings):
|
||||
EVOTOR_WEBHOOK_SECRET: str = ""
|
||||
|
||||
HEALTH_CHECK_INTERVAL_SECONDS: int = 600
|
||||
CATALOG_REFRESH_INTERVAL_SECONDS: int = 3600
|
||||
|
||||
VK_CLIENT_ID: str = ""
|
||||
VK_CLIENT_SECRET: str = ""
|
||||
|
||||
@@ -5,7 +5,7 @@ from datetime import datetime, timedelta
|
||||
import httpx
|
||||
|
||||
from web.database import SessionLocal
|
||||
from web.models import EvotorConnection, VkConnection
|
||||
from web.models import EvotorConnection, VkConnection, CachedStore
|
||||
|
||||
logger = logging.getLogger("uvicorn.error")
|
||||
|
||||
@@ -116,10 +116,28 @@ async def run_health_checks() -> None:
|
||||
conn.last_checked_at = now
|
||||
|
||||
db.commit()
|
||||
|
||||
# Refresh catalog cache for online Evotor connections
|
||||
from web.config import settings
|
||||
refreshed_catalog = 0
|
||||
for conn in evotor_connections:
|
||||
if not conn.is_online:
|
||||
continue
|
||||
cached = db.query(CachedStore).filter(CachedStore.user_id == conn.user_id).first()
|
||||
cache_age = (now - cached.fetched_at).total_seconds() if cached else None
|
||||
if cached is None or cache_age >= settings.CATALOG_REFRESH_INTERVAL_SECONDS:
|
||||
try:
|
||||
from web.evotor_api import refresh_catalog_cache
|
||||
await refresh_catalog_cache(conn.user_id, conn.access_token, db)
|
||||
refreshed_catalog += 1
|
||||
except Exception:
|
||||
logger.exception("Failed to refresh catalog cache for user_id=%d", conn.user_id)
|
||||
|
||||
logger.info(
|
||||
"Health checks completed: %d Evotor, %d VK",
|
||||
"Health checks completed: %d Evotor, %d VK, %d catalogs refreshed",
|
||||
len(evotor_connections),
|
||||
len(vk_connections),
|
||||
refreshed_catalog,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Error during health checks")
|
||||
|
||||
Reference in New Issue
Block a user