Skip to content

Commit

Permalink
init async resources at app start
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnik512 committed May 25, 2024
1 parent 0246594 commit 0b451c3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self) -> None:
@contextlib.asynccontextmanager
async def lifespan_manager(self, _: fastapi.FastAPI) -> typing.AsyncIterator[dict[str, typing.Any]]:
try:
await ioc.IOCContainer.init_async_resources()
yield {}
finally:
await ioc.IOCContainer.tear_down()
Expand Down
6 changes: 3 additions & 3 deletions app/db/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


async def create_sa_engine(settings: Settings) -> typing.AsyncIterator[sa.AsyncEngine]:
logger.info("Initializing SQLAlchemy engine")
logger.debug("Initializing SQLAlchemy engine")
engine = sa.create_async_engine(
url=settings.db_dsn,
echo=settings.debug,
Expand All @@ -19,12 +19,12 @@ async def create_sa_engine(settings: Settings) -> typing.AsyncIterator[sa.AsyncE
pool_pre_ping=settings.db_pool_pre_ping,
max_overflow=settings.db_max_overflow,
)
logger.info("SQLAlchemy engine has been initialized")
logger.debug("SQLAlchemy engine has been initialized")
try:
yield engine
finally:
await engine.dispose()
logger.info("SQLAlchemy engine has been cleaned up")
logger.debug("SQLAlchemy engine has been cleaned up")


async def create_session(engine: sa.AsyncEngine) -> typing.AsyncIterator[sa.AsyncSession]:
Expand Down

0 comments on commit 0b451c3

Please sign in to comment.