From f27e3b8e0cc80061984c642ca6c4fc7128cea907 Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Mon, 5 Aug 2024 13:35:22 -0400 Subject: [PATCH] feat: use latest fastapi event management api (#298) --- server/src/curfu/main.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/server/src/curfu/main.py b/server/src/curfu/main.py index f56edef4..0f5df845 100644 --- a/server/src/curfu/main.py +++ b/server/src/curfu/main.py @@ -1,5 +1,8 @@ """Provide FastAPI application and route declarations.""" +from collections.abc import AsyncGenerator +from contextlib import asynccontextmanager + from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles @@ -124,15 +127,15 @@ def get_domain_services() -> DomainService: return domain_service -@app.on_event("startup") -async def startup() -> None: - """Get FUSOR reference""" +@asynccontextmanager +async def lifespan(app: FastAPI) -> AsyncGenerator: + """Configure FastAPI instance lifespan. + + :param app: FastAPI app instance + :return: async context handler + """ app.state.fusor = await start_fusor() app.state.genes = get_gene_services() app.state.domains = get_domain_services() - - -@app.on_event("shutdown") -async def shutdown() -> None: - """Clean up thread pool.""" + yield await app.state.fusor.cool_seq_tool.uta_db._connection_pool.close() # noqa: SLF001