If you try to run Swarm.whereis_name(name) while Swarm.register_name(name, Sup....) didn't finish yet, the application stops to respond.
defmodule MyDynamicSupervisor do
use DynamicSupervisor
# Initialization
def register(name) do
child_spec = %{
id: MyActor,
start: {MyActor, :start_link, [name]},
restart: :transient
}
case DynamicSupervisor.start_child(__MODULE__, child_spec) do
{:error, {:already_started, pid}} -> {:ok, pid}
{:ok, pid} -> {:ok, pid}
end
end
end
defmodule MyActor do
use GenServer
def start_actor(name) do
{:ok, pid} = Swarm.register_name(name, MyDynamicSupervisor, :register, [name])
end
def start_link(name) do
GenServer.start_link(__MODULE__, name)
end
def init(name) do
pid = Swarm.whereis_name(name) # At this moment, the application stops to respond
{:ok, name}
end
end
If you try to run
Swarm.whereis_name(name)
whileSwarm.register_name(name, Sup....)
didn't finish yet, the application stops to respond.