Relation still in relations in 'relation-broken' event

hello,

I am trying to signal to my charm when there are no more relations on the relation. Currently, I am emitting an event on ‘relation-broken’ if the number of relations are < 1. The event will never emit though, because even when the last application leaves the relation, the relation is still in the list of relations.

 81     def _on_relation_broken(self, event):
 82         """Emit slurmd_unavailable if there are no remaining relations."""
 83         if len(self.framework.model.relations['slurmd']) < 1:
 84             self._state.slurmd_acquired = False
 85             self.on.slurmd_unavailable.emit()

My findings are such that the relation is still in the relation data in the ‘relation-broken’ event. My question is, is this intended?

Thank you!

I’m not positive, but I think the issue is that when the hook fires, it is the last event that happens while the relation exists. And once the hook exits is when the relation is actually destroyed.

I think the fact that you are getting broken is sufficient to state that acquired is False. If you want to handle the fact that you might have had 2+ established relations, then you can do:

# this is the last related application
if len(self.framework.model.relation['slurmd']) <= 1:
  ...
1 Like

This is what I ended up going with. I though it was worth bringing up for clarity though.

Thank you for your insight.