Packages
commanded
0.15.1
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/registration.ex
defmodule Commanded.Registration do
@moduledoc """
Defines a behaviour for a process registry to be used by Commanded.
By default, Commanded will use a local process registry, defined in
`Commanded.Registration.LocalRegistry`, that uses Elixir's `Registry` module
for local process registration. This limits Commanded to only run on a single
node. However the `Commanded.Registration` behaviour can be implemented by a
library to provide distributed process registration to support running on a
cluster of nodes.
"""
@doc """
Return an optional supervisor spec for the registry
"""
@callback child_spec() :: [:supervisor.child_spec()]
@doc """
Starts a uniquely named child process of a supervisor using the given module
and args.
Registers the pid with the given name.
"""
@callback start_child(name :: term(), supervisor :: module(), args :: [any()]) :: {:ok, pid()} | {:error, reason :: term()}
@doc """
Starts a uniquely named `GenServer` process for the given module and args.
Registers the pid with the given name.
"""
@callback start_link(name :: term(), module :: module(), args :: any()) :: {:ok, pid()} | {:error, reason :: term()}
@doc """
Sends a message to the given dest and returns `:ok`.
"""
@callback multi_send(dest :: atom(), message :: any()) :: :ok
@doc """
Get the pid of a registered name.
Returns `:undefined` if the name is unregistered.
"""
@callback whereis_name(name :: term()) :: pid() | :undefined
@doc """
Return a `:via` tuple to route a message to a process by its registered name
"""
@callback via_tuple(name :: term()) :: {:via, module(), name :: term()}
@doc false
@spec child_spec() :: [:supervisor.child_spec()]
def child_spec, do: registry_provider().child_spec()
@doc false
@callback start_child(name :: term(), supervisor :: module(), args :: [any()]) :: {:ok, pid()} | {:error, reason :: term()}
def start_child(name, supervisor, args), do: registry_provider().start_child(name, supervisor, args)
@doc false
@spec start_link(name :: term(), module :: module(), args :: any()) :: {:ok, pid()} | {:error, reason :: term()}
def start_link(name, module, args), do: registry_provider().start_link(name, module, args)
@doc false
@spec multi_send(server :: atom(), message :: any()) :: :ok
def multi_send(server, message), do: registry_provider().multi_send(server, message)
@doc false
@spec whereis_name(term()) :: pid() | :undefined
def whereis_name(name), do: registry_provider().whereis_name(name)
@doc false
@spec via_tuple(name :: term()) :: {:via, module(), name :: term()}
def via_tuple(name), do: registry_provider().via_tuple(name)
@doc """
Get the configured process registry.
Defaults to a local registry, restricted to running on a single node, if not configured.
"""
@spec registry_provider() :: module()
def registry_provider do
case Application.get_env(:commanded, :registry, :local) do
:local -> Commanded.Registration.LocalRegistry
other -> other
end
end
@doc """
Use the `Commanded.Registration` module to import the registry and via tuple functions.
"""
defmacro __using__(_) do
quote location: :keep do
@before_compile unquote(__MODULE__)
import unquote(__MODULE__), only: [registry_provider: 0, via_tuple: 1]
alias unquote(__MODULE__)
end
end
@doc """
Allow a registry provider to handle the standard `GenServer` callback functions
"""
defmacro __before_compile__(_env) do
quote location: :keep do
@doc false
def handle_call(request, from, state),
do: registry_provider().handle_call(request, from, state)
@doc false
def handle_cast(request, state),
do: registry_provider().handle_cast(request, state)
@doc false
def handle_info(msg, state),
do: registry_provider().handle_info(msg, state)
end
end
end