question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to run observed event ONLY on Juju leader

See original GitHub issue

Reasoning

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:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PietroPasotticommented, Jun 9, 2022

Something I’d not be ‘too opposed to’ is this pattern:

# (in __init__):
if self.unit.is_leader():
    self.framework.observe(foo, self._foo_if_leader)
else: 
    self.framework.observe(foo, self._foo_if_not_leader)

# regardless:
self.framework.observe(bar, self._on_bar)

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…

0reactions
marcoppenheimercommented, Jun 9, 2022

The event.unit attribute (which is only on relation events) refers to the remote application unit that triggered the event. You would probably want kwargs["event"].framework.model.unit.is_leader(). @PietroPasotti and others have spent some time thinking about similar needs/issues. We’ll keep this suggestion on our radar - it’s a good thought.

Yeah it was a crappy solution, decorator probably wouldn’t work, but overall I hope the intent was clear.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found