Skip to content

Commit

Permalink
Define charm is_unit_stopped property
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclert-canonical committed Jan 30, 2025
1 parent cfce9de commit b339193
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ def is_unit_departing(self) -> bool:
"""Returns whether the unit is departing."""
return "departing" in self.unit_peer_data

@property
def is_unit_stopped(self) -> bool:
"""Returns whether the unit is stopped."""
return "stopped" in self.unit_peer_data

@property
def postgresql(self) -> PostgreSQL:
"""Returns an instance of the object used to interact with the database."""
Expand Down Expand Up @@ -1437,7 +1442,7 @@ def _on_update_status(self, _) -> None:
if (
not self.is_cluster_restoring_backup
and not self.is_cluster_restoring_to_time
and "stopped" not in self.unit_peer_data
and not self.is_unit_stopped
and services[0].current != ServiceStatus.ACTIVE
):
logger.warning(
Expand Down
9 changes: 3 additions & 6 deletions src/relations/async_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,11 @@ def _on_async_relation_changed(self, event: RelationChangedEvent) -> None:
if not self._stop_database(event):
return

if not all(
if not (self.charm.is_unit_stopped or self._is_following_promoted_cluster()) or not all(
"stopped" in self.charm._peers.data[unit]
or self.charm._peers.data[unit].get("unit-promoted-cluster-counter")
== self._get_highest_promoted_cluster_counter_value()
for unit in {*self.charm._peers.units, self.charm.unit}
for unit in self.charm._peers.units
):
self.charm.unit.status = WaitingStatus(
"Waiting for the database to be stopped in all units"
Expand Down Expand Up @@ -692,10 +692,7 @@ def _set_app_status(self) -> None:

def _stop_database(self, event: RelationChangedEvent) -> bool:
"""Stop the database."""
if (
"stopped" not in self.charm._peers.data[self.charm.unit]
and not self._is_following_promoted_cluster()
):
if not self.charm.is_unit_stopped and not self._is_following_promoted_cluster():
if not self.charm.unit.is_leader() and not self.container.exists(POSTGRESQL_DATA_PATH):
logger.debug("Early exit on_async_relation_changed: following promoted cluster.")
return False
Expand Down

0 comments on commit b339193

Please sign in to comment.