Skip to content

Commit

Permalink
Defer on non-existent peer databag
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Aug 7, 2023
1 parent ba5cbf6 commit c882f60
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/relations/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ def _check_for_blocking_relations(self, relation_id: int) -> bool:
return
self.charm.unit.status = ActiveStatus()

def _on_relation_joined_init_checks(self) -> bool:
if not self._check_backend():
# We can't relate an app to the backend database without a backend postgres relation
return False

try:
self.charm.read_pgb_config()
except FileNotFoundError:
wait_str = "waiting for pgbouncer to start"
logger.warning(wait_str)
self.charm.unit.status = WaitingStatus(wait_str)
return False

logger.info(f"Setting up {self.relation_name} relation")
logger.warning(
f"DEPRECATION WARNING - {self.relation_name} is a legacy relation, and will be deprecated in a future release. "
)
return True

def _on_relation_joined(self, join_event: RelationJoinedEvent):
"""Handle db-relation-joined event.
Expand All @@ -205,27 +224,16 @@ def _on_relation_joined(self, join_event: RelationJoinedEvent):
- If password hasn't been added to the databag by this charm, implying that a user
has not been created.
"""
if not self._check_backend():
# We can't relate an app to the backend database without a backend postgres relation
if not self._on_relation_joined_init_checks():
join_event.defer()
return

try:
cfg = self.charm.read_pgb_config()
except FileNotFoundError:
wait_str = "waiting for pgbouncer to start"
logger.warning(wait_str)
self.charm.unit.status = WaitingStatus(wait_str)
remote_app_databag = join_event.relation.data.get(join_event.app)
if remote_app_databag is None:
logger.warning("Defer relation joined: No remote app databag")
join_event.defer()
return

logger.info(f"Setting up {self.relation_name} relation")
logger.warning(
f"DEPRECATION WARNING - {self.relation_name} is a legacy relation, and will be deprecated in a future release. "
)

remote_app_databag = join_event.relation.data[join_event.app]

if self._block_on_extensions(join_event.relation, remote_app_databag):
return

Expand Down

0 comments on commit c882f60

Please sign in to comment.