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

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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New

We're in Beta

About us Mission Statement