I'm trying to deploy a function on a remote Elixir node using Node.spawn/4
but I can't seem to manage it.
I have defined the function in question, (hello world), in a module Hello in iex and tried to deploy it using Node.spawn(:node2@remote, Hello, :world, [])
.
iex(node1@local) defmodule Hello do
...(node1@local) def world, do: IO.puts("Hello World)
...(node1@local) end
Node.spawn(:node2@remote, Hello, :world, [])
I would expect spawn/4 to send the module Hello over to the remote node so that the function world/0 is recognized there when trying to spawn it. However, I get the following error:
[error] Error in process #PID<16431.208.0> on node :node2@remote with exit value:
{:undef, [{Hello, :world, [], []}]}
The terminology you use makes it extremely hard to understand what have you tried.
As stated by :erlang.spawn/4
documentation, this function works in all the aspects exactly as :erlang.spawn/3
except it executed the code on the remote node. There is no deploy involved, whatever it means. The remote node must be known to the caller and must contain the code to be executed.
There is a great tutorial available at elixir-lang.org — Distributed tasks and configuration
.
To check if the remote node is visible to the caller, one might call Node.connect/1
.
Summing up.
Start two named iex
sessions from two different terminals:
$ iex --sname foo
$ iex --sname bar
From the first one try:
Node.spawn(:bar@localhost, IO, :puts, ["¡YAY!"])
You should see "¡YAY!"
printed. To test your own Hello.world/0
, this function must be known to the target node. So, switch to :foo@localhost
instance and type:
defmodule Hello, do: def world, do: IO.puts("¡YAY!")
Now switch to :bar@localhost
and do:
Node.spawn(:foo@localhost, Hello, :world, [])
You should see "¡YAY!"
printed out.