Skip to content

Commit e28e2c1

Browse files
committed
Try making logic clearer
1 parent 612210c commit e28e2c1

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

juju/model.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,7 +2623,7 @@ async def wait_for_idle(self, apps=None, raise_on_error=True, raise_on_blocked=F
26232623
warnings.warn("wait_for_active is deprecated; use status", DeprecationWarning)
26242624
status = "active"
26252625

2626-
_wait_for_units = wait_for_at_least_units if wait_for_at_least_units is not None else 1
2626+
_wait_for_units = wait_for_at_least_units or 1
26272627

26282628
timeout = timedelta(seconds=timeout) if timeout is not None else None
26292629
idle_period = timedelta(seconds=idle_period)
@@ -2657,7 +2657,7 @@ def _raise_for_status(entities, status):
26572657
", ".join(errored),
26582658
))
26592659

2660-
if wait_for_exact_units is not None:
2660+
if wait_for_exact_units:
26612661
assert type(wait_for_exact_units) == int and wait_for_exact_units >= 0, \
26622662
'Invalid value for wait_for_exact_units : %s' % wait_for_exact_units
26632663

@@ -2680,7 +2680,7 @@ def _raise_for_status(entities, status):
26802680
blocks.setdefault("App", []).append(app.name)
26812681

26822682
# Check if wait_for_exact_units flag is used
2683-
if wait_for_exact_units is not None:
2683+
if wait_for_exact_units:
26842684
if len(app.units) != wait_for_exact_units:
26852685
busy.append(app.name + " (waiting for exactly %s units, current : %s)" %
26862686
(wait_for_exact_units, len(app.units)))
@@ -2697,7 +2697,7 @@ def _raise_for_status(entities, status):
26972697
# errors to raise at the end
26982698
break
26992699
for unit in app.units:
2700-
if unit.machine is not None and unit.machine.status == "error":
2700+
if unit.machine and unit.machine.status == "error":
27012701
errors.setdefault("Machine", []).append(unit.machine.id)
27022702
continue
27032703
if unit.agent_status == "error":
@@ -2709,12 +2709,24 @@ def _raise_for_status(entities, status):
27092709
if raise_on_blocked and unit.workload_status == "blocked":
27102710
blocks.setdefault("Unit", []).append(unit.name)
27112711
continue
2712-
waiting_for_a_particular_status = status and unit.workload_status != status
2713-
if not waiting_for_a_particular_status and unit.agent_status == "idle":
2714-
# We'll be here in two cases:
2715-
# 1) We're not waiting for a particular status and the agent is "idle"
2716-
# 2) We're waiting for a particular status and the workload is in that
2717-
# status
2712+
# TODO (cderici): we need two versions of wait_for_idle, one for waiting on
2713+
# individual units, another one for waiting for an application.
2714+
# The convoluted logic below is the result of trying to do both at the same
2715+
# time
2716+
need_to_wait_more_for_a_particular_status = status and (unit.workload_status != status)
2717+
app_is_in_desired_status = (not status) or (app_status == status)
2718+
if not need_to_wait_more_for_a_particular_status and \
2719+
unit.agent_status == "idle" and \
2720+
(wait_for_at_least_units or app_is_in_desired_status):
2721+
# A unit is ready if either:
2722+
# 1) Don't need to wait more for a particular status and the agent is "idle"
2723+
# 2) We're looking for a particular status and the unit's workload,
2724+
# as well as the application, is in that status. If the user wants to
2725+
# see only a particular number of units in that state -- i.e. a subset of
2726+
# the units is needed, then we don't care about the application status
2727+
# (because e.g. app can be in 'waiting' while unit.0 is 'active' and unit.1
2728+
# is 'waiting')
2729+
27182730
# Either way, the unit is ready, start measuring the time period that
27192731
# it needs to stay in that state (i.e. idle_period)
27202732
units_ready.add(unit.name)
@@ -2737,7 +2749,7 @@ def _raise_for_status(entities, status):
27372749
if not busy:
27382750
break
27392751
busy = "\n ".join(busy)
2740-
if timeout is not None and datetime.now() - start_time > timeout:
2752+
if timeout and datetime.now() - start_time > timeout:
27412753
raise jasyncio.TimeoutError("Timed out waiting for model:\n" + busy)
27422754
if last_log_time is None or datetime.now() - last_log_time > log_interval:
27432755
log.info("Waiting for model:\n " + busy)

0 commit comments

Comments
 (0)