Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Dec 6, 2024
1 parent d71f65c commit 6e8ec77
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3282,6 +3282,7 @@ async def new_wait_for_idle(
)
apps = apps or list(self.applications)
idle_times: dict[str, datetime] = {}
xidle_times: dict[str, datetime] = {}

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

Expand All @@ -3290,7 +3291,7 @@ async def new_wait_for_idle(
expected_idle_since = xnow - timedelta(seconds=idle_period)
full_status = await self.get_status()

rv, _ustat = _check_idle(
rv, ustat = _check_idle(
full_status,
apps=apps,
raise_on_error=raise_on_error,
Expand All @@ -3304,6 +3305,55 @@ async def new_wait_for_idle(
now=xnow,
expected_idle_since=expected_idle_since,
)

if rv:
assert ustat

if ustat:
xrv = True

for name in ustat.units:
xidle_times.setdefault(name, xnow)

for name in ustat.busy_units:
xidle_times[name] = xnow

for app_name in apps:
ready_units = [
n for n in ustat.ready_units if n.startswith(f"{app_name}/")
]
if (
wait_for_exact_units is None
and len(ready_units) < _wait_for_units
):
logger.info(
"Waiting for app %r units %s >= %s",
app_name,
len(ustat.ready_units),
_wait_for_units,
)
xrv = False

if (
wait_for_exact_units is not None
and len(ready_units) != wait_for_exact_units
):
logger.info(
"Waiting for app %r units %s == %s",
app_name,
len(ready_units),
wait_for_exact_units,
)
xrv = False

if busy := [
n for n, t in xidle_times.items() if expected_idle_since < t
]:
logger.info("Waiting for %s to be idle enough", busy)
xrv = False

assert xrv == rv

if rv:
break

Expand Down Expand Up @@ -3421,7 +3471,7 @@ def _check_idle(

if wait_for_exact_units is None and len(ready_units) < _wait_for_units:
logger.info(
"Waiting for app %r units %s/%s",
"Waiting for app %r units %s >= %s",
app_name,
len(ready_units),
_wait_for_units,
Expand All @@ -3433,10 +3483,10 @@ def _check_idle(
and len(ready_units) != wait_for_exact_units
):
logger.info(
"Waiting for app %r units %s/%s",
"Waiting for app %r units %s == %s",
app_name,
len(ready_units),
_wait_for_units,
wait_for_exact_units,
)
return False, rv

Expand Down

0 comments on commit 6e8ec77

Please sign in to comment.