Getting number of deployed units

I’m charming a CI runner, which spawns jobs on a cloud and collects/publishes their results.

In my charm I have config which specifies the number of workloads to run at once. This is an external limit imposed on us by our cloud configuration (number of concurrent cloud instances we’re allowed to spawn).

Sometimes the controller itself gets overloaded - it performs a lot of network and local IO to orchestrate job running. Our charm does not support horizontal scaling at the minute, but I’d like to add this ability so that when we are so constrained we can add a unit or two. When this happens the external limits still need to be respected, so we need to adjust the number of jobs that each unit is running (quota / n units) to spread the load out.

Simple question then, which I can’t find the answer to in the docs: in a charm, how can I get the number of units that are deployed for an application? Or if that’s not possible directly, does anyone have any ideas for how to implement this scheme in a nice way?

Ah, can I use peers and then look at charmhelpers.core.hookenv.related_units in relation-changed maybe?

I did something similar a while ago with peers relations. You can find it here. I’m not sure if this is the best way to go about this however, if someone has a better solutions please share.