Skip to content

Commit 6b30628

Browse files
author
Sergio García Prado
committed
ISSUE #?
* Improve integration between `ConfigV1` and `PoolFactory`. * Minor improvements.
1 parent 772e330 commit 6b30628

File tree

7 files changed

+63
-22
lines changed

7 files changed

+63
-22
lines changed

packages/core/minos-microservice-aggregate/minos/aggregate/snapshots/repositories/abc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ async def find_entries(
190190
performed to the global snapshot.
191191
:param exclude_deleted: If ``True``, deleted ``RootEntity`` entries are included, otherwise deleted
192192
``RootEntity`` entries are filtered.
193-
:param synchronize: TODO
193+
:param synchronize: If ``True`` a synchronization is performed before processing the query, otherwise the query
194+
is performed without any synchronization step.
194195
:param kwargs: Additional named arguments.
195196
:return: An asynchronous iterator that containing the ``RootEntity`` instances.
196197
"""

packages/core/minos-microservice-common/minos/common/config/v1.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ def _get_saga(self) -> dict[str, Any]:
121121
return saga
122122

123123
def _get_injections(self) -> list[type[InjectableMixin]]:
124+
from ..pools import (
125+
Pool,
126+
PoolFactory,
127+
)
128+
129+
injections = self._get_raw_injections()
130+
injections = [
131+
injection
132+
for injection in injections
133+
if not (issubclass(injection, Pool) or issubclass(injection, PoolFactory))
134+
]
135+
with suppress(MinosConfigException):
136+
pool_factory = self._get_pools().get("factory")
137+
if pool_factory is not None:
138+
# noinspection PyTypeChecker
139+
injections.insert(0, pool_factory)
140+
141+
# noinspection PyTypeChecker
142+
return injections
143+
144+
def _get_raw_injections(self) -> list[type[InjectableMixin]]:
124145
try:
125146
injections = self.get_by_key("service.injections")
126147
if isinstance(injections, dict):
@@ -217,7 +238,31 @@ def _get_services(self) -> list[type]:
217238
return services
218239

219240
def _get_pools(self) -> dict[str, type]:
220-
return dict()
241+
from ..pools import (
242+
Pool,
243+
PoolFactory,
244+
)
245+
246+
factory = next(
247+
(injection for injection in self._get_raw_injections() if issubclass(injection, PoolFactory)), PoolFactory
248+
)
249+
injections = [injection for injection in self._get_raw_injections() if issubclass(injection, Pool)]
250+
if not len(injections):
251+
return dict()
252+
253+
types = dict()
254+
for injection in injections:
255+
if "lock" in injection.__name__.lower():
256+
types["lock"] = injection
257+
elif "database" in injection.__name__.lower():
258+
types["database"] = injection
259+
elif "broker" in injection.__name__.lower():
260+
types["broker"] = injection
261+
262+
return {
263+
"factory": factory,
264+
"types": types,
265+
}
221266

222267
def _get_routers(self) -> list[type]:
223268
try:
@@ -266,7 +311,7 @@ def _get_database_broker(self):
266311
return self._get_database_by_name("broker.queue")
267312

268313
def _get_database_saga(self) -> dict[str, Any]:
269-
raw = self.get_by_key("saga.storage")
314+
raw = self._get_database_by_name("saga.storage")
270315
return raw
271316

272317
def _get_database_event(self) -> dict[str, Any]:

packages/core/minos-microservice-common/minos/common/database/pools.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
from ..config import (
1717
Config,
1818
)
19-
from ..injections import (
20-
Injectable,
21-
)
2219
from ..locks import (
2320
LockPool,
2421
)
@@ -37,7 +34,6 @@
3734
logger = logging.getLogger(__name__)
3835

3936

40-
@Injectable("database_pool")
4137
class DatabaseClientPool(Pool[DatabaseClient]):
4238
"""Database Client Pool class."""
4339

@@ -49,7 +45,6 @@ def __init__(self, client_builder: DatabaseClientBuilder, *args, **kwargs):
4945
@classmethod
5046
def _from_config(cls, config: Config, identifier: Optional[str] = None, **kwargs):
5147
base_builder = config.get_database_by_name(identifier).get("client")
52-
5348
if base_builder is None:
5449
raise ValueError(f"{base_builder!r} is not a {DatabaseClientBuilder!r} instance.")
5550
elif issubclass(base_builder, DatabaseClient):

packages/core/minos-microservice-common/minos/common/locks.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
cached_property,
1515
)
1616

17-
from .injections import (
18-
Injectable,
19-
)
2017
from .pools import (
2118
Pool,
2219
)
@@ -65,6 +62,5 @@ def hashed_key(self) -> int:
6562
return self.key
6663

6764

68-
@Injectable("lock_pool")
6965
class LockPool(Pool[Lock], ABC):
7066
"""Lock Pool class."""

packages/core/minos-microservice-common/tests/test_common/test_config/test_v1/test_base.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Config,
88
ConfigV1,
99
MinosConfigException,
10+
PoolFactory,
1011
)
1112
from tests.utils import (
1213
BASE_PATH,
@@ -51,9 +52,7 @@ def test_name(self):
5152

5253
def test_injections(self):
5354
expected = [
54-
FakeLockPool,
55-
FakeDatabasePool,
56-
FakeBrokerClientPool,
55+
PoolFactory,
5756
FakeHttpConnector,
5857
FakeBrokerPublisher,
5958
FakeBrokerSubscriberBuilder,
@@ -185,7 +184,15 @@ def test_interface_unknown(self):
185184
config.get_interface_by_name("unknown")
186185

187186
def test_pools(self):
188-
self.assertEqual(dict(), self.config.get_pools())
187+
expected = {
188+
"factory": PoolFactory,
189+
"types": {
190+
"lock": FakeLockPool,
191+
"database": FakeDatabasePool,
192+
"broker": FakeBrokerClientPool,
193+
},
194+
}
195+
self.assertEqual(expected, self.config.get_pools())
189196

190197
def test_services(self):
191198
self.assertEqual([float, int], self.config.get_services())

packages/core/minos-microservice-common/tests/utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Injectable,
1313
Lock,
1414
LockPool,
15+
Pool,
1516
Port,
1617
testing,
1718
)
@@ -163,13 +164,11 @@ class FakeBrokerSubscriberBuilder(Builder[FakeBrokerSubscriber]):
163164
FakeBrokerSubscriber.set_builder(FakeBrokerSubscriberBuilder)
164165

165166

166-
@Injectable("database_pool")
167-
class FakeDatabasePool:
167+
class FakeDatabasePool(Pool):
168168
"""For testing purposes."""
169169

170170

171-
@Injectable("broker_pool")
172-
class FakeBrokerClientPool:
171+
class FakeBrokerClientPool(Pool):
173172
"""For testing purposes."""
174173

175174

packages/core/minos-microservice-networks/minos/networks/brokers/pools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from minos.common import (
1616
Config,
17-
Injectable,
1817
Pool,
1918
)
2019

@@ -28,7 +27,6 @@
2827
logger = logging.getLogger(__name__)
2928

3029

31-
@Injectable("broker_pool")
3230
class BrokerClientPool(Pool):
3331
"""Broker Client Pool class."""
3432

0 commit comments

Comments
 (0)