defmodule Worm.Leader do
def leader_election() do
nodes =
([Node.self()] ++ Node.list())
max = Enum.max(nodes)
Enum.each(nodes, fn node -> send_leader_info(node, max) end)
end
def send_leader_info(node, max) do
Process.send_after(node, {:leader, max}, 200)
end
end
Genserver
@impl GenServer
def handle_info({:leader, max_node}, state) do
Logger.info("--- New Leader is: #{state}")
# Reschedule once more
Worm.Leader.leader_election()
{:noreply, state}
end
You are sending messages, but you have never specified to which process you want to send them. This mean, that it treats it as an process names in local registry, so in the end these messages goes nowhere.
Process.send_after/3 needs a pid or a process name as the first argument, but you gave it a node name.
Here’s part the documentation of Process.send_after/3:
If dest (the first arg) is a PID, it must be the PID of a local process, dead or alive. If dest
is an atom, it must be the name of a registered PROCESS which is looked up at
the time of delivery. No error is produced if the name does not refer to a
process.
Kernel.send/2 can take {process_name, node_name} as the first arg, where process_name is an atom. You have to workaround it in order to delay the sending, for example, send to self() a message after 200ms, and when that message is consumed, use Kernel.send/2 to send a message to the remote processes.
Assuming you want to receive the messages in your GenServer process, you can start the genserver with a name and send messages to {genserve_name, node_name}.
def start_link(opts) do
# starting genserver with a name
GenServer.start_link(__MODULE__, opts, name: SomeName)
end
@impl GenServer
def handle_info({:leader, max_node}, state) do
Logger.info("--- New Leader is: #{state}")
# Reschedule once more
Worm.Leader.leader_election()
{:noreply, state}
end
defmodule Worm.Leader do
def leader_election() do
nodes =
([Node.self()] ++ Node.list())
max = Enum.max(nodes)
Enum.each(nodes, fn node -> send_leader_info(node, max) end)
end
def send_leader_info(node, max) do
# sending to {SomeName, node} instead of node
Process.send_after({SomeName, node}, {:leader, max}, 200)
end
end
I recommend you take a look at the erlang Erlang -- pg module. It sounds like it’d fit this use case well, getting registered processes across nodes to send them messages.
At the most basic explanation, pg2 allows for a group to be created and then for processes to connect to the group. This leads to a mapping of name -> pid list. The pid list consists of all known processes, whether they be local or remote.