Skip to content

Commit 8409407

Browse files
committed
connect with asyncpg
1 parent 806269c commit 8409407

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

services/api-server/src/simcore_service_api_server/core/events.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
from fastapi import FastAPI
55

66
from .._meta import APP_FINISHED_BANNER_MSG, APP_STARTED_BANNER_MSG
7-
from ..db.events import close_db_connection, connect_to_db
7+
from ..db.events import (
8+
asyncpg_close_db_connection,
9+
asyncpg_connect_to_db,
10+
close_db_connection,
11+
connect_to_db,
12+
)
13+
from .settings import ApplicationSettings
814

915
logger = logging.getLogger(__name__)
1016

@@ -14,10 +20,12 @@ async def _on_startup() -> None:
1420
logger.info("Application starting ...")
1521
if app.state.settings.API_SERVER_POSTGRES:
1622
# database
23+
assert isinstance(app.state.settings, ApplicationSettings) # nosec
1724
await connect_to_db(app)
25+
await asyncpg_connect_to_db(app, app.state.settings.API_SERVER_POSTGRES)
1826
assert app.state.engine # nosec
1927

20-
print(APP_STARTED_BANNER_MSG, flush=True)
28+
print(APP_STARTED_BANNER_MSG, flush=True) # noqa: T201
2129

2230
return _on_startup
2331

@@ -27,7 +35,9 @@ async def _on_shutdown() -> None:
2735
logger.info("Application stopping, ...")
2836

2937
if app.state.settings.API_SERVER_POSTGRES:
38+
assert isinstance(app.state.settings, ApplicationSettings) # nosec
3039
try:
40+
await asyncpg_close_db_connection(app)
3141
await close_db_connection(app)
3242

3343
except Exception as err: # pylint: disable=broad-except
@@ -38,6 +48,6 @@ async def _on_shutdown() -> None:
3848
stack_info=app.state.settings.debug,
3949
)
4050

41-
print(APP_FINISHED_BANNER_MSG, flush=True)
51+
print(APP_FINISHED_BANNER_MSG, flush=True) # noqa: T201
4252

4353
return _on_shutdown

services/api-server/src/simcore_service_api_server/db/events.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
from aiopg.sa import Engine, create_engine
44
from fastapi import FastAPI
5+
from servicelib.db_asyncpg_utils import create_async_engine_and_pg_database_ready
6+
from servicelib.logging_utils import log_context
57
from servicelib.retry_policies import PostgresRetryPolicyUponInitialization
68
from settings_library.postgres import PostgresSettings
79
from simcore_postgres_database.utils_aiopg import (
810
close_engine,
911
get_pg_engine_info,
1012
raise_if_migration_not_ready,
1113
)
14+
from simcore_postgres_database.utils_aiosqlalchemy import get_pg_engine_stateinfo
1215
from tenacity import retry
1316

1417
from .._meta import PROJECT_NAME
@@ -53,3 +56,30 @@ async def close_db_connection(app: FastAPI) -> None:
5356
await close_engine(engine)
5457

5558
logger.debug("Disconnected from %s", engine.dsn)
59+
60+
61+
async def asyncpg_connect_to_db(app: FastAPI, settings: PostgresSettings) -> None:
62+
with log_context(
63+
logger,
64+
logging.DEBUG,
65+
f"Connecting and migraging {settings.dsn_with_async_sqlalchemy}",
66+
):
67+
engine = await create_async_engine_and_pg_database_ready(settings)
68+
69+
app.state.asyncpg_engine = engine
70+
logger.debug(
71+
"Setup engine: %s",
72+
await get_pg_engine_stateinfo(engine),
73+
)
74+
75+
76+
async def asyncpg_close_db_connection(app: FastAPI) -> None:
77+
with log_context(
78+
logger, logging.DEBUG, f"db disconnect of {app.state.asyncpg_engine}"
79+
):
80+
if engine := app.state.asyncpg_engine:
81+
await engine.dispose()
82+
83+
84+
def get_asyncpg_engine(app: FastAPI):
85+
return app.state.asyncpg_engine

0 commit comments

Comments
 (0)