From cb6859c88d55d0979c8f683af655838302078539 Mon Sep 17 00:00:00 2001 From: thedragonsinn <98635854+thedragonsinn@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:47:32 +0530 Subject: [PATCH] aio web server fixes. --- app/core/client/client.py | 9 ++++++--- app/utils/aiohttp_tools.py | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/core/client/client.py b/app/core/client/client.py index deadbca..0fabeb8 100644 --- a/app/core/client/client.py +++ b/app/core/client/client.py @@ -6,13 +6,14 @@ import sys import traceback from io import BytesIO -from app import DB_CLIENT, LOGGER, Config, Message -from app.core.decorators.add_cmd import AddCmd -from app.utils.aiohttp_tools import aio from pyrogram import Client, filters, idle from pyrogram.enums import ParseMode from pyrogram.types import Message as Msg +from app import DB_CLIENT, LOGGER, Config, Message +from app.core.decorators.add_cmd import AddCmd +from app.utils.aiohttp_tools import aio + def import_modules(): for py_module in glob.glob(pathname="app/**/[!^_]*.py", recursive=True): @@ -67,6 +68,7 @@ class BOT(Client, AddCmd): LOGGER.info("Idling...") await idle() await aio.session.close() + await aio.runner.cleanup() LOGGER.info("DB Closed.") DB_CLIENT.close() @@ -100,6 +102,7 @@ class BOT(Client, AddCmd): await super().stop(block=False) LOGGER.info("Closing DB...") DB_CLIENT.close() + await aio.runner.cleanup() if hard: os.remove("logs/app_logs.txt") os.execl("/bin/bash", "/bin/bash", "run") diff --git a/app/utils/aiohttp_tools.py b/app/utils/aiohttp_tools.py index ced2e22..a932ac8 100644 --- a/app/utils/aiohttp_tools.py +++ b/app/utils/aiohttp_tools.py @@ -12,7 +12,6 @@ class Aio: def __init__(self): self.session: ClientSession | None = None self.app = None - self.site = None self.port = os.environ.get("API_PORT", 0) self.runner = None if self.port: @@ -28,8 +27,10 @@ class Aio: self.app.router.add_get(path="/", handler=self.handle_request) self.runner = web.AppRunner(self.app) await self.runner.setup() - self.site = web.TCPSite(self.runner, "0.0.0.0", self.port) - await self.site.start() + site = web.TCPSite( + self.runner, "0.0.0.0", self.port, reuse_address=True, reuse_port=True + ) + await site.start() async def handle_request(self, _): return web.Response(text="Web Server Running...")