mgwidmann

mgwidmann

I need to have a cluster wide monitor which will produce an output (hard coded) if a certain set of processes goes down at any time. I am using Horde’s DynamicSupervisor to start this child process on whichever node boots first like so in my application tree:

Code has the main application name changed to MyApp to remove my project’s name.

Supervisor.start_link(
      [
        {Cluster.Supervisor, [Application.get_env(:libcluster, :topologies), [name: MyApp.ClusterSupervisor]]},
        {Horde.Registry, name: MyApp.Registry, keys: :unique, members: :auto},
        {Task.Supervisor, name: MyApp.TaskSupervisor, strategy: :one_for_one},
        {Horde.DynamicSupervisor, name: MyApp.ClusterWideSupervisor, strategy: :one_for_one, distribution_strategy: Horde.UniformRandomDistribution}
        |
        children ++ [ # At the end of the tree
          %{
            id: MyApp.StartCrashResponder,
            start: {Task, :start_link, [__MODULE__, :start_crash_responder, []]},
            restart: :temporary, # The task start_link is temporary, not my dynamic process
            type: :worker
          }
        ]
      ],
      opts
    )

Then that function which the task runs to start up the dynamic child:

def start_crash_responder() do
    result = Horde.DynamicSupervisor.start_child(
      MyApp.ClusterWideSupervisor,
      %{
        id: "crash-responder", # Hard coded so theres only ever one
        start: {MyApp.Monitoring.CrashResponder, :start_link, [@monitored_processes]}, # Their registered names (which is their module names)
        restart: :permanent # This should cause it to be restarted on hard or graceful node down
      }
    )

    case result do
      {:ok, _pid} ->
        nil
      {:error, {:already_started, pid}} ->
        Logger.info("CrashResponder already online: #{inspect(pid)}")
        MyApp.Monitoring.CrashResponder.update()
      {:error, reason} ->
        Logger.error("Error starting CrashResponder: #{inspect(reason)}")
    end

    result
  end

The CrashResponder is a GenServer that receives a list of names in its start_link and in its handle_continue callback will look them up in the registry and call Process.monitor on each. It then handles the down messages and will produce an output if one goes down to alert the user the system is unstable and may not have processed something and may require trying again. If a node goes offline, the CrashResponder should be located on another node somewhere in the cluster. Its not a big deal if a duplicate is online for a short period of time or even produces a double output on rare occasions, just so long as at least 1 is running somewhere in the cluster.

When I boot two processes locally on my machine, one CrashResponder comes online and both nodes recognize that its a singleton:

Node A:

11:44:20.944 service=CrashResponder [info] Online! #PID<0.259.0>

11:44:20.944 service=CrashResponder [info] Continue completed!

Node B:

11:44:24.804 [info] CrashResponder already online: #PID<27461.259.0>

Then when I do :init.stop() or CTRL-C twice (doesn’t matter), the node goes down but node B does not restart the CrashResponder. When I look it up in the registry, its no longer registered. I can’t figure out what I’ve done incorrectly. Any ideas where to look?

Showing Posts 1 to 2

mgwidmann

mgwidmann OP

I found this but I am not sure how to fix it, it seems to indicate the dynamic supervisor is not aware of the other node:

iex(nodea@127.0.0.1)4> Horde.Cluster.members(MyApp.Registry)
[
  {MyApp.Registry, :"nodeb@127.0.0.1"},
  {MyApp.Registry, :"nodea@127.0.0.1"}
]
iex(nodea@127.0.0.1)5> Horde.Cluster.members(MyApp.ClusterWideSupervisor)
[{MyApp.ClusterWideSupervisor, :"nodea@127.0.0.1"}]
mgwidmann

mgwidmann OP

Ok I fixed it but I’m not sure why it was necessary, and I don’t see anything mentioning needing to do this in the docs as far as I have come across.

Adding this to my supervision tree after the DynamicSupervisor fixed the problem:

{Horde.NodeListener, MyApp.ClusterWideSupervisor}

This ensures that the DynamicSupervisor is updated with node add and remove events.

Digging through the code, the real issue seems to be that the members option defaults to [] instead of :auto whereas I had the Horde.Registry already set to :auto (which also has the same default). Using memers: :auto fixes the problem, I was not aware it was needed in both places.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews