Recover stalled database connections and skip unreachable chat recipients
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
10
main.py
10
main.py
@@ -336,8 +336,9 @@ async def main():
|
||||
|
||||
# Запускаем polling
|
||||
try:
|
||||
from src.core.health import HEARTBEAT, heartbeat
|
||||
from src.core.health import HEARTBEAT, heartbeat, start_watchdog
|
||||
HEARTBEAT.unlink(missing_ok=True)
|
||||
watchdog = start_watchdog()
|
||||
if REDIS_URL:
|
||||
await storage.redis.ping()
|
||||
from sqlalchemy import select
|
||||
@@ -345,12 +346,15 @@ async def main():
|
||||
async with async_session_maker() as session:
|
||||
await session.execute(select(User).limit(0))
|
||||
await bot.get_me()
|
||||
health_task = asyncio.create_task(heartbeat())
|
||||
health_task = asyncio.create_task(heartbeat(), name="database-heartbeat")
|
||||
logger.info("Бот запущен")
|
||||
polling_task = asyncio.create_task(dp.start_polling(bot))
|
||||
completed, _ = await asyncio.wait({health_task, polling_task}, return_when=asyncio.FIRST_COMPLETED)
|
||||
for task in completed:
|
||||
await task
|
||||
except Exception:
|
||||
logger.exception("Runtime supervision failed; stopping the application")
|
||||
raise
|
||||
finally:
|
||||
from src.core.database import close_db
|
||||
from contextlib import suppress
|
||||
@@ -366,6 +370,8 @@ async def main():
|
||||
await bot.session.close()
|
||||
await storage.close()
|
||||
await close_db()
|
||||
if "watchdog" in locals():
|
||||
watchdog.set()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user