Skip to content

Commit

Permalink
Close engine connection at the end of the lifespan and patch logger (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Aug 2, 2024
1 parent e449756 commit 3c89d09
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions labs_api/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import typing as t
from contextlib import asynccontextmanager

import fastapi
import uvicorn
from config import Config
from fastapi.middleware.cors import CORSMiddleware
from prediction_market_agent_tooling.gtypes import HexAddress
from prediction_market_agent_tooling.loggers import logger
from prediction_market_agent_tooling.loggers import logger, patch_logger

from labs_api.config import Config
from labs_api.insights import MarketInsightsResponse, market_insights_cached
Expand All @@ -22,7 +23,16 @@


def create_app() -> fastapi.FastAPI:
app = fastapi.FastAPI()
patch_logger()

@asynccontextmanager
async def lifespan(app: fastapi.FastAPI) -> t.AsyncIterator[None]:
# At start of the service.
yield
# At end of the service.
market_insights_cache.engine.dispose()

app = fastapi.FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
Expand Down

0 comments on commit 3c89d09

Please sign in to comment.