Packages
commanded
0.17.0
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/commanded/registration/local_registry.ex
defmodule Commanded.Registration.LocalRegistry do
@moduledoc """
Local process registration, restricted to a single node, using Elixir's
[Registry](https://hexdocs.pm/elixir/Registry.html).
"""
@behaviour Commanded.Registration
@doc """
Return an optional supervisor spec for the registry
"""
@spec child_spec() :: [:supervisor.child_spec()]
@impl Commanded.Registration
def child_spec do
[
{Registry, keys: :unique, name: __MODULE__}
]
end
@doc """
Starts a uniquely named child process of a supervisor using the given module
and args.
Registers the pid with the given name.
"""
@spec start_child(name :: term(), supervisor :: module(), args :: [any()]) ::
{:ok, pid} | {:error, term}
@impl Commanded.Registration
def start_child(name, supervisor, args) do
via_name = {:via, Registry, {__MODULE__, name}}
case Supervisor.start_child(supervisor, args ++ [[name: via_name]]) do
{:error, {:already_started, pid}} -> {:ok, pid}
reply -> reply
end
end
@doc """
Starts a uniquely named `GenServer` process for the given module and args.
Registers the pid with the given name.
"""
@spec start_link(name :: term(), module :: module(), args :: any()) ::
{:ok, pid} | {:error, term}
@impl Commanded.Registration
def start_link(name, module, args) do
via_name = {:via, Registry, {__MODULE__, name}}
case GenServer.start_link(module, args, name: via_name) do
{:error, {:already_started, pid}} -> {:ok, pid}
reply -> reply
end
end
@doc """
Get the pid of a registered name.
Returns `:undefined` if the name is unregistered.
"""
@spec whereis_name(name :: term) :: pid | :undefined
@impl Commanded.Registration
def whereis_name(name), do: Registry.whereis_name({__MODULE__, name})
@doc """
Return a `:via` tuple to route a message to a process by its registered name
"""
@spec via_tuple(name :: term()) :: {:via, module(), name :: term()}
@impl Commanded.Registration
def via_tuple(name), do: {:via, Registry, {__MODULE__, name}}
@doc false
def handle_call(_request, _from, _state) do
raise "attempted to call GenServer #{inspect(proc())} but no handle_call/3 clause was provided"
end
@doc false
def handle_cast(_request, _state) do
raise "attempted to cast GenServer #{inspect(proc())} but no handle_cast/2 clause was provided"
end
@doc false
def handle_info(_msg, state) do
{:noreply, state}
end
defp proc do
case Process.info(self(), :registered_name) do
{_, []} -> self()
{_, name} -> name
end
end
end