justinbkay

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

josevalim

Creator of Elixir

I am guessing here, two possible reasons:

  1. The fourth argument to multicall needs to be list of parameters, so you need: :rpc.multicall(Node.list(), Appapi.Log.FindPayload, :start, ['13102496'])

  2. The module Appapi.Log.FindPayload is not loaded in the remote node

Also Liked

josevalim

josevalim

Creator of Elixir

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

RudManusachi

There are functions in :rpc module :rpc.call, :rpc.cast, :rpc.multicall etc. see rpc — OTP 29.0.2 (kernel 11.0.2)
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 =)

Last Post!

Erynn

Erynn

will it work for all nodes if all nodes have connect_all set false and connected in a ring manually like 1 connected to 2, 2 to 3, 3 to 4, 4 to 5. will rpc.multicall work from node 3, if i have to fetch data from all nodes?

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement