diff --git a/apps/tgbot/tgbot/bot.py b/apps/tgbot/tgbot/bot.py index 04f6ed5..e1ac336 100644 --- a/apps/tgbot/tgbot/bot.py +++ b/apps/tgbot/tgbot/bot.py @@ -2,7 +2,9 @@ from aiogram import Bot, Dispatcher, executor from aiogram.contrib.fsm_storage.memory import MemoryStorage from tgbot.middlewares import UserMiddleware, WriteCommandMetric, LoggingMiddleware, ThrottlingMiddleware from tortoise import Tortoise +from tortoise.exceptions import DBConnectionError from loguru import logger +from asyncio import sleep import config import handlers @@ -17,12 +19,17 @@ async def database_init(): f"{config.MYSQL_HOST}:{config.MYSQL_PORT}/{config.MYSQL_DATABASE}" else: db_url = "sqlite://db.sqlite3" - await Tortoise.init( - db_url=db_url, - modules={ - 'models': ['tgbot.models'] - } - ) + try: + await Tortoise.init( + db_url=db_url, + modules={ + 'models': ['tgbot.models'] + } + ) + except DBConnectionError: + logger.error("Connection to database failed.") + await sleep(10) + await database_init() await Tortoise.generate_schemas() logger.info("Tortoise inited!") diff --git a/apps/tgbot/tgbot/middlewares/userdata.py b/apps/tgbot/tgbot/middlewares/userdata.py index c8e0476..99fb07e 100644 --- a/apps/tgbot/tgbot/middlewares/userdata.py +++ b/apps/tgbot/tgbot/middlewares/userdata.py @@ -10,6 +10,8 @@ def userdata_required(func): setattr(func, 'userdata_required', True) return func +# I think, now it useless + class UserMiddleware(BaseMiddleware):