generated from john/python-template
shutdown fixes
This commit is contained in:
@@ -74,7 +74,6 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
# --- persistence ---
|
# --- persistence ---
|
||||||
database: DatabaseSettings = Field(default_factory=SqliteSettings)
|
database: DatabaseSettings = Field(default_factory=SqliteSettings)
|
||||||
database_url: str = "sqlite:///./transcription.db"
|
|
||||||
bootstrap_schema_on_startup: bool = False
|
bootstrap_schema_on_startup: bool = False
|
||||||
sqlite_check_same_thread: bool = False
|
sqlite_check_same_thread: bool = False
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionTransaction
|
|||||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
|
from ..config import Settings
|
||||||
from ..config import get_settings
|
from ..config import get_settings
|
||||||
from .engine import dispose_engine
|
from .engine import dispose_engine
|
||||||
from .engine import get_database_url
|
from .engine import get_database_url
|
||||||
@@ -25,8 +26,13 @@ def get_session_factory(database_url: str) -> SessionFactory:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def resolve_session_factory(database_url: str | None = None) -> SessionFactory:
|
def resolve_session_factory(
|
||||||
return get_session_factory(database_url or get_database_url(get_settings()))
|
database_url: str | None = None,
|
||||||
|
*,
|
||||||
|
settings: Settings | None = None,
|
||||||
|
) -> SessionFactory:
|
||||||
|
active_settings = settings or get_settings()
|
||||||
|
return get_session_factory(database_url or get_database_url(active_settings))
|
||||||
|
|
||||||
|
|
||||||
type SessionFactoryDep = Annotated[SessionFactory, Depends(resolve_session_factory)]
|
type SessionFactoryDep = Annotated[SessionFactory, Depends(resolve_session_factory)]
|
||||||
@@ -40,15 +46,20 @@ async def dispose_session_factory(database_url: str) -> None:
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def session_scope(
|
async def session_scope(
|
||||||
*,
|
*,
|
||||||
|
settings: Settings | None = None,
|
||||||
database_url: str | None = None,
|
database_url: str | None = None,
|
||||||
|
session_factory: SessionFactory | None = None,
|
||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> AsyncGenerator[AsyncSession]:
|
) -> AsyncGenerator[AsyncSession]:
|
||||||
if session is not None:
|
if session is not None:
|
||||||
yield session
|
yield session
|
||||||
return
|
return
|
||||||
|
|
||||||
session_factory = resolve_session_factory(database_url)
|
active_session_factory = session_factory or resolve_session_factory(
|
||||||
async with session_factory() as owned_session:
|
database_url,
|
||||||
|
settings=settings,
|
||||||
|
)
|
||||||
|
async with active_session_factory() as owned_session:
|
||||||
yield owned_session
|
yield owned_session
|
||||||
|
|
||||||
|
|
||||||
@@ -58,9 +69,11 @@ type SessionScopeDep = Annotated[AsyncSession, Depends(session_scope)]
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def transaction_scope(
|
async def transaction_scope(
|
||||||
*,
|
*,
|
||||||
|
settings: Settings | None = None,
|
||||||
database_url: str | None = None,
|
database_url: str | None = None,
|
||||||
session: AsyncSessionTransaction | None = None,
|
session_factory: SessionFactory | None = None,
|
||||||
) -> AsyncGenerator[AsyncSessionTransaction]:
|
session: AsyncSession | AsyncSessionTransaction | None = None,
|
||||||
|
) -> AsyncGenerator[AsyncSession | AsyncSessionTransaction]:
|
||||||
match session:
|
match session:
|
||||||
case AsyncSession() as async_session:
|
case AsyncSession() as async_session:
|
||||||
if not async_session.in_transaction():
|
if not async_session.in_transaction():
|
||||||
@@ -71,8 +84,11 @@ async def transaction_scope(
|
|||||||
yield async_transaction
|
yield async_transaction
|
||||||
return
|
return
|
||||||
|
|
||||||
session_factory = resolve_session_factory(database_url)
|
active_session_factory = session_factory or resolve_session_factory(
|
||||||
async with session_factory().begin() as owned_session:
|
database_url,
|
||||||
|
settings=settings,
|
||||||
|
)
|
||||||
|
async with active_session_factory.begin() as owned_session:
|
||||||
yield owned_session
|
yield owned_session
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,10 @@ class ServiceBase(ABC):
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def _session_scope(self, session: AsyncSession | None = None):
|
async def _session_scope(self, session: AsyncSession | None = None):
|
||||||
"""Provide a transactional scope around a series of operations."""
|
"""Provide a transactional scope around a series of operations."""
|
||||||
async with session_scope(session=session) as active_session:
|
async with session_scope(
|
||||||
|
session_factory=self.session_factory,
|
||||||
|
session=session,
|
||||||
|
) as active_session:
|
||||||
yield active_session
|
yield active_session
|
||||||
|
|
||||||
async def _finalize(
|
async def _finalize(
|
||||||
|
|||||||
Reference in New Issue
Block a user