Get state of all workers managed by a supervisor

Wanting a quick and easy way to get the state of all workers managed by a supervisor process – use case is for debugging a live running application.

Poked around a bit, and worked this up:

def get_worker_states(supervisor) do
  Enum.each(Supervisor.which_children(supervisor), fn({_id, pid, type, _modules}) ->
    if type == :worker do
      IO.puts "----------------------------------"
      IO.puts inspect(:sys.get_state(pid, 1000))
      IO.puts "----------------------------------"

Seems to work OK.

Wondering if anybody has thoughts on a better/more elegant approach?

What could be “better or more elegant approach” to enumerate children rather than enumerating children?

which_children/1 and get_state/2 are pretty great for one-off debugging and generally what I use.

Other alternatives which I’m guessing might not apply to your use case:

  • :observer.start/0 if you can launch a GUI
  • register the workers in a Registry and get their states individually

If you want to get fancy, you could write a GenServer to track the worker pids, but that’s overkill unless you’re implementing some form of status monitoring.