From d7b7d5efe179c0cb5759d2fe77d8bdf6f0e305f7 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Sat, 8 Mar 2025 18:37:22 -0700 Subject: [PATCH] more ws --- discord-bot/main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/discord-bot/main.py b/discord-bot/main.py index bc7a78a..bf057ec 100644 --- a/discord-bot/main.py +++ b/discord-bot/main.py @@ -111,12 +111,22 @@ async def start_websocket_server(): await asyncio.Future() + @asynccontextmanager async def lifespan(app: FastAPI): - asyncio.create_task(start_websocket_server()) - asyncio.create_task(bot.start(os.getenv("DISCORD_SECRET"))) + # Start WebSocket and Discord bot in managed background tasks + websocket_task = asyncio.create_task(start_websocket_server()) + bot_task = asyncio.create_task(bot.start(os.getenv("DISCORD_SECRET"))) + + app.state.websocket_task = websocket_task + app.state.bot_task = bot_task + yield + app.state.websocket_task.cancel() + app.state.bot_task.cancel() + await asyncio.gather(app.state.websocket_task, app.state.bot_task, return_exceptions=True) + app = FastAPI(lifespan=lifespan) app.mount("/", StaticFiles(directory="./client", html=True), name="static") \ No newline at end of file