fix(admin): avoid stale Telegram webhook alerts

Only surface Telegram delivery errors while updates are still pending, and register the webhook after the aiohttp webhook site starts listening.
This commit is contained in:
3252a8
2026-06-10 13:04:04 +03:00
parent ab21253d4f
commit 217bed3c5d
5 changed files with 64 additions and 8 deletions
+11 -1
View File
@@ -239,16 +239,26 @@ class TelegramAlertsTests(unittest.IsolatedAsyncioTestCase):
alerts = await health.telegram_alerts(bot, _settings())
self.assertEqual(_alert_ids(alerts), ["telegram_webhook_mismatch"])
async def test_recent_delivery_error_reported(self):
async def test_recent_delivery_error_with_pending_update_reported(self):
info = self._webhook_info(
last_error_date=datetime.now(timezone.utc),
last_error_message="SSL error",
pending_update_count=1,
)
bot = SimpleNamespace(get_webhook_info=AsyncMock(return_value=info))
alerts = await health.telegram_alerts(bot, _settings())
self.assertEqual(_alert_ids(alerts), ["telegram_webhook_error"])
self.assertEqual(alerts[0].params["error"], "SSL error")
async def test_recent_delivery_error_without_pending_update_not_reported(self):
info = self._webhook_info(
last_error_date=datetime.now(timezone.utc),
last_error_message="Connection refused",
pending_update_count=0,
)
bot = SimpleNamespace(get_webhook_info=AsyncMock(return_value=info))
self.assertEqual(await health.telegram_alerts(bot, _settings()), [])
async def test_stale_delivery_error_not_reported(self):
info = self._webhook_info(last_error_date=time.time() - 7200)
bot = SimpleNamespace(get_webhook_info=AsyncMock(return_value=info))
+23
View File
@@ -45,6 +45,29 @@ def test_worker_starts_backup_task_without_enabled_guard():
assert guarded_backup_tasks == []
def test_telegram_webhook_configuration_is_deferred_until_site_start():
main_source = Path("backend/bot/main_bot.py").read_text(encoding="utf-8")
web_source = Path("backend/bot/app/web/web_server.py").read_text(encoding="utf-8")
tree = ast.parse(main_source)
startup_node = next(
node
for node in ast.walk(tree)
if isinstance(node, ast.AsyncFunctionDef) and node.name == "on_startup_configured"
)
startup_set_webhook_calls = [
node
for node in ast.walk(startup_node)
if isinstance(node, ast.Attribute) and node.attr == "set_webhook"
]
assert startup_set_webhook_calls == []
assert "after_webhooks_started=_after_webhooks_started" in main_source
assert web_source.index("await site.start()") < web_source.index(
"await after_webhooks_started()"
)
def test_telegram_startup_network_error_retries_until_success_without_traceback(caplog):
calls = []