mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-21 17:16:46 +03:00
Fix reverse shutdown order bug in container.shutdown_resources()
This commit is contained in:
parent
f44924f285
commit
aa251a44ba
File diff suppressed because it is too large
Load Diff
|
@ -290,24 +290,19 @@ class DynamicContainer(Container):
|
|||
|
||||
def shutdown_resources(self):
|
||||
"""Shutdown all container resources."""
|
||||
def _no_initialized_dependencies(resource):
|
||||
for related in resource.related:
|
||||
if isinstance(related, providers.Resource) and related.initialized:
|
||||
return False
|
||||
return True
|
||||
|
||||
def _without_initialized_dependencies(resources):
|
||||
return list(filter(_no_initialized_dependencies, resources))
|
||||
|
||||
def _any_initialized(resources):
|
||||
return any(resource.initialized for resource in resources)
|
||||
|
||||
def _any_in_async_mode(resources):
|
||||
return any(resource.is_async_mode_enabled() for resource in resources)
|
||||
def _independent_resources(resources):
|
||||
for resource in resources:
|
||||
for other_resource in resources:
|
||||
if not other_resource.initialized:
|
||||
continue
|
||||
if resource in other_resource.related:
|
||||
break
|
||||
else:
|
||||
yield resource
|
||||
|
||||
async def _async_ordered_shutdown(resources):
|
||||
while _any_initialized(resources):
|
||||
resources_to_shutdown = _without_initialized_dependencies(resources)
|
||||
while any(resource.initialized for resource in resources):
|
||||
resources_to_shutdown = list(_independent_resources(resources))
|
||||
if not resources_to_shutdown:
|
||||
raise RuntimeError('Unable to resolve resources shutdown order')
|
||||
futures = []
|
||||
|
@ -318,15 +313,15 @@ class DynamicContainer(Container):
|
|||
await asyncio.gather(*futures)
|
||||
|
||||
def _sync_ordered_shutdown(resources):
|
||||
while _any_initialized(resources):
|
||||
resources_to_shutdown = _without_initialized_dependencies(resources)
|
||||
while any(resource.initialized for resource in resources):
|
||||
resources_to_shutdown = list(_independent_resources(resources))
|
||||
if not resources_to_shutdown:
|
||||
raise RuntimeError('Unable to resolve resources shutdown order')
|
||||
for resource in resources_to_shutdown:
|
||||
resource.shutdown()
|
||||
|
||||
resources = list(self.traverse(types=[providers.Resource]))
|
||||
if _any_in_async_mode(resources):
|
||||
if any(resource.is_async_mode_enabled() for resource in resources):
|
||||
return _async_ordered_shutdown(resources)
|
||||
else:
|
||||
return _sync_ordered_shutdown(resources)
|
||||
|
|
|
@ -70,15 +70,15 @@ class AsyncResourcesTest(AsyncTestCase):
|
|||
|
||||
self._run(container.shutdown_resources())
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
||||
|
||||
self._run(container.init_resources())
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
||||
|
||||
self._run(container.shutdown_resources())
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1', 'r3', 'r2', 'r1'])
|
||||
|
||||
def test_shutdown_circular_dependencies_breaker(self):
|
||||
async def _resource(name, **_):
|
||||
|
@ -148,12 +148,12 @@ class AsyncResourcesTest(AsyncTestCase):
|
|||
|
||||
self._run(container.shutdown_resources())
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
||||
|
||||
self._run(container.init_resources())
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
||||
|
||||
self._run(container.shutdown_resources())
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1', 'r3', 'r2', 'r1'])
|
||||
|
|
|
@ -233,15 +233,15 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
|||
|
||||
container.shutdown_resources()
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
||||
|
||||
container.init_resources()
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
||||
|
||||
container.shutdown_resources()
|
||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1', 'r3', 'r2', 'r1'])
|
||||
|
||||
def test_shutdown_resources_circular_dependencies_breaker(self):
|
||||
def _resource(name, **_):
|
||||
|
|
Loading…
Reference in New Issue
Block a user