@@ -345,6 +345,26 @@ def is_cluster_initialised(self) -> bool:
345345 """Returns whether the cluster is already initialised."""
346346 return "cluster_initialised" in self .app_peer_data
347347
348+ @property
349+ def is_cluster_restoring_backup (self ) -> bool :
350+ """Returns whether the cluster is restoring a backup."""
351+ return "restoring-backup" in self .app_peer_data
352+
353+ @property
354+ def is_cluster_restoring_to_time (self ) -> bool :
355+ """Returns whether the cluster is restoring a backup to a specific time."""
356+ return "restore-to-time" in self .app_peer_data
357+
358+ @property
359+ def is_unit_departing (self ) -> bool :
360+ """Returns whether the unit is departing."""
361+ return "departing" in self .unit_peer_data
362+
363+ @property
364+ def is_unit_stopped (self ) -> bool :
365+ """Returns whether the unit is stopped."""
366+ return "stopped" in self .unit_peer_data
367+
348368 @property
349369 def postgresql (self ) -> PostgreSQL :
350370 """Returns an instance of the object used to interact with the database."""
@@ -452,9 +472,9 @@ def _on_peer_relation_departed(self, event: RelationDepartedEvent) -> None:
452472 if not self .unit .is_leader ():
453473 return
454474
455- if "cluster_initialised" not in self ._peers . data [
456- self .app
457- ] or not self . _updated_synchronous_node_count ( len ( self . _units_ips ) ):
475+ if not self . is_cluster_initialised or not self ._updated_synchronous_node_count (
476+ len ( self ._units_ips )
477+ ):
458478 logger .debug ("Deferring on_peer_relation_departed: cluster not initialized" )
459479 event .defer ()
460480 return
@@ -662,7 +682,7 @@ def has_raft_keys(self):
662682 def _peer_relation_changed_checks (self , event : HookEvent ) -> bool :
663683 """Split of to reduce complexity."""
664684 # Prevents the cluster to be reconfigured before it's bootstrapped in the leader.
665- if "cluster_initialised" not in self ._peers . data [ self . app ] :
685+ if not self .is_cluster_initialised :
666686 logger .debug ("Deferring on_peer_relation_changed: cluster not initialized" )
667687 event .defer ()
668688 return False
@@ -710,7 +730,7 @@ def _on_peer_relation_changed(self, event: HookEvent):
710730 return
711731
712732 if (
713- "restoring-backup" in self .app_peer_data or "restore-to-time" in self .app_peer_data
733+ self .is_cluster_restoring_backup or self .is_cluster_restoring_to_time
714734 ) and not self ._was_restore_successful ():
715735 logger .debug ("on_peer_relation_changed early exit: Backup restore check failed" )
716736 return
@@ -1127,7 +1147,7 @@ def _on_leader_elected(self, event: LeaderElectedEvent) -> None:
11271147
11281148 # Don't update connection endpoints in the first time this event run for
11291149 # this application because there are no primary and replicas yet.
1130- if "cluster_initialised" not in self ._peers . data [ self . app ] :
1150+ if not self .is_cluster_initialised :
11311151 logger .debug ("Early exit on_leader_elected: Cluster not initialized" )
11321152 return
11331153
@@ -1403,7 +1423,7 @@ def _start_primary(self, event: StartEvent) -> None:
14031423
14041424 def _start_replica (self , event ) -> None :
14051425 """Configure the replica if the cluster was already initialised."""
1406- if "cluster_initialised" not in self ._peers . data [ self . app ] :
1426+ if not self .is_cluster_initialised :
14071427 logger .debug ("Deferring on_start: awaiting for cluster to start" )
14081428 self .unit .status = WaitingStatus ("awaiting for cluster to start" )
14091429 event .defer ()
@@ -1541,7 +1561,7 @@ def _on_update_status(self, _) -> None:
15411561 return
15421562
15431563 if (
1544- "restoring-backup" in self .app_peer_data or "restore-to-time" in self .app_peer_data
1564+ self .is_cluster_restoring_backup or self .is_cluster_restoring_to_time
15451565 ) and not self ._was_restore_successful ():
15461566 return
15471567
@@ -1566,7 +1586,7 @@ def _on_update_status(self, _) -> None:
15661586 self ._observer .start_observer ()
15671587
15681588 def _was_restore_successful (self ) -> bool :
1569- if "restore-to-time" in self .app_peer_data and all (self .is_pitr_failed ()):
1589+ if self .is_cluster_restoring_to_time and all (self .is_pitr_failed ()):
15701590 logger .error (
15711591 "Restore failed: database service failed to reach point-in-time-recovery target. "
15721592 "You can launch another restore with different parameters"
@@ -1623,7 +1643,7 @@ def _was_restore_successful(self) -> bool:
16231643 return True
16241644
16251645 def _can_run_on_update_status (self ) -> bool :
1626- if "cluster_initialised" not in self ._peers . data [ self . app ] :
1646+ if not self .is_cluster_initialised :
16271647 return False
16281648
16291649 if self .has_raft_keys ():
0 commit comments