File tree 2 files changed +46
-0
lines changed
services/director-v2/src/simcore_service_director_v2/modules/db
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 1
1
from fastapi import FastAPI
2
2
from settings_library .postgres import PostgresSettings
3
3
4
+ from ._asyncpg import (
5
+ asyncpg_close_db_connection ,
6
+ asyncpg_connect_to_db ,
7
+ get_asyncpg_engine ,
8
+ )
4
9
from .events import close_db_connection , connect_to_db
5
10
6
11
7
12
def setup (app : FastAPI , settings : PostgresSettings ) -> None :
8
13
async def on_startup () -> None :
9
14
await connect_to_db (app , settings )
15
+ await asyncpg_connect_to_db (app , settings )
10
16
11
17
async def on_shutdown () -> None :
18
+ await asyncpg_close_db_connection (app )
12
19
await close_db_connection (app )
13
20
14
21
app .add_event_handler ("startup" , on_startup )
15
22
app .add_event_handler ("shutdown" , on_shutdown )
23
+
24
+
25
+ __all__ : tuple [str , ...] = ("get_asyncpg_engine" ,)
Original file line number Diff line number Diff line change
1
+ import logging
2
+
3
+ from fastapi import FastAPI
4
+ from servicelib .db_asyncpg_utils import create_async_engine_and_pg_database_ready
5
+ from servicelib .logging_utils import log_context
6
+ from settings_library .postgres import PostgresSettings
7
+ from simcore_postgres_database .utils_aiosqlalchemy import get_pg_engine_stateinfo
8
+
9
+ _logger = logging .getLogger (__name__ )
10
+
11
+
12
+ async def asyncpg_connect_to_db (app : FastAPI , settings : PostgresSettings ) -> None :
13
+ with log_context (
14
+ _logger ,
15
+ logging .DEBUG ,
16
+ f"Connecting and migraging { settings .dsn_with_async_sqlalchemy } " ,
17
+ ):
18
+ engine = await create_async_engine_and_pg_database_ready (settings )
19
+
20
+ app .state .asyncpg_engine = engine
21
+ _logger .debug (
22
+ "Setup engine: %s" ,
23
+ await get_pg_engine_stateinfo (engine ),
24
+ )
25
+
26
+
27
+ async def asyncpg_close_db_connection (app : FastAPI ) -> None :
28
+ with log_context (
29
+ _logger , logging .DEBUG , f"db disconnect of { app .state .asyncpg_engine } "
30
+ ):
31
+ if engine := app .state .asyncpg_engine :
32
+ await engine .dispose ()
33
+
34
+
35
+ def get_asyncpg_engine (app : FastAPI ):
36
+ return app .state .asyncpg_engine
You can’t perform that action at this time.
0 commit comments