From a7711cc2e0ef6a6e6e69cd736d05546a563e7af0 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 6 Aug 2024 12:04:26 +0300 Subject: [PATCH] close web-session on app close --- examples/echo_bot.py | 3 +++ examples/echo_bot_webhook.py | 8 +++++++- examples/echo_bot_webhook_ssl.py | 8 +++++++- examples/error_handling.py | 4 ++++ examples/finite_state_machine.py | 3 +++ examples/multibot.py | 6 ++++++ examples/quiz_scene.py | 1 + examples/specify_updates.py | 3 +++ examples/stars_invoice.py | 3 +++ 9 files changed, 37 insertions(+), 2 deletions(-) diff --git a/examples/echo_bot.py b/examples/echo_bot.py index 6a720ddb..bfaff4d5 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -52,6 +52,9 @@ async def main() -> None: # And the run events dispatching await dp.start_polling(bot) + # Don't forget to close web-session on your app close + await bot.session.close() + if __name__ == "__main__": logging.basicConfig(level=logging.INFO, stream=sys.stdout) diff --git a/examples/echo_bot_webhook.py b/examples/echo_bot_webhook.py index 07a83351..d419622d 100644 --- a/examples/echo_bot_webhook.py +++ b/examples/echo_bot_webhook.py @@ -71,14 +71,20 @@ async def on_startup(bot: Bot) -> None: await bot.set_webhook(f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}", secret_token=WEBHOOK_SECRET) +async def on_shutdown(bot: Bot) -> None: + # Gracefully close web-sessions on app shutdown + await bot.session.close() + + def main() -> None: # Dispatcher is a root router dp = Dispatcher() # ... and all other routers should be attached to Dispatcher dp.include_router(router) - # Register startup hook to initialize webhook + # Register startup and shutdown hooks dp.startup.register(on_startup) + dp.shutdown.register(on_shutdown) # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) diff --git a/examples/echo_bot_webhook_ssl.py b/examples/echo_bot_webhook_ssl.py index 28c14d1c..dc8acf2c 100644 --- a/examples/echo_bot_webhook_ssl.py +++ b/examples/echo_bot_webhook_ssl.py @@ -82,14 +82,20 @@ async def on_startup(bot: Bot) -> None: ) +async def on_shutdown(bot: Bot) -> None: + # Gracefully close web-sessions on app shutdown + await bot.session.close() + + def main() -> None: # Dispatcher is a root router dp = Dispatcher() # ... and all other routers should be attached to Dispatcher dp.include_router(router) - # Register startup hook to initialize webhook + # Register startup and shutdown hooks dp.startup.register(on_startup) + dp.shutdown.register(on_shutdown) # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) diff --git a/examples/error_handling.py b/examples/error_handling.py index fca83f82..79d494e1 100644 --- a/examples/error_handling.py +++ b/examples/error_handling.py @@ -104,9 +104,13 @@ async def handle_set_name(message: types.Message, command: CommandObject) -> Non async def main() -> None: # Initialize Bot instance with default bot properties which will be passed to all API calls bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) + # And the run events dispatching await dp.start_polling(bot) + # Don't forget to close web-session on your app close + await bot.session.close() + if __name__ == "__main__": loop = asyncio.new_event_loop() diff --git a/examples/finite_state_machine.py b/examples/finite_state_machine.py index f371d2ca..61ca7643 100644 --- a/examples/finite_state_machine.py +++ b/examples/finite_state_machine.py @@ -135,6 +135,9 @@ async def main(): # Start event dispatching await dp.start_polling(bot) + # Don't forget to close web-session on your app close + await bot.session.close() + if __name__ == "__main__": logging.basicConfig(level=logging.INFO, stream=sys.stdout) diff --git a/examples/multibot.py b/examples/multibot.py index 82fac4a1..818eb3e2 100644 --- a/examples/multibot.py +++ b/examples/multibot.py @@ -58,6 +58,11 @@ async def on_startup(dispatcher: Dispatcher, bot: Bot): await bot.set_webhook(f"{BASE_URL}{MAIN_BOT_PATH}") +async def on_shutdown(dispatcher: Dispatcher, bot: Bot) -> None: + # Gracefully close web-sessions on app shutdown + await bot.session.close() + + def main(): logging.basicConfig(level=logging.INFO, stream=sys.stdout) session = AiohttpSession() @@ -70,6 +75,7 @@ def main(): main_dispatcher = Dispatcher(storage=storage) main_dispatcher.include_router(main_router) main_dispatcher.startup.register(on_startup) + main_dispatcher.shutdown.register(on_shutdown) multibot_dispatcher = Dispatcher(storage=storage) multibot_dispatcher.include_router(form_router) diff --git a/examples/quiz_scene.py b/examples/quiz_scene.py index b07888df..a27b7c81 100644 --- a/examples/quiz_scene.py +++ b/examples/quiz_scene.py @@ -292,6 +292,7 @@ async def main(): dp = create_dispatcher() bot = Bot(token=TOKEN) await dp.start_polling(bot) + await bot.session.close() if __name__ == "__main__": diff --git a/examples/specify_updates.py b/examples/specify_updates.py index c25613a2..92563a8b 100644 --- a/examples/specify_updates.py +++ b/examples/specify_updates.py @@ -91,6 +91,9 @@ async def main() -> None: # Start event dispatching await dp.start_polling(bot) + # Don't forget to close web-session on your app close + await bot.session.close() + if __name__ == "__main__": logging.basicConfig(level=logging.INFO, stream=sys.stdout) diff --git a/examples/stars_invoice.py b/examples/stars_invoice.py index 917c491a..d49a83be 100644 --- a/examples/stars_invoice.py +++ b/examples/stars_invoice.py @@ -58,6 +58,9 @@ async def main() -> None: await dispatcher.start_polling(bot) + # Don't forget to close web-session on your app close + await bot.session.close() + if __name__ == "__main__": logging.basicConfig(level=logging.INFO)