tellesleandro

tellesleandro

Call GenServer from a remote node

I have the following code running on node “a”:

defmodule Devices.Teste do
  use GenServer

  def start_link() do
    IO.inspect(["a", self()])
    GenServer.start_link(__MODULE__, [], [])
  end

  def a(pid) do
    IO.inspect(["b", self(), pid])
    GenServer.call(pid, :a)
  end

  @impl true
  def init([]) do
    IO.inspect(["c", self()])
    {:ok, []}
  end

  @impl true
  def handle_call(:a, from, state) do
    IO.inspect(["d", :a, self(), from])
    Process.sleep(3000)
    {:reply, :xyz, state}
  end

  @impl true
  def handle_info(x, state) do
    IO.inspect(["e", x, self()])
    {:noreply, state}
  end
end

From a node b, I call

{:ok, pid} = :erpc.call(:"a@192.168.15.83", Devices.Teste, :start_link, [])

then, the messages

["a", #PID<0.225.0>]
["c", #PID<0.226.0>]

are printed and the call returns

{:ok, #PID<20968.226.0>}

After that, I call

:erpc.call(:"a@192.168.15.83", Devices.Teste, :a, [pid])

then, the message

["b", #PID<0.227.0>, #PID<0.226.0>]

is printed, but I get the following error:

** (exit) {:exception, {:noproc, {GenServer, :call, [#PID<20968.226.0>, :a, 5000]}}}
    (kernel 8.5.4) erpc.erl:700: :erpc.call/5
    iex:2: (file)

If I call

GenServer.call(pid, :a)

no messages are printed and I get the following error

** (exit) exited in: GenServer.call(#PID<20968.214.0>, :a, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir 1.15.0) lib/gen_server.ex:1074: GenServer.call/3
    iex:2: (file)

What am I missing?
As long as I understand, I should use the “remote” pid that I get from the start_link function.

Most Liked

anuaralfetahe

anuaralfetahe

When the :erpc call is finished the spawned process on the remote node is terminated and it brings down the started child as well because they are linked.

Consider using start instead of start_link, and this approach should resolve the issue. Unlike start_link, start does not link the caller to the created processes. As a result, even after the caller terminates, the created processes remain active.

GenServer.start(__MODULE__, [], [])

There might be a more elegant solution to your problem, but I’m not fully aware of the entire context.

Be careful with ‘start’ because it can leave rogue processes in the system..

tellesleandro

tellesleandro

start did the trick. Thanks @anuaralfetahe.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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 40082 209
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement