Skip to content

Commit

Permalink
Merge pull request #1969 from DSD-DBS/lifespan
Browse files Browse the repository at this point in the history
refactor(backend): Replace deprecated `on_startup` and `on_shutdown`
  • Loading branch information
MoritzWeber0 authored Nov 8, 2024
2 parents b6f1bf2 + 94b4927 commit dd6abbd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 60 deletions.
32 changes: 17 additions & 15 deletions backend/capellacollab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# SPDX-License-Identifier: Apache-2.0


import contextlib
import logging
import os
import typing as t

import fastapi
import fastapi_pagination
Expand Down Expand Up @@ -45,33 +45,35 @@
]


async def startup():
@contextlib.asynccontextmanager
async def lifespan(_app: fastapi.FastAPI):
del _app

migration.migrate_db(engine, config.database.url)
logging.info("Migrations done - Server is running")

# This is needed to load the Kubernetes configuration at startup
# Load the Kubernetes configuration at startup
operators.get_operator()

idletimeout.terminate_idle_sessions_in_background()
pipeline_runs_interface.schedule_refresh_and_trigger_pipeline_jobs()
sessions_auth.initialize_session_pre_authentication()

metrics.register_metrics()

logging.getLogger("uvicorn.access").disabled = True
logging.getLogger("kubernetes.client.rest").setLevel("INFO")

logging.info("Startup completed.")

async def shutdown():
logging.getLogger("uvicorn.access").disabled = False
logging.getLogger("uvicorn.error").disabled = False
yield

logging.getLogger("uvicorn.access").disabled = False
logging.info("Shutdown completed.")

on_startup: list[t.Callable] = [
startup,
idletimeout.terminate_idle_sessions_in_background,
pipeline_runs_interface.schedule_refresh_and_trigger_pipeline_jobs,
sessions_auth.initialize_session_pre_authentication,
]

app = fastapi.FastAPI(
title="Capella Collaboration",
version=__version__,
on_startup=on_startup + metrics.metrics_registration,
middleware=[
middleware.Middleware(
cors.CORSMiddleware,
Expand All @@ -86,7 +88,7 @@ async def shutdown():
middleware.Middleware(core_logging.LogRequestsMiddleware),
middleware.Middleware(starlette_prometheus.PrometheusMiddleware),
],
on_shutdown=[shutdown],
lifespan=lifespan,
openapi_url="/api/docs/openapi.json",
docs_url="/api/docs/swagger",
redoc_url="/api/docs/redoc",
Expand Down
72 changes: 37 additions & 35 deletions backend/capellacollab/core/database/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,43 +52,45 @@


def migrate_db(engine, database_url: str):
if os.getenv("ALEMBIC_CONTEXT") != "1":
os.environ["ALEMBIC_CONFIGURE_LOGGER"] = "false"
root_dir = pathlib.Path(__file__).parents[2]
if os.getenv("ALEMBIC_CONTEXT") == "1":
return

# Get current revision of Database. If no revision is available, initialize the database.
os.environ["ALEMBIC_CONFIGURE_LOGGER"] = "false"
root_dir = pathlib.Path(__file__).parents[2]

alembic_cfg = alembic_config.Config(str(root_dir / "alembic.ini"))
alembic_cfg.set_main_option(
"script_location", str(root_dir / "alembic")
)
alembic_cfg.set_main_option("sqlalchemy.url", database_url)
alembic_cfg.attributes["configure_logger"] = False

with engine.connect() as conn:
context = migration.MigrationContext.configure(conn)
current_rev = context.get_current_revision()

session_maker = orm.sessionmaker(bind=engine)

with session_maker() as session:
if current_rev:
LOGGER.info("Upgrade database to head")
command.upgrade(alembic_cfg, "head")
else:
LOGGER.info("Empty database detected.")
database.Base.metadata.create_all(bind=engine)
LOGGER.info("Database structure creation successful")
command.stamp(alembic_cfg, "head")
initialize_admin_user(session)
initialize_default_project(session)
initialize_coffee_machine_project(session)

create_tools(session)
create_t4c_instance_and_repositories(session)
create_github_instance(session)
create_default_models(session)
create_coffee_machine_model(session)
# Get current revision of Database. If no revision is available, initialize the database.

alembic_cfg = alembic_config.Config(str(root_dir / "alembic.ini"))
alembic_cfg.set_main_option("script_location", str(root_dir / "alembic"))
alembic_cfg.set_main_option("sqlalchemy.url", database_url)
alembic_cfg.attributes["configure_logger"] = False

with engine.connect() as conn:
context = migration.MigrationContext.configure(conn)
current_rev = context.get_current_revision()

session_maker = orm.sessionmaker(bind=engine)

with session_maker() as session:
if current_rev:
LOGGER.info("Upgrade database to head")
command.upgrade(alembic_cfg, "head")
else:
LOGGER.info("Empty database detected.")
database.Base.metadata.create_all(bind=engine)
LOGGER.info("Database structure creation successful")
command.stamp(alembic_cfg, "head")
initialize_admin_user(session)
initialize_default_project(session)
initialize_coffee_machine_project(session)

create_tools(session)
create_t4c_instance_and_repositories(session)
create_github_instance(session)
create_default_models(session)
create_coffee_machine_model(session)

logging.info("Database migrations completed.")


def initialize_admin_user(db: orm.Session):
Expand Down
14 changes: 6 additions & 8 deletions backend/capellacollab/metrics.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t

import capellacollab.sessions.metrics as sessions_metrics
import capellacollab.settings.modelsources.t4c.license_server.metrics as t4c_metrics
from capellacollab.core.database import metrics as database_metrics
from capellacollab.feedback import metrics as feedback_metrics

metrics_registration: list[t.Callable] = [
sessions_metrics.register,
t4c_metrics.register,
feedback_metrics.register,
database_metrics.register,
]

def register_metrics():
sessions_metrics.register()
t4c_metrics.register()
feedback_metrics.register()
database_metrics.register()
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
PIPELINES_TIMEOUT = config.pipelines.timeout


async def schedule_refresh_and_trigger_pipeline_jobs(interval=5):
def schedule_refresh_and_trigger_pipeline_jobs(interval=5):
async def loop():
while True:
try:
Expand Down
2 changes: 1 addition & 1 deletion backend/capellacollab/sessions/idletimeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def terminate_idle_session():
operators.get_operator().kill_session(session_id)


async def terminate_idle_sessions_in_background(interval=60):
def terminate_idle_sessions_in_background(interval=60):
async def loop():
while True:
try:
Expand Down

0 comments on commit dd6abbd

Please sign in to comment.