fix(api): update
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user