Skip to content

Commit 634d990

Browse files
authored
Fix startup w/ seed_db (#4481)
1 parent 5792261 commit 634d990

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

backend/ee/onyx/main.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from collections.abc import AsyncGenerator
2+
from contextlib import asynccontextmanager
3+
14
from fastapi import FastAPI
25
from httpx_oauth.clients.google import GoogleOAuth2
36
from httpx_oauth.clients.openid import BASE_SCOPES
@@ -44,21 +47,36 @@
4447
from onyx.main import get_application as get_application_base
4548
from onyx.main import include_auth_router_with_prefix
4649
from onyx.main import include_router_with_global_prefix_prepended
50+
from onyx.main import lifespan as lifespan_base
4751
from onyx.utils.logger import setup_logger
4852
from onyx.utils.variable_functionality import global_version
4953
from shared_configs.configs import MULTI_TENANT
5054

5155
logger = setup_logger()
5256

5357

58+
@asynccontextmanager
59+
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
60+
"""Small wrapper around the lifespan of the MIT application.
61+
Basically just calls the base lifespan, and then adds EE-only
62+
steps after."""
63+
64+
async with lifespan_base(app):
65+
# seed the Onyx environment with LLMs, Assistants, etc. based on an optional
66+
# environment variable. Used to automate deployment for multiple environments.
67+
seed_db()
68+
69+
yield
70+
71+
5472
def get_application() -> FastAPI:
5573
# Anything that happens at import time is not guaranteed to be running ee-version
5674
# Anything after the server startup will be running ee version
5775
global_version.set_ee()
5876

5977
test_encryption()
6078

61-
application = get_application_base()
79+
application = get_application_base(lifespan_override=lifespan)
6280

6381
if MULTI_TENANT:
6482
add_tenant_id_middleware(application, logger)
@@ -166,10 +184,6 @@ def get_application() -> FastAPI:
166184
# Ensure all routes have auth enabled or are explicitly marked as public
167185
check_ee_router_auth(application)
168186

169-
# seed the Onyx environment with LLMs, Assistants, etc. based on an optional
170-
# environment variable. Used to automate deployment for multiple environments.
171-
seed_db()
172-
173187
# for debugging discovered routes
174188
# for route in application.router.routes:
175189
# print(f"Path: {route.path}, Methods: {route.methods}")

backend/onyx/main.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from sentry_sdk.integrations.fastapi import FastApiIntegration
2222
from sentry_sdk.integrations.starlette import StarletteIntegration
2323
from sqlalchemy.orm import Session
24+
from starlette.types import Lifespan
2425

2526
from onyx import __version__
2627
from onyx.auth.schemas import UserCreate
@@ -275,8 +276,12 @@ def log_http_error(request: Request, exc: Exception) -> JSONResponse:
275276
)
276277

277278

278-
def get_application() -> FastAPI:
279-
application = FastAPI(title="Onyx Backend", version=__version__, lifespan=lifespan)
279+
def get_application(lifespan_override: Lifespan | None = None) -> FastAPI:
280+
application = FastAPI(
281+
title="Onyx Backend",
282+
version=__version__,
283+
lifespan=lifespan_override or lifespan,
284+
)
280285
if SENTRY_DSN:
281286
sentry_sdk.init(
282287
dsn=SENTRY_DSN,

0 commit comments

Comments
 (0)