Skip to content

Commit db4ec41

Browse files
Add self._reconcile_allowed instead of exiting
1 parent 299cb22 commit db4ec41

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/abstract_charm.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class MySQLRouterCharm(ops.CharmBase, abc.ABC):
6161
_READ_ONLY_X_PORT = 6449
6262

6363
refresh: charm_refresh.Common
64+
# Whether `reconcile` method is allowed to run
65+
# `False` if `charm_refresh.UnitTearingDown` or `charm_refresh.PeerRelationNotReady` raised
66+
# Most of the charm code should not run if either of those exceptions is raised
67+
# However, some charm libs (i.e. data-platform-libs) will break if they do not receive every
68+
# event they expect (e.g. relation-created)
69+
_reconcile_allowed: bool
6470

6571
def __init__(self, *args) -> None:
6672
super().__init__(*args)
@@ -285,6 +291,9 @@ def _update_endpoints(self) -> None:
285291

286292
def reconcile(self, event=None) -> None: # noqa: C901
287293
"""Handle most events."""
294+
if not self._reconcile_allowed:
295+
logger.debug("Reconcile not allowed")
296+
return
288297
workload_ = self.get_workload(event=event)
289298
logger.debug(
290299
"State of reconcile "

src/charm.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ def __init__(self, *args) -> None:
9595
# unit(s) tear down
9696
self.unit.status = ops.MaintenanceStatus("Tearing down")
9797
snap.uninstall()
98-
exit()
98+
self._reconcile_allowed = False
9999
except charm_refresh.PeerRelationNotReady:
100100
self.unit.status = ops.MaintenanceStatus("Waiting for peer relation")
101101
if self.unit.is_leader():
102102
self.app.status = ops.MaintenanceStatus("Waiting for peer relation")
103-
exit()
103+
self._reconcile_allowed = False
104+
else:
105+
self._reconcile_allowed = True
104106

105107
@property
106108
def _subordinate_relation_endpoint_names(self) -> typing.Optional[typing.Iterable[str]]:

0 commit comments

Comments
 (0)