fix(watcher): resume the bot backlog from a saved cursor instead of restarting it

This commit is contained in:
hh committed 2026-09-19 16:18:40 +00:00
1 parent 59d3f214e8
commit 468f72ed11
4 files changed
+156 -32

No files matched your search

+27 -5
View File
@@ -22,11 +22,13 @@ from utils.read import accounts, bots
from utils.storage import ContentAddressedStorage
from watcher import tokens
from watcher.context import build_bot_capture_context
from watcher.drain import drain_backlog
from watcher.drain import DrainCursor, drain_backlog, first_cursor
DEVICE_MODEL = "Beavergram Bot"
TOKEN_MISSING = "Токен не найден на диске" # noqa: S105
INVALID_TOKEN_ERRORS = ("ACCESS_TOKEN_INVALID", "ACCESS_TOKEN_EXPIRED")
BACKLOG_ATTEMPTS = 3
BACKLOG_RETRY_PAUSE = 60
@dataclass
@@ -184,10 +186,30 @@ class BotRegistry:
)
async def _backlog(self, client: PyroClient, bot_id: int, account_id: int) -> None:
stored = await drain_backlog(client, account_id)
if stored is None:
return
await bots.mark_backfilled(self._pool, bot_id)
async def save(at: DrainCursor) -> None:
await bots.save_backlog_cursor(self._pool, bot_id, at.pts, at.qts, at.date)
saved = await bots.backlog_cursor(self._pool, bot_id)
cursor = DrainCursor(*saved) if saved else first_cursor()
stored = 0
for attempt in range(BACKLOG_ATTEMPTS):
if attempt:
await asyncio.sleep(BACKLOG_RETRY_PAUSE)
walk = await drain_backlog(client, account_id, cursor, save)
stored += walk.stored
cursor = walk.cursor
if walk.done:
await bots.mark_backfilled(self._pool, bot_id)
logger.info(
f"[green]Account {account_id}: backlog complete,[/] "
f"{stored} messages stored"
)
break
else:
logger.warning(
f"[yellow]Account {account_id}: backlog unfinished at "
f"pts={cursor.pts},[/] {stored} messages stored, resumes on restart"
)
if stored:
await accounts.notify_accounts_changed(self._pool)