From 8d86eb9407cdf47fa6aa61a8e9f2944440fb3ec9 Mon Sep 17 00:00:00 2001 From: Dmitry Afanasyev Date: Sun, 21 Aug 2022 23:47:18 +0300 Subject: [PATCH] add queue to bot --- app/core/bot.py | 1 - app/main.py | 32 +++++--------------------------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/app/core/bot.py b/app/core/bot.py index 0c0c581..f86fa54 100644 --- a/app/core/bot.py +++ b/app/core/bot.py @@ -75,7 +75,6 @@ async def office_home( @dispatcher.message_handler(commands=['chatid']) async def chat_id(message: types.Message) -> types.Message: - logger.info(message.chat.id) return await bot.send_message(message.chat.id, message.chat.id) diff --git a/app/main.py b/app/main.py index 1fc802d..e9462ff 100644 --- a/app/main.py +++ b/app/main.py @@ -25,7 +25,8 @@ from app.settings import ( queue = asyncio.Queue() # type: ignore -async def bot_startup() -> None: +async def on_startup(dp: Dispatcher) -> None: + logger.info("Start bot with webhook") await bot.set_webhook(WEBHOOK_URL) loop = asyncio.get_running_loop() loop.create_task(get_updates_from_queue()) @@ -33,16 +34,12 @@ async def bot_startup() -> None: asyncio_schedule() -async def bot_shutdown() -> None: +async def on_shutdown(dp: Dispatcher) -> None: logger.warning('Shutting down..') # Remove webhook (not acceptable in some cases) await bot.delete_webhook() - # Close DB connection (if used) - await dispatcher.storage.close() - await dispatcher.storage.wait_closed() - session = await bot.get_session() if session and not session.closed: await session.close() @@ -51,30 +48,11 @@ async def bot_shutdown() -> None: logger.warning('Bye!') -async def aiogram_startup(dp: Dispatcher) -> None: - await bot_startup() - - -async def aiogram_shutdown(dp: Dispatcher) -> None: - await bot_shutdown() - - -async def on_startup_gunicorn(app: web.Application) -> None: - logger.info("Start bot with webhook") - await bot_startup() - - -async def on_shutdown_gunicorn(app: web.Application) -> None: - await bot_shutdown() - - def bot_polling() -> None: logger.info("Start bot in polling mode") start_polling( dispatcher=dispatcher, skip_updates=True, - on_startup=aiogram_startup, - on_shutdown=aiogram_shutdown, ) @@ -103,8 +81,8 @@ async def get_updates_from_queue() -> None: async def create_app() -> web.Application: application = web.Application() application.router.add_post(f'{WEBHOOK_PATH}/{API_TOKEN}', put_updates_on_queue) - application.on_startup.append(on_startup_gunicorn) - application.on_shutdown.append(on_shutdown_gunicorn) + application.on_startup.append(on_startup) + application.on_shutdown.append(on_shutdown) return application