Add ability to run observed event ONLY on Juju leader
See original GitHub issueReasoning
It’s very common to have the first part of any event handler to be if not self.charm.unit.is_leader(): return
Usually this is because the handler sets app data, but there can be other reasons why you might want to only run on leader.
Although it’s small, it would be nice to have some functionality to avoid this.
(IDEA) - Maybe a decorator?
I have a pretty crappy decorator that I wrote that looks roughly like the following:
def leader_check(func: Callable) -> Any:
def check_unit_leader(*args, **kwargs):
if not kwargs["event"].unit.is_leader():
return
else:
return func(*args, **kwargs)
return check_unit_leader
@leader_check
def _on_relation_joined(self, event):
set_app_data()
but that doesn’t run on every event. It would be nice if we could reduce the leader-checks overall in some way.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Leader (`ops.model.Unit.is_leader`) - Juju
When a new application leader is elected, the Juju controller emits a leader-elected event at least once to the newly elected leader only....
Read more >Bug #1853055 ""ERROR could not determine leader” but `juju ...
I'm not able to run actions on the leader of an application that is scaled out: (mojo-stg-ua-event-bus)stg-ua-event-bus@wendigo:~$ juju ...
Read more >Charm hooks - doc - Charmhub - Juju Discourse
When a unit running a given charm participates in a given relation, it runs at least three hooks for every remote unit it...
Read more >juju - PyPI
Python library for Juju. ... INFO) # Run the deploy coroutine in an asyncio event loop, using a helper ... Add ability to...
Read more >Appendix P: Managing Power Events - OpenStack Zed
Once your OpenStack cloud is deployed and in production you will need to consider how to manage applications in terms of shutting them...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Something I’d not be ‘too opposed to’ is this pattern:
If you have some events (E.g. relation stuff) you really only care about if you’re a leader…
Regarding the fact that leader/nonleader is a common branching point in charms, yeah… I had a half baked design on ‘branching charms’ which I might need to revisit…
Yeah it was a crappy solution, decorator probably wouldn’t work, but overall I hope the intent was clear.