DRRK/app/services/request_log.py

29 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
import logging
from app.db.base import SessionLocal
from app.integrations.gateway import GatewayClient
from app.repositories.request_log import RequestLogRepository
logger = logging.getLogger(__name__)
async def save_request_log(gateway: GatewayClient, user: str) -> None:
"""Сохраняет накопленные клиентом записи в external_request_log.
Отдельная короткая транзакция: журнал не должен откатываться вместе с
неудачной записью котировки, а его сбой — ронять обновление.
"""
records = gateway.drain_records()
if not records:
return
try:
async with SessionLocal() as session:
await RequestLogRepository(session).add_many(records, user)
await session.commit()
except Exception: # noqa: BLE001
logger.exception(
"Не удалось записать %d записей журнала запросов", len(records)
)