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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement