add async queue

This commit is contained in:
Dmitry Afanasyev 2022-08-20 02:36:13 +03:00
parent 50bdc01b9d
commit d9b4556795

View File

@ -2,6 +2,7 @@ import asyncio
import sys import sys
from http import HTTPStatus from http import HTTPStatus
from pathlib import Path from pathlib import Path
from typing import Any
from aiogram import Bot, Dispatcher from aiogram import Bot, Dispatcher
from aiogram.types import Update from aiogram.types import Update
@ -22,11 +23,14 @@ from app.settings import (
WEBHOOK_URL, WEBHOOK_URL,
) )
queue: Any = asyncio.Queue()
async def bot_startup() -> None: async def bot_startup() -> None:
await bot.set_webhook(WEBHOOK_URL) await bot.set_webhook(WEBHOOK_URL)
logger.info(f'Webhook set to {WEBHOOK_URL}'.replace(API_TOKEN, '{BOT_API_TOKEN}')) logger.info(f'Webhook set to {WEBHOOK_URL}'.replace(API_TOKEN, '{BOT_API_TOKEN}'))
asyncio_schedule() asyncio_schedule()
await worker()
async def bot_shutdown() -> None: async def bot_shutdown() -> None:
@ -83,13 +87,17 @@ async def webhook(request: web.Request) -> web.Response:
""" """
data = await request.json() data = await request.json()
tg_update = Update(**data) tg_update = Update(**data)
queue.put_nowait(tg_update)
return web.Response(status=HTTPStatus.ACCEPTED)
async def worker() -> None:
Dispatcher.set_current(dispatcher) Dispatcher.set_current(dispatcher)
Bot.set_current(dispatcher.bot) Bot.set_current(dispatcher.bot)
await dispatcher.process_update(tg_update) while True:
update = await queue.get()
return web.Response(status=HTTPStatus.OK) await dispatcher.process_update(update)
async def create_app() -> web.Application: async def create_app() -> web.Application: