import asyncio import logging from datetime import datetime import httpx from web.database import SessionLocal from web.models import EvotorConnection logger = logging.getLogger("uvicorn.error") EVOTOR_STORES_URL = "https://api.evotor.ru/stores" async def check_evotor_connection(access_token: str) -> bool: try: async with httpx.AsyncClient() as client: response = await client.get( EVOTOR_STORES_URL, headers={"Authorization": f"Bearer {access_token}"}, timeout=15, ) return response.status_code == 200 except Exception: return False async def run_health_checks() -> None: db = SessionLocal() try: connections = db.query(EvotorConnection).all() for conn in connections: is_online = await check_evotor_connection(conn.access_token) conn.is_online = is_online conn.last_checked_at = datetime.utcnow() db.commit() logger.info("Health checks completed for %d connection(s)", len(connections)) except Exception: logger.exception("Error during health checks") db.rollback() finally: db.close() async def health_check_loop(interval: int) -> None: while True: await run_health_checks() await asyncio.sleep(interval)