Skip to content

Commit dfcafd5

Browse files
authored
use-diff-sessions (#380)
1 parent 52ba912 commit dfcafd5

File tree

3 files changed

+16
-39
lines changed

3 files changed

+16
-39
lines changed

.github/workflows/license-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
with:
5151
requirements: "backend/requirements-all.txt"
5252
fail: "Copyleft,Other,Error"
53-
exclude: '(envier.*0\.5\.0|psycopg2.*2\.9\.3|fqdn.*1\.5\.1|pyzmq.*25\.1\.2|debugpy.*1\.6\.7|certifi.*2024\.8\.30|tqdm.*4\.66\..*|webencodings.*0\.5\.1|torch.*1\.10\.2.*|torch.*1\.11\.0.*|pytorch-ignite.*0\.4\.10.*|torchaudio.*0\.11\.0.*|torchvision.*0\.12\.0.*|terminado.*0\.15\.0|qudida.*0\.0\.4|expiringdict.*1\.2\.2|botocore.*1\.29\.80|orderedmultidict.*1\.0\.1|deepchecks.*)'
53+
exclude: '(envier.*0\.5\.0|psycopg2.*2\.9\.3|fqdn.*1\.5\.1|pyzmq.*25\.1\.2|debugpy.*1\.6\.7|certifi.*2024\.8\.30|tqdm.*4\.67\..*|webencodings.*0\.5\.1|torch.*1\.10\.2.*|torch.*1\.11\.0.*|pytorch-ignite.*0\.4\.10.*|torchaudio.*0\.11\.0.*|torchvision.*0\.12\.0.*|terminado.*0\.15\.0|qudida.*0\.0\.4|expiringdict.*1\.2\.2|botocore.*1\.29\.80|orderedmultidict.*1\.0\.1|deepchecks.*)'
5454
# psycopg2 is LGPL 2
5555
# pyzmq is Revised BSD https://github.com/zeromq/pyzmq/blob/main/examples/LICENSE
5656
# debugpy is MIT https://github.com/microsoft/debugpy/blob/main/LICENSE

backend/deepchecks_monitoring/resources.py

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from sqlalchemy import select
2727
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
2828
from sqlalchemy.future.engine import Engine, create_engine
29-
from sqlalchemy.orm import Session, sessionmaker
29+
from sqlalchemy.orm import Session
3030

3131
from deepchecks_monitoring import config
3232
from deepchecks_monitoring.features_control import FeaturesControl
@@ -79,9 +79,7 @@ class ResourcesProvider(BaseResourcesProvider):
7979
def __init__(self, settings: config.BaseSettings):
8080
self._settings = settings
8181
self._database_engine: t.Optional[Engine] = None
82-
self._session_factory: t.Optional[sessionmaker] = None
8382
self._async_database_engine: t.Optional[AsyncEngine] = None
84-
self._async_session_factory: t.Optional[sessionmaker] = None
8583
self._kafka_admin: t.Optional[KafkaAdminClient] = None
8684
self._redis_client: t.Optional[Redis] = None
8785
self._cache_funcs: t.Optional[CacheFunctions] = None
@@ -147,15 +145,11 @@ def settings(self) -> config.Settings:
147145

148146
def dispose_resources(self):
149147
"""Dispose resources."""
150-
if self._session_factory is not None:
151-
self._session_factory.close_all()
152148
if self._database_engine is not None:
153149
self._database_engine.dispose()
154150

155151
async def async_dispose_resources(self):
156152
"""Dispose async resources."""
157-
# if self._async_session_factory is not None:
158-
# await AsyncSession.close_all()
159153
if self._async_database_engine is not None:
160154
await self._async_database_engine.dispose()
161155

@@ -180,23 +174,15 @@ def database_engine(self) -> Engine:
180174

181175
return self._database_engine
182176

183-
@property
184-
def session_factory(self) -> sessionmaker:
185-
"""Return alchemy session factory."""
186-
if self._session_factory is None:
187-
self._session_factory = sessionmaker(
188-
self.database_engine,
189-
# class_=ExtendedAsyncSession, # TODO:
190-
autoflush=False,
191-
expire_on_commit=False,
192-
autocommit=False,
193-
)
194-
return self._session_factory
195-
196177
@contextmanager
197178
def create_database_session(self) -> t.Iterator[Session]:
198179
"""Create sqlalchemy database session."""
199-
with self.session_factory() as session: # pylint: disable=not-callable
180+
with Session(
181+
self.database_engine,
182+
autoflush=False,
183+
expire_on_commit=False,
184+
autocommit=False,
185+
) as session: # pylint: disable=not-callable
200186
try:
201187
yield session
202188
session.commit()
@@ -225,19 +211,6 @@ def async_database_engine(self) -> AsyncEngine:
225211
)
226212
return self._async_database_engine
227213

228-
@property
229-
def async_session_factory(self) -> sessionmaker:
230-
"""Return async alchemy session maker."""
231-
if self._async_session_factory is None:
232-
self._async_session_factory = sessionmaker(
233-
self.async_database_engine,
234-
class_=ExtendedAsyncSession,
235-
autoflush=False,
236-
expire_on_commit=False,
237-
autocommit=False,
238-
)
239-
return self._async_session_factory
240-
241214
@t.overload
242215
def create_async_database_session(
243216
self,
@@ -260,8 +233,12 @@ async def create_async_database_session(
260233
organization_id: t.Optional[int] = None
261234
) -> t.AsyncIterator[t.Optional[ExtendedAsyncSession]]:
262235
"""Create async sqlalchemy database session."""
263-
async with self.async_session_factory() as session: # pylint: disable=not-callable
264-
session: ExtendedAsyncSession
236+
async with ExtendedAsyncSession(
237+
self.async_database_engine,
238+
autoflush=False,
239+
expire_on_commit=False,
240+
autocommit=False,
241+
) as session: # pylint: disable=not-callable
265242
try:
266243
if organization_id:
267244
organization_schema = await session.scalar(

backend/tests/logic/test_cache_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def test_clear_monitor_cache(resources_provider):
3737

3838

3939
@pytest.mark.asyncio
40-
async def test_delete_monitor_cache_by_timestamp(resources_provider):
40+
async def test_delete_monitor_cache_by_timestamp(resources_provider, async_session):
4141
cache_funcs: CacheFunctions = resources_provider.cache_functions
4242

4343
# Arrange - Organization with 2 monitors and 2 model versions, and another organization with same monitor id.
@@ -60,7 +60,7 @@ async def test_delete_monitor_cache_by_timestamp(resources_provider):
6060
cache_funcs.add_invalidation_timestamps(1, 1, timestamps_to_invalidate)
6161

6262
# Act - run task
63-
async with resources_provider.async_session_factory() as session:
63+
async with async_session as session:
6464
task_id = await insert_model_version_cache_invalidation_task(1, 1, session=session)
6565
task = await session.scalar(select(Task).where(Task.id == task_id))
6666
await ModelVersionCacheInvalidation().run(task, session, resources_provider, lock=None)

0 commit comments

Comments
 (0)