Is a set state called multiple times?
alex.kavanagh at canonical.com
Thu Jul 27 08:13:00 UTC 2017
On Thu, Jul 27, 2017 at 2:37 AM, fengxia <fxia1 at lenovo.com> wrote:
> Hi Juju,
> Once I set a state, set_state("here"), I want to make sure its @when will
> only be executed ONCE (when "here" from False->True).
> So my thought is to remove_state("here") in its @when("here") code block.
> If I don't, will this @when be called multiple times if I don't reset this
> state? What's the good practice here?
You have a couple of options here depending on the nature of the handler.
1. If, in the lifetime of the unit's existence, the handler only has to
execute ONCE. (and I mean EVER), then there is a @only_once decorator that
can be used. It can be used in combination with other decorators to set up
a condition, but it guarantees that the handler will only be called once.
However, what you probably want is ...
2. Use a @when_not('flag') and then set it the 'flag' in the body of the
The first would look something like:
... do something once ...
The second treats a flag as a 'have I done this yet' condition, and allows
you to reset the flag at some other point in the charm's life cycle so that
you can do it again. 'installed' is a good example of this:
... do the installation ...
# when it is fully successful, set the installed-something flag. Don't
set it early as
# if it errors, a future handler invocation may be able to continue the
@when(some other conditions indicating do an upgrade)
... set upgrade sources, or other pre upgrade actions
In this situation, hopefully you can see that we can re-use
'do_install_of_something()' when we do upgrades.
I think it's useful to think about states (flags) as being a 'memory' that
something has happened, and use them to either gate on not doing things
again, or to trigger the next action is a graph of actions that need to
take place to get the charm's payload to the desired operational state. I
tend to name them, and use them, to indicate when something has happened,
rather than when it hasn't, and so tend to use @when_not('some-flag') on
the handler that eventually sets that flag.
Hope that this helps.
> Feng xia
> Lenovo USA
> Phone: 5088011794
> fxia1 at lenovo.com
> Twitter | Facebook | Instagram | Blogs | Forums
> Juju mailing list
> Juju at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailm
Alex Kavanagh - Software Engineer
Cloud Dev Ops - Solutions & Product Engineering - Canonical Ltd
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Juju