Stage 07.4.3.15 — Isolated debug runtime and debug auto screen
This commit is contained in:
170
app/src/trading/debug/runner.py
Normal file
170
app/src/trading/debug/runner.py
Normal file
@@ -0,0 +1,170 @@
|
||||
# app/src/trading/debug/runner.py
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Callable
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.exceptions import TelegramBadRequest, TelegramRetryAfter
|
||||
|
||||
from src.trading.debug.service import DebugTradeService
|
||||
|
||||
|
||||
class DebugTradeRunner:
|
||||
_task: asyncio.Task | None = None
|
||||
|
||||
_bot: Bot | None = None
|
||||
_chat_id: int | None = None
|
||||
_message_id: int | None = None
|
||||
_render_text: Callable[[], str] | None = None
|
||||
_render_markup: Callable[[], object] | None = None
|
||||
|
||||
_current_screen: str | None = None
|
||||
|
||||
_interval_seconds = 5
|
||||
_last_text: str | None = None
|
||||
_last_refresh_at: float = 0.0
|
||||
_retry_after_until: float = 0.0
|
||||
|
||||
@classmethod
|
||||
def register_screen(
|
||||
cls,
|
||||
*,
|
||||
bot: Bot,
|
||||
chat_id: int,
|
||||
message_id: int,
|
||||
render_text: Callable[[], str],
|
||||
render_markup: Callable[[], object],
|
||||
) -> None:
|
||||
cls._bot = bot
|
||||
cls._chat_id = chat_id
|
||||
cls._message_id = message_id
|
||||
cls._render_text = render_text
|
||||
cls._render_markup = render_markup
|
||||
cls._last_text = None
|
||||
|
||||
@classmethod
|
||||
async def delete_registered_screen(
|
||||
cls,
|
||||
*,
|
||||
bot: Bot,
|
||||
chat_id: int,
|
||||
) -> None:
|
||||
if cls._chat_id is None or cls._message_id is None:
|
||||
return
|
||||
|
||||
if cls._chat_id != chat_id:
|
||||
return
|
||||
|
||||
try:
|
||||
await bot.delete_message(
|
||||
chat_id=cls._chat_id,
|
||||
message_id=cls._message_id,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
cls._message_id = None
|
||||
cls._render_text = None
|
||||
cls._render_markup = None
|
||||
cls._last_text = None
|
||||
|
||||
@classmethod
|
||||
def set_current_screen(cls, screen: str) -> None:
|
||||
cls._current_screen = screen
|
||||
|
||||
@classmethod
|
||||
def start(cls) -> None:
|
||||
state = DebugTradeService().get_state()
|
||||
state.status = "RUNNING"
|
||||
|
||||
if cls._task is not None and not cls._task.done():
|
||||
return
|
||||
|
||||
cls._task = asyncio.create_task(cls._worker())
|
||||
|
||||
@classmethod
|
||||
def stop(cls) -> None:
|
||||
if cls._task is None:
|
||||
return
|
||||
|
||||
cls._task.cancel()
|
||||
cls._task = None
|
||||
|
||||
@classmethod
|
||||
async def _worker(cls) -> None:
|
||||
service = DebugTradeService()
|
||||
|
||||
while True:
|
||||
state = service.get_state()
|
||||
|
||||
if state.status == "OFF":
|
||||
cls._task = None
|
||||
break
|
||||
|
||||
service.process()
|
||||
await cls.refresh_screen(force=False)
|
||||
|
||||
await asyncio.sleep(cls._interval_seconds)
|
||||
|
||||
@classmethod
|
||||
async def refresh_screen(cls, *, force: bool = False) -> None:
|
||||
if cls._current_screen != "debug_auto":
|
||||
return
|
||||
|
||||
now = time.monotonic()
|
||||
|
||||
if now < cls._retry_after_until:
|
||||
return
|
||||
|
||||
if not force and now - cls._last_refresh_at < cls._interval_seconds:
|
||||
return
|
||||
|
||||
if not all(
|
||||
[
|
||||
cls._bot,
|
||||
cls._chat_id,
|
||||
cls._message_id,
|
||||
cls._render_text,
|
||||
cls._render_markup,
|
||||
]
|
||||
):
|
||||
return
|
||||
|
||||
text = cls._render_text()
|
||||
|
||||
if text == cls._last_text:
|
||||
return
|
||||
|
||||
try:
|
||||
await cls._bot.edit_message_text(
|
||||
chat_id=cls._chat_id,
|
||||
message_id=cls._message_id,
|
||||
text=text,
|
||||
reply_markup=cls._render_markup(),
|
||||
)
|
||||
cls._last_text = text
|
||||
cls._last_refresh_at = now
|
||||
|
||||
except TelegramRetryAfter as exc:
|
||||
cls._retry_after_until = time.monotonic() + exc.retry_after + 5
|
||||
|
||||
except TelegramBadRequest as exc:
|
||||
error_text = str(exc).lower()
|
||||
|
||||
if "message is not modified" in error_text:
|
||||
cls._last_text = text
|
||||
cls._last_refresh_at = now
|
||||
return
|
||||
|
||||
if "message to edit not found" in error_text:
|
||||
cls._message_id = None
|
||||
cls._render_text = None
|
||||
cls._render_markup = None
|
||||
cls._last_text = None
|
||||
return
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
Reference in New Issue
Block a user