Skip to content

Commit

Permalink
Defer _on_change
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Feb 7, 2024
1 parent 6427c8f commit 12cc43a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
24 changes: 15 additions & 9 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,18 +663,24 @@ def _get_relation_databases(self, filter_relation=None) -> List[Dict[str, str]]:
}
)

for relation in self.model.relations.get(CLIENT_RELATION_NAME, []):
database = self.client_relation.get_database(relation)
for rel_id, data in self.client_relation.database_provides.fetch_relation_data(
fields=["database", "extra-user-roles"]
).items():
database = data.get("database")
roles = data.get("extra-user-roles", "").lower().split(",")
admin = None
if "admin" in roles or "superuser" in roles:
admin = f"relation_id_{rel_id}"
if database and relation.id != filter_relation:
# TODO new rel admins
db_list = database.split(",")
for db in db_list:
databases.append(
{
"name": db,
"legacy": False,
}
)
record = {
"name": db,
"legacy": False,
}
if admin:
record["admin"] = admin
databases.append(record)
return databases

def _get_relation_config(
Expand Down
7 changes: 5 additions & 2 deletions src/relations/backend_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
Relation,
WaitingStatus,
)
from ops.pebble import ConnectionError
from ops.pebble import ConnectionError, PathError

from constants import (
APP_SCOPE,
Expand Down Expand Up @@ -349,7 +349,10 @@ def _on_relation_broken(self, event: RelationBrokenEvent):
logging.info("exiting relation-broken hook - nothing to do")
return

self.charm.delete_file(f"{PGB_DIR}/userlist.txt")
try:
self.charm.delete_file(f"{PGB_DIR}/userlist.txt")
except PathError:
logger.warning("Cannot delete userlist.txt")
if self.charm.unit.is_leader():
self.charm.remove_secret(APP_SCOPE, AUTH_FILE_DATABAG_KEY)

Expand Down
3 changes: 2 additions & 1 deletion src/relations/peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ def _on_changed(self, event: HookEvent):
self.update_leader()

if not self.charm.is_container_ready:
logger.debug("_on_peer_changed early exit: container unavailable")
logger.debug("_on_peer_changed defer: container unavailable")
event.defer()
return

self.charm.render_pgb_config(reload_pgbouncer=True)
Expand Down

0 comments on commit 12cc43a

Please sign in to comment.