justinbkay
Run a function on each node in the cluster
Hi,
I have a small cluster of Elixir nodes and I want to run a function on each node in the cluster and display the output on a liveview page. How’s the best way to go about running the function on each node in Node.list(), awaiting the response from each one? I’ve seen some examples using Node.spawn, and I know there’s an async/await syntax in Elixir. Looking for some guidance. Thanks!
Marked As Solved
josevalim
I am guessing here, two possible reasons:
-
The fourth argument to multicall needs to be list of parameters, so you need:
:rpc.multicall(Node.list(), Appapi.Log.FindPayload, :start, ['13102496']) -
The module
Appapi.Log.FindPayloadis not loaded in the remote node
Also Liked
josevalim
Phoenix.PubSub could be used for this too. You could have a process running on each node that subscribes to a topic called “log_tailers”. Then you broadcast on said topic something like “give me the last 10 lines” and all subscribed processes would return to you the relevant info. :rpc and friends work too.
RudManusachi
There are functions in :rpc module :rpc.call, :rpc.cast, :rpc.multicall etc. see Erlang -- rpc
Can be used as
{results, failed_nodes} = :rpc.multicall(Node.list(), mod, fun, args)
Could you please tell more about the scenario, the connection between the user interaction through liveview and distributed nodes? To give an idea why I’m asking is, for example, sometimes all we need is to broadcast data updates that happened on one node to all users connected to any node in the cluster through liveview, in that case Phoenix.PubSub broadcast could be enough and no need to manually call a function on each node. But YMMV =)







