Skip to content

Commit

Permalink
chore: implement model.wait_for_idle(timeout=n)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Dec 3, 2024
1 parent 322280c commit 2aa4634
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
15 changes: 7 additions & 8 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import stat
import sys
import tempfile
import time
import warnings
import weakref
import zipfile
Expand Down Expand Up @@ -3281,25 +3282,26 @@ async def new_wait_for_idle(
apps = apps or list(self.applications)
idle_times: dict[str, datetime] = {}
units_ready: set[str] = set() # The units that are in the desired state
last_log_time: list[datetime | None] = [None]

start_time = datetime.now()
deadline = None if timeout is None else time.monotonic() + timeout

while True:
if deadline and time.monotonic() > deadline:
raise jasyncio.TimeoutError(
"Timed out waiting for model to become idle"
)

if await self._check_idle(
apps=apps,
raise_on_error=raise_on_error,
raise_on_blocked=raise_on_blocked,
status=status,
wait_for_at_least_units=wait_for_at_least_units,
wait_for_exact_units=wait_for_exact_units,
timeout=timeout,
idle_period=idle_period,
_wait_for_units=_wait_for_units,
idle_times=idle_times,
units_ready=units_ready,
last_log_time=last_log_time,
start_time=start_time,
):
break

Expand All @@ -3314,13 +3316,10 @@ async def _check_idle(
status: str | None,
wait_for_at_least_units: int | None,
wait_for_exact_units: int | None,
timeout: float | None,
idle_period: float,
_wait_for_units: int,
idle_times: dict[str, datetime],
units_ready: set[str],
last_log_time: list[datetime | None],
start_time: datetime,
) -> bool:
now = datetime.now()
expected_idle_since = now - timedelta(seconds=idle_period)
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/test_wait_for_idle.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,10 @@ def kwargs() -> dict[str, Any]:
status=None,
wait_for_at_least_units=None,
wait_for_exact_units=None,
timeout=100,
idle_period=0,
_wait_for_units=1,
idle_times={},
units_ready=set(),
last_log_time=[None],
start_time=datetime.now(),
)


Expand Down

0 comments on commit 2aa4634

Please sign in to comment.