feat: add FastAPI lifespan and healthcheck endpoint (#144)

* chore: Add healthcheck endpoint + build indexes and constraints on svc startup

* chore: Bring back driver close call
This commit is contained in:
Pavlo Paliychuk 2024-09-23 10:12:35 -04:00 committed by GitHub
parent 5d2121e1a3
commit 2fc1b00602
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 5 deletions

View File

@ -158,8 +158,8 @@ class Graphiti:
# Use graphiti...
finally:
graphiti.close()
self.driver.close()
"""
self.driver.close()
async def build_indices_and_constraints(self):
"""

View File

@ -1,14 +1,29 @@
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from graph_service.config import get_settings
from graph_service.routers import ingest, retrieve
from graph_service.zep_graphiti import initialize_graphiti
app = FastAPI()
@asynccontextmanager
async def lifespan(_: FastAPI):
settings = get_settings()
await initialize_graphiti(settings)
yield
# Shutdown
# No need to close Graphiti here, as it's handled per-request
app = FastAPI(lifespan=lifespan)
app.include_router(retrieve.router)
app.include_router(ingest.router)
@app.get('/')
def read_root():
return {'Hello': 'World'}
@app.get('/healthcheck')
async def healthcheck():
return JSONResponse(content={'status': 'healthy'}, status_code=200)

View File

@ -82,6 +82,15 @@ async def get_graphiti(settings: ZepEnvDep):
client.close()
async def initialize_graphiti(settings: ZepEnvDep):
client = ZepGraphiti(
uri=settings.neo4j_uri,
user=settings.neo4j_user,
password=settings.neo4j_password,
)
await client.build_indices_and_constraints()
def get_fact_result_from_edge(edge: EntityEdge):
return FactResult(
uuid=edge.uuid,