Skip to content

Commit eaafb51

Browse files
author
Sergio García Prado
committed
* Fix bugs related with concurrency on EntrypointLauncher.launch.
1 parent 32223e7 commit eaafb51

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import warnings
77
from asyncio import (
88
AbstractEventLoop,
9+
gather,
910
)
1011
from enum import (
1112
Enum,
@@ -126,8 +127,7 @@ def launch(self) -> NoReturn:
126127

127128
exception = None
128129
try:
129-
self.loop.run_until_complete(self.setup())
130-
self.loop.run_until_complete(self.entrypoint.__aenter__())
130+
self.graceful_launch()
131131
logger.info("Microservice is up and running!")
132132
self.loop.run_forever()
133133
except KeyboardInterrupt as exc: # pragma: no cover
@@ -138,13 +138,19 @@ def launch(self) -> NoReturn:
138138
finally:
139139
self.graceful_shutdown(exception)
140140

141+
def graceful_launch(self) -> None:
142+
"""Launch the execution gracefully.
143+
144+
:return: This method does not return anything.
145+
"""
146+
self.loop.run_until_complete(gather(self.setup(), self.entrypoint.__aenter__()))
147+
141148
def graceful_shutdown(self, err: Exception = None) -> None:
142149
"""Shutdown the execution gracefully.
143150
144151
:return: This method does not return anything.
145152
"""
146-
self.loop.run_until_complete(self.entrypoint.graceful_shutdown(err))
147-
self.loop.run_until_complete(self.destroy())
153+
self.loop.run_until_complete(gather(self.entrypoint.__aexit__(None, err, None), self.destroy()))
148154

149155
@cached_property
150156
def entrypoint(self) -> Entrypoint:

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ def __init__(self, *args, maxsize: int = 10, recycle: Optional[int] = 300, alrea
3737

3838
# noinspection PyUnresolvedReferences
3939
async def __acquire(self) -> Any: # pragma: no cover
40-
# FIXME: This method inheritance should be improved.
41-
4240
if self._instances.empty() and not self._semaphore.locked():
4341
await self._PoolBase__create_new_instance()
4442

@@ -56,8 +54,14 @@ async def __acquire(self) -> Any: # pragma: no cover
5654
return await self._PoolBase__acquire()
5755

5856
self._used.add(instance)
57+
logger.debug(f"Acquired instance: {instance!r}")
5958
return instance
6059

60+
# noinspection PyUnresolvedReferences
61+
async def __release(self, instance: Any) -> Any: # pragma: no cover
62+
await self._PoolBase__release(instance)
63+
logger.debug(f"Released instance: {instance!r}")
64+
6165
def acquire(self, *args, **kwargs) -> P:
6266
"""Acquire a new instance wrapped on an asynchronous context manager.
6367
@@ -66,7 +70,7 @@ def acquire(self, *args, **kwargs) -> P:
6670
:return: An asynchronous context manager.
6771
"""
6872
# noinspection PyUnresolvedReferences
69-
return ContextManager(self.__acquire, self._PoolBase__release)
73+
return ContextManager(self.__acquire, self.__release)
7074

7175
async def _destroy(self) -> None:
7276
if len(self._used):

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async def start(self) -> None:
2323
return await self._start()
2424
except Exception as exc:
2525
logger.exception(f"Raised an exception on {self!r} start: {exc!r}")
26+
raise exc
2627

2728
@abstractmethod
2829
def _start(self):
@@ -38,6 +39,7 @@ async def stop(self, err: Exception = None) -> None:
3839
return await self._stop(err)
3940
except Exception as exc:
4041
logger.exception(f"Raised an exception on {self!r} stop: {exc!r}")
42+
raise exc
4143

4244
@abstractmethod
4345
async def _stop(self, err: Exception = None) -> None:

packages/core/minos-microservice-common/tests/test_common/test_ports.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ async def test_start_raises(self):
4747
mock = AsyncMock(side_effect=ValueError)
4848
port._start = mock
4949

50-
await port.start()
50+
with self.assertRaises(ValueError):
51+
await port.start()
5152

5253
self.assertEqual([call()], mock.call_args_list)
5354

@@ -67,7 +68,8 @@ async def test_stop_raises(self):
6768
mock = AsyncMock(side_effect=ValueError)
6869
port._stop = mock
6970

70-
await port.stop()
71+
with self.assertRaises(ValueError):
72+
await port.stop()
7173

7274
self.assertEqual([call(None)], mock.call_args_list)
7375

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, *args, **kwargs):
2424
async def __aenter__(self):
2525
"""For testing purposes."""
2626

27-
async def graceful_shutdown(*args, **kwargs):
27+
async def __aexit__(self, exc_type, exc_val, exc_tb):
2828
"""For testing purposes."""
2929

3030

0 commit comments

Comments
 (0)