ops: Xray in the installer (mihomo fails REALITY against Xray servers), watchdog rebuilds a poisoned Telegram app

This commit is contained in:
maksarsanjeev
2026-11-09 00:53:46 +03:00
parent a4f5a89026
commit b8541465ec
4 changed files with 174 additions and 86 deletions
+36 -23
View File
@@ -437,37 +437,50 @@ async def main():
log.warning("TELEGRAM_TOKEN not set: API only")
await asyncio.Event().wait()
proxy = CFG["proxy"] or None
builder = (Application.builder().token(CFG["token"])
.request(HTTPXRequest(proxy=proxy, connect_timeout=10, read_timeout=20, write_timeout=20,
pool_timeout=10, connection_pool_size=8))
.get_updates_request(PollRequest(proxy=proxy, connect_timeout=10, read_timeout=40,
write_timeout=10, pool_timeout=10)))
tg = builder.build()
async def on_error(update, ctx):
log.error("handler failed", exc_info=ctx.error)
tg.add_error_handler(on_error)
tg.add_handler(CommandHandler("start", cmd_start))
tg.add_handler(CommandHandler("today", cmd_today))
tg.add_handler(CommandHandler("tasks", cmd_tasks))
tg.add_handler(CommandHandler("subs", cmd_subs))
tg.add_handler(CommandHandler("routines", cmd_routines))
tg.add_handler(CommandHandler("stop", cmd_stop))
tg.add_handler(CallbackQueryHandler(on_button))
tg.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, on_voice))
tg.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, on_text))
# A Telegram/proxy outage must not kill the API, calendar and reminders. Keep the
# process alive and retry Telegram; systemd restart is still reserved for the polling
# watchdog, which catches a connection that looked established but stopped updating.
def build_tg() -> Application:
builder = (Application.builder().token(CFG["token"])
.request(HTTPXRequest(proxy=proxy, connect_timeout=10, read_timeout=20, write_timeout=20,
pool_timeout=10, connection_pool_size=8))
.get_updates_request(PollRequest(proxy=proxy, connect_timeout=10, read_timeout=40,
write_timeout=10, pool_timeout=10)))
tg = builder.build()
async def on_error(update, ctx):
log.error("handler failed", exc_info=ctx.error)
tg.add_error_handler(on_error)
tg.add_handler(CommandHandler("start", cmd_start))
tg.add_handler(CommandHandler("today", cmd_today))
tg.add_handler(CommandHandler("tasks", cmd_tasks))
tg.add_handler(CommandHandler("subs", cmd_subs))
tg.add_handler(CommandHandler("routines", cmd_routines))
tg.add_handler(CommandHandler("stop", cmd_stop))
tg.add_handler(CallbackQueryHandler(on_button))
tg.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, on_voice))
tg.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, on_text))
return tg
# A Telegram/proxy outage must not kill the API, calendar and reminders. Every retry
# builds a FRESH Application: 06.11.2026 a proxy hiccup poisoned the shared httpx pool
# (PoolTimeout on every get_me) and reusing the same tg object looped forever — the API
# answered, the bot stayed dead for 2.5 days. If a fresh app still can't start 10 times
# (~5 minutes), exit and let systemd bring up a clean process.
fails = 0
while True:
try:
async with tg:
async with build_tg() as tg:
await tg.start()
await tg.updater.start_polling(drop_pending_updates=False, timeout=25)
fails = 0
asyncio.create_task(reminder_loop(tg.bot))
asyncio.create_task(poll_watchdog())
await asyncio.Event().wait()
except Exception:
log.exception("Telegram unavailable; API remains active, retrying in 30 s")
fails += 1
log.exception("Telegram unavailable (attempt %d); API remains active, retrying in 30 s", fails)
if fails >= 10:
log.error("Telegram still broken after %d fresh attempts — restarting", fails)
os._exit(1)
await asyncio.sleep(30)