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

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement