Skip to content

Fix startup w/ seed_db #4481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions backend/ee/onyx/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager

from fastapi import FastAPI
from httpx_oauth.clients.google import GoogleOAuth2
from httpx_oauth.clients.openid import BASE_SCOPES
Expand Down Expand Up @@ -44,21 +47,36 @@
from onyx.main import get_application as get_application_base
from onyx.main import include_auth_router_with_prefix
from onyx.main import include_router_with_global_prefix_prepended
from onyx.main import lifespan as lifespan_base
from onyx.utils.logger import setup_logger
from onyx.utils.variable_functionality import global_version
from shared_configs.configs import MULTI_TENANT

logger = setup_logger()


@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"""Small wrapper around the lifespan of the MIT application.
Basically just calls the base lifespan, and then adds EE-only
steps after."""

async with lifespan_base(app):
# seed the Onyx environment with LLMs, Assistants, etc. based on an optional
# environment variable. Used to automate deployment for multiple environments.
seed_db()

yield


def get_application() -> FastAPI:
# Anything that happens at import time is not guaranteed to be running ee-version
# Anything after the server startup will be running ee version
global_version.set_ee()

test_encryption()

application = get_application_base()
application = get_application_base(lifespan_override=lifespan)

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

# seed the Onyx environment with LLMs, Assistants, etc. based on an optional
# environment variable. Used to automate deployment for multiple environments.
seed_db()

# for debugging discovered routes
# for route in application.router.routes:
# print(f"Path: {route.path}, Methods: {route.methods}")
Expand Down
9 changes: 7 additions & 2 deletions backend/onyx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
from sqlalchemy.orm import Session
from starlette.types import Lifespan

from onyx import __version__
from onyx.auth.schemas import UserCreate
Expand Down Expand Up @@ -275,8 +276,12 @@ def log_http_error(request: Request, exc: Exception) -> JSONResponse:
)


def get_application() -> FastAPI:
application = FastAPI(title="Onyx Backend", version=__version__, lifespan=lifespan)
def get_application(lifespan_override: Lifespan | None = None) -> FastAPI:
application = FastAPI(
title="Onyx Backend",
version=__version__,
lifespan=lifespan_override or lifespan,
)
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
Expand Down
Loading