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 Responses

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

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
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
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement