Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

refactor(backend): Replace deprecated event hooks with lifecycle handler (#8092)

vishesh1024cc5131d74b
diff --git a/autogpt_platform/backend/backend/server/ws_api.py b/autogpt_platform/backend/backend/server/ws_api.py
index da941233d0..91267602d9 100644
--- a/autogpt_platform/backend/backend/server/ws_api.py
+++ b/autogpt_platform/backend/backend/server/ws_api.py
@@ -1,5 +1,6 @@
import asyncio
import logging
+from contextlib import asynccontextmanager
import uvicorn
from autogpt_libs.auth import parse_jwt_token
@@ -16,7 +17,17 @@ from backend.util.settings import Config, Settings
logger = logging.getLogger(__name__)
settings = Settings()
-app = FastAPI()
+
+@asynccontextmanager
+async def lifespan(app: FastAPI):
+ await event_queue.connect()
+ manager = get_connection_manager()
+ asyncio.create_task(event_broadcaster(manager))
+ yield
+ await event_queue.close()
+
+
+app = FastAPI(lifespan=lifespan)
event_queue = AsyncRedisEventQueue()
_connection_manager = None
@@ -37,18 +48,6 @@ def get_connection_manager():
return _connection_manager
[email protected]_event("startup")
-async def startup_event():
- await event_queue.connect()
- manager = get_connection_manager()
- asyncio.create_task(event_broadcaster(manager))
-
-
[email protected]_event("shutdown")
-async def shutdown_event():
- await event_queue.close()
-
-
async def event_broadcaster(manager: ConnectionManager):
while True:
event = await event_queue.get()