Refactor web server initialization for graceful shutdown; update .gitignore to exclude additional cache files
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -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
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -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())
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user