From d91a6de53018553c54d683c4b9785e71125659d6 Mon Sep 17 00:00:00 2001 From: Dmitry Afanasyev Date: Tue, 16 Aug 2022 16:04:29 +0300 Subject: [PATCH] remove aiogram webhook method add gunicorn logs --- app/core/bot.py | 7 ++--- app/main.py | 61 +++++++++++++++++++++++++------------------- scripts/start-bot.sh | 12 ++++++++- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/app/core/bot.py b/app/core/bot.py index 6b2e105..56742e2 100644 --- a/app/core/bot.py +++ b/app/core/bot.py @@ -1,4 +1,5 @@ import asyncio +from concurrent.futures.thread import ThreadPoolExecutor from aiogram import Bot, types from aiogram.contrib.middlewares.logging import LoggingMiddleware @@ -20,6 +21,8 @@ driver = configure_firefox_driver() stations_cb = CallbackData('station', 'direction') +executor = ThreadPoolExecutor(10) + def get_keyboard() -> types.InlineKeyboardMarkup: """ @@ -66,6 +69,7 @@ async def office_home( 'l=masstransit&ll=37.505338%2C55.800160&tab=overview&z=211', message='Остановка Улица Алабяна', ) + return await bot.send_message( query.message.chat.id, text, reply_markup=get_keyboard() ) @@ -73,9 +77,6 @@ async def office_home( @dispatcher.message_handler(commands=['chatid']) async def chat_id(message: types.Message) -> types.Message: - from app.core.logger import logger - - logger.info(message) return await bot.send_message(message.chat.id, message.chat.id) diff --git a/app/main.py b/app/main.py index 1354c29..356ec0b 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ from http import HTTPStatus from aiogram import Bot, Dispatcher from aiogram.types import Update -from aiogram.utils.executor import start_polling, start_webhook +from aiogram.utils.executor import start_polling from aiohttp import web from app.core.bot import bot, dispatcher from app.core.logger import logger @@ -16,66 +16,75 @@ from app.settings import ( ) -async def on_startup(dp: Dispatcher) -> None: +async def bot_startup() -> None: await bot.set_webhook(WEBHOOK_URL) + logger.info(f'Webhook set to {WEBHOOK_URL}') asyncio_schedule() -async def on_shutdown(dp: Dispatcher) -> None: +async def bot_shutdown() -> None: logger.warning('Shutting down..') # Remove webhook (not acceptable in some cases) await bot.delete_webhook() # Close DB connection (if used) - await dp.storage.close() - await dp.storage.wait_closed() + await dispatcher.storage.close() + await dispatcher.storage.wait_closed() 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=on_startup, - on_shutdown=on_shutdown, - ) - - -def bot_webhook() -> None: - logger.info("Start bot with webhook") - start_webhook( - dispatcher=dispatcher, - webhook_path=WEBHOOK_PATH, - on_startup=on_startup, - on_shutdown=on_shutdown, - skip_updates=True, - host=WEBAPP_HOST, - port=WEBAPP_PORT, + on_startup=aiogram_startup, + on_shutdown=aiogram_shutdown, ) async def webhook(request: web.Request) -> web.Response: + """ + Listen {WEBHOOK_PATH} and proxy post request to bot + + :param request: + :return: + """ data = await request.json() tg_update = Update(**data) + Dispatcher.set_current(dispatcher) Bot.set_current(dispatcher.bot) + await dispatcher.process_update(tg_update) + return web.Response(status=HTTPStatus.OK) -async def on_startup_gunicorn(app: web.Application) -> None: - await bot.set_webhook(WEBHOOK_URL) - logger.info(f'Webhook set to {WEBHOOK_URL}') - asyncio_schedule() - - async def create_app() -> web.Application: application = web.Application() application.router.add_post(WEBHOOK_PATH, webhook) application.on_startup.append(on_startup_gunicorn) + application.on_shutdown.append(on_shutdown_gunicorn) return application diff --git a/scripts/start-bot.sh b/scripts/start-bot.sh index 80bd6b2..bbaadfa 100644 --- a/scripts/start-bot.sh +++ b/scripts/start-bot.sh @@ -4,4 +4,14 @@ echo "starting the bot" cd /opt/mosgortrans \ && source /home/balsh/.cache/pypoetry/virtualenvs/mosgortrans-3eZxMcY3-py3.10/bin/activate \ -&& gunicorn app.main:create_app --bind prod-server.lan:8084 --worker-class aiohttp.GunicornWebWorker \ No newline at end of file +&& gunicorn app.main:create_app \ + --bind prod-server.lan:8084 \ + --worker-class aiohttp.GunicornWebWorker \ + --timeout 150 \ + --max-requests 2000 \ + --max-requests-jitter 400 \ + --chdir "/opt/mosgortrans/logs" \ + --log-level info \ + --error-logfile "/opt/mosgortrans/logs" \ + --worker-tmp-dir '/tmp/shm' \ + --pid "/opt/mosgortrans/logs/gunicorn_pid" \ No newline at end of file