Stage 07.4.3.2 — price polling, event bus and UI throttling

This commit is contained in:
2026-05-02 14:57:43 +03:00
parent 38c8686a9b
commit bd6b40fcb2
9 changed files with 644 additions and 31 deletions

View File

@@ -9,6 +9,8 @@ from typing import Callable
from aiogram import Bot
from aiogram.exceptions import TelegramBadRequest, TelegramRetryAfter
from src.core.event_bus import EventBus
from src.integrations.exchange.market_data_runner import MarketDataRunner
from src.trading.auto.service import AutoTradeService
@@ -20,9 +22,16 @@ class AutoTradeRunner:
_render_text: Callable[[], str] | None = None
_render_markup: Callable[[], object] | None = None
_current_screen: str | None = None
_interval_seconds = 15
# анализ стратегии — часто
_analysis_interval_seconds = 5
# Telegram UI — редко
_ui_interval_seconds = 60
_last_text: str | None = None
_last_ui_refresh_at: float = 0.0
_last_event_version: int = 0
_retry_after_until: float = 0.0
@classmethod
@@ -89,6 +98,13 @@ class AutoTradeRunner:
@classmethod
def start(cls) -> None:
service = AutoTradeService()
MarketDataRunner.start(
symbol_provider=lambda: service.get_state().symbol,
interval_seconds=1,
)
if cls._task is not None and not cls._task.done():
return
@@ -96,6 +112,8 @@ class AutoTradeRunner:
@classmethod
def stop(cls) -> None:
MarketDataRunner.stop()
if cls._task is None:
return
@@ -111,16 +129,29 @@ class AutoTradeRunner:
if state.status == "OFF":
cls._task = None
MarketDataRunner.stop()
break
service.run_cycle()
await cls._refresh_screen()
await asyncio.sleep(cls._interval_seconds)
current_event_version = EventBus.version()
has_important_event = current_event_version != cls._last_event_version
if has_important_event:
cls._last_event_version = current_event_version
await cls._refresh_screen(force=has_important_event)
await asyncio.sleep(cls._analysis_interval_seconds)
@classmethod
async def _refresh_screen(cls) -> None:
if time.monotonic() < cls._retry_after_until:
async def _refresh_screen(cls, *, force: bool = False) -> None:
now = time.monotonic()
if now < cls._retry_after_until:
return
if not force and now - cls._last_ui_refresh_at < cls._ui_interval_seconds:
return
if not all(
@@ -147,6 +178,7 @@ class AutoTradeRunner:
reply_markup=cls._render_markup(),
)
cls._last_text = text
cls._last_ui_refresh_at = now
except TelegramRetryAfter as exc:
cls._retry_after_until = time.monotonic() + exc.retry_after + 5
@@ -156,6 +188,7 @@ class AutoTradeRunner:
if "message is not modified" in error_text:
cls._last_text = text
cls._last_ui_refresh_at = now
return
if "message to edit not found" in error_text: