fix(api): update

This commit is contained in:
2025-12-29 00:46:07 +03:00
parent 734c65253d
commit f8edaa0aaf
3 changed files with 83 additions and 22 deletions

View File

@@ -3,24 +3,26 @@ from __future__ import annotations
import asyncio
from contextlib import suppress
from datetime import timezone
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.session import SessionLocal
from app.repositories.audio_repository import AudioRepository
from app.ws.manager import manager
from app.ws.router import manager # используем тот же manager, что и в ws/router.py
def _iso_z(dt) -> str:
# dt ожидается timezone-aware
return dt.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
async def audio_live_broadcaster(poll_interval_sec: float = 0.2) -> None:
async def audio_live_broadcaster(poll_interval_sec: float = 0.05) -> None:
"""
Poll latest row and broadcast only when a NEW row appears.
Throttling per client is handled by manager.broadcast_json().
"""
last_time = None
while True:
try:
async with SessionLocal() as db: # AsyncSession
async with SessionLocal() as db:
repo = AudioRepository(db)
rows = await repo.latest(1)
if rows:
@@ -35,7 +37,7 @@ async def audio_live_broadcaster(poll_interval_sec: float = 0.2) -> None:
}
)
except Exception:
# чтобы WS не умирал из-за временных проблем с БД
# не даём таске умереть при временных проблемах БД
pass
await asyncio.sleep(poll_interval_sec)