X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Fjuju_watcher.py;h=e122786ccf7e33785a69a933585957b031a876fe;hp=842e990b9295e7ecb990801f7df630a83fb1eb87;hb=refs%2Fchanges%2F33%2F9733%2F15;hpb=c38a696d168531e3c067451044262ef4d78ef11f diff --git a/n2vc/juju_watcher.py b/n2vc/juju_watcher.py index 842e990..e122786 100644 --- a/n2vc/juju_watcher.py +++ b/n2vc/juju_watcher.py @@ -48,9 +48,43 @@ def entity_ready(entity: ModelEntity) -> bool: class JujuModelWatcher: + @staticmethod + async def wait_for_model(model: Model, timeout: float = 3600): + """ + Wait for all entities in model to reach its final state. + + :param: model: Model to observe + :param: timeout: Timeout for the model applications to be active + + :raises: asyncio.TimeoutError when timeout reaches + """ + + if timeout is None: + timeout = 3600.0 + + # Coroutine to wait until the entity reaches the final state + wait_for_entity = asyncio.ensure_future( + asyncio.wait_for( + model.block_until( + lambda: all( + entity_ready(entity) for entity in model.applications.values() + ) + ), + timeout=timeout, + ) + ) + + tasks = [wait_for_entity] + try: + await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) + finally: + # Cancel tasks + for task in tasks: + task.cancel() + @staticmethod async def wait_for( - model, + model: Model, entity: ModelEntity, progress_timeout: float = 3600, total_timeout: float = 3600, @@ -103,8 +137,6 @@ class JujuModelWatcher: # Execute tasks, and stop when the first is finished # The watcher task won't never finish (unless it timeouts) await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) - except Exception as e: - raise e finally: # Cancel tasks for task in tasks: