remove aiogram webhook method add gunicorn logs

This commit is contained in:
Dmitry Afanasyev 2022-08-16 16:04:29 +03:00
parent b4c183332f
commit d91a6de530
3 changed files with 50 additions and 30 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
&& 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"