diff --git a/.gitignore b/.gitignore index 880743f..df2410f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,7 @@ queue_state.json system_prompt.yaml discord_glhf/__pycache__/bot.cpython-313.pyc discord_glhf/handlers/__pycache__/event_handler.cpython-313.pyc +discord_glhf/__pycache__/api.cpython-313.pyc +discord_glhf/__pycache__/bot.cpython-313.pyc +discord_glhf/handlers/__pycache__/event_handler.cpython-313.pyc +discord_glhf/web/__pycache__/app.cpython-313.pyc diff --git a/discord_glhf/__pycache__/api.cpython-313.pyc b/discord_glhf/__pycache__/api.cpython-313.pyc index 9a389a5..85387f5 100644 Binary files a/discord_glhf/__pycache__/api.cpython-313.pyc and b/discord_glhf/__pycache__/api.cpython-313.pyc differ diff --git a/discord_glhf/__pycache__/bot.cpython-313.pyc b/discord_glhf/__pycache__/bot.cpython-313.pyc index da38c59..839b875 100644 Binary files a/discord_glhf/__pycache__/bot.cpython-313.pyc and b/discord_glhf/__pycache__/bot.cpython-313.pyc differ diff --git a/discord_glhf/bot.py b/discord_glhf/bot.py index 938a191..2d3f7a6 100644 --- a/discord_glhf/bot.py +++ b/discord_glhf/bot.py @@ -47,7 +47,7 @@ class DiscordBot: self.tool_handler = None self.event_handler = None self.training_manager = TrainingManager() # Initialize training manager - self.http_server = None + self.web_shutdown = None async def _initialize_services(self) -> None: """Initialize API and queue services.""" @@ -84,7 +84,8 @@ class DiscordBot: logger.info("Initializing web interface...") app = init_app(self.event_handler) web_port = 5000 - asyncio.create_task(run_webserver(start_port=web_port)) + self.web_shutdown = asyncio.Event() + asyncio.create_task(run_webserver(start_port=web_port, shutdown_event=self.web_shutdown)) logger.info("Web interface initialized (first available port in range 5000-5009 will be used)") # Start API manager @@ -255,8 +256,12 @@ class DiscordBot: stop_tasks = [] if self.training_manager and self.training_manager.is_running: stop_tasks.append(self.training_manager.stop()) - # Web app will be stopped when the event loop closes - logger.info("Web app will be stopped with event loop") + # Stop web server gracefully + if self.web_shutdown: + logger.info("Stopping web server...") + self.web_shutdown.set() + await asyncio.sleep(1) # Give web server a moment to shut down + logger.info("Web server stopped") if self.db_pool: stop_tasks.append(self.db_pool.close()) diff --git a/discord_glhf/handlers/__pycache__/event_handler.cpython-313.pyc b/discord_glhf/handlers/__pycache__/event_handler.cpython-313.pyc index 7cab6e0..f0bb42f 100644 Binary files a/discord_glhf/handlers/__pycache__/event_handler.cpython-313.pyc and b/discord_glhf/handlers/__pycache__/event_handler.cpython-313.pyc differ diff --git a/discord_glhf/web/__pycache__/app.cpython-313.pyc b/discord_glhf/web/__pycache__/app.cpython-313.pyc index 7f08b3f..e08027e 100644 Binary files a/discord_glhf/web/__pycache__/app.cpython-313.pyc and b/discord_glhf/web/__pycache__/app.cpython-313.pyc differ diff --git a/discord_glhf/web/app.py b/discord_glhf/web/app.py index e26c833..3b32a82 100644 --- a/discord_glhf/web/app.py +++ b/discord_glhf/web/app.py @@ -67,7 +67,7 @@ async def send_prompt(): except Exception as e: return jsonify({'error': str(e)}), 500 -async def run_webserver(start_port=5000): +async def run_webserver(start_port=5000, shutdown_event=None): """Run the web server.""" import hypercorn.asyncio from hypercorn.config import Config @@ -87,11 +87,14 @@ async def run_webserver(start_port=5000): logger.info(f"Starting web interface at http://localhost:{port}") try: - await hypercorn.asyncio.serve(app, config) - break - except KeyboardInterrupt: - logger.info("Web server shutdown requested") + await hypercorn.asyncio.serve(app, config, + shutdown_trigger=shutdown_event.wait if shutdown_event else None) break + except Exception as e: + if isinstance(e, KeyboardInterrupt) or (shutdown_event and shutdown_event.is_set()): + logger.info("Web server shutdown requested") + break + raise except OSError: if port == start_port + 9: # Last attempt logger.error(f"Could not find an available port in range {start_port}-{port}")