Skip to content

Commit

Permalink
fix: status checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu1nness committed Nov 28, 2024
1 parent 1d404f3 commit e94e6e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
18 changes: 17 additions & 1 deletion single_kernel_mongo/events/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
UpgradeInProgressError,
)
from single_kernel_mongo.lib.charms.data_platform_libs.v0.data_interfaces import DatabaseProvides
from single_kernel_mongo.utils.event_helpers import defer_event_with_info_log

if TYPE_CHECKING:
from single_kernel_mongo.core.operator import OperatorProtocol
Expand Down Expand Up @@ -64,6 +65,9 @@ def _on_relation_event(self, event: RelationEvent):
"""
relation_departing = False
relation_changed = False
# TODO : Handle the Mongos VM case
# (https://github.com/canonical/mongos-operator/blob/6/edge/lib/charms/mongos/v0/mongos_client_interface.py)
# for interface unification.
try:
if not self.pass_hook_checks(event):
logger.info(f"Skipping {type(event)}: Hook checks did not pass")
Expand All @@ -79,7 +83,19 @@ def _on_relation_event(self, event: RelationEvent):
if isinstance(event, RelationBrokenEvent):
relation_departing = True
# TODO: Checks

if not self.dependent.state.has_departed_run(event.relation.id):
defer_event_with_info_log(
logger,
event,
"relation broken",
"must wait for relation departed hook to decide if relation should be removed.",
)
return
if self.dependent.state.is_scaling_down(event.relation.id):
logger.info(
"Relation broken event due to scale down, do not proceed to remove users."
)
return
logger.info("Relation broken event due to relation removal, proceed to remove user.")
if isinstance(event, RelationChangedEvent):
relation_changed = True
Expand Down
14 changes: 10 additions & 4 deletions single_kernel_mongo/managers/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from single_kernel_mongo.workload.mongodb_workload import MongoDBWorkload

if TYPE_CHECKING:
from single_kernel_mongo.abstract_charm import AbstractMongoCharm
from single_kernel_mongo.core.operator import OperatorProtocol

logger = logging.getLogger(__name__)

Expand All @@ -53,13 +53,13 @@ class MongoManager(Object):

def __init__(
self,
charm: AbstractMongoCharm,
dependent: OperatorProtocol,
workload: MongoDBWorkload,
state: CharmState,
substrate: Substrates,
) -> None:
super().__init__(parent=charm, key="managers")
self.charm = charm
super().__init__(parent=dependent, key="managers")
self.charm = dependent.charm
self.workload = workload
self.state = state
self.substrate = substrate
Expand Down Expand Up @@ -179,6 +179,8 @@ def update_diff(self, relation: Relation):
Args:
relation: The relation to update the databag from.
"""
if not self.charm.unit.is_leader():
return
data_interface = DatabaseProviderData(
self.model,
relation.name,
Expand Down Expand Up @@ -274,6 +276,10 @@ def remove_user(self, relation: Relation):

def update_app_relation_data(self, relation: Relation) -> None:
"""Helper function to update this application relation data."""
if not self.charm.unit.is_leader():
return
if not self.state.db_initialised:
return
data_interface = DatabaseProviderData(self.model, relation.name)
if not data_interface.fetch_relation_field(relation.id, "database"):
return
Expand Down
2 changes: 1 addition & 1 deletion single_kernel_mongo/managers/mongodb_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, charm: AbstractMongoCharm):
# Managers
self.backup_manager = BackupManager(self.charm, self.substrate, self.state, container)
self.tls_manager = TLSManager(self, self.workload, self.state, self.substrate)
self.mongo_manager = MongoManager(self.charm, self.workload, self.state, self.substrate)
self.mongo_manager = MongoManager(self, self.workload, self.state, self.substrate)

# Event Handlers
self.password_actions = PasswordActionEvents(self)
Expand Down

0 comments on commit e94e6e0

Please sign in to comment.