gen_registry v1.0.1 GenRegistry View Source

GenRegistry provides a Registry like interface for managing processes.

Link to this section Summary

Types

t()

GenRegistry State

Functions

Callback called by Supervisor.init/2

Return the number of running processes in this registry

Invoked when the server is started. start_link/3 or start/3 will block until it returns

Lookup a running a process

Attempts to lookup a running process by id

Loop over all the processes and return result

Start a registry instance

Safely stops a process managed by the GenRegistry

Link to this section Types

Link to this type t() View Source
t() :: %GenRegistry{
  worker_module: module(),
  worker_type: :supervisor | :worker,
  workers: :ets.tid()
}

GenRegistry State.

  • worker_module: Module to spawn
  • worker_type: :supervisor if the worker_module is a supervisor, :worker otherwise
  • workers: ETS table id holding the worker tracking records.

Link to this section Functions

Link to this function child_spec(arg) View Source
child_spec(opts :: Keyword.t()) :: Supervisor.child_spec()

Callback called by Supervisor.init/2

It is required that you provide a :worker_module argument or the call will fail.

Return the number of running processes in this registry.

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

args is the argument term (second argument) passed to start_link/3.

Returning {:ok, state} will cause start_link/3 to return {:ok, pid} and the process to enter its loop.

Returning {:ok, state, timeout} is similar to {:ok, state} except handle_info(:timeout, state) will be called after timeout milliseconds if no messages are received within the timeout.

Returning {:ok, state, :hibernate} is similar to {:ok, state} except the process is hibernated before entering the loop. See c:handle_call/3 for more information on hibernation.

Returning {:ok, state, {:continue, continue}} is similar to {:ok, state} except that immediately after entering the loop the c:handle_continue/2 callback will be invoked with the value continue as first argument.

Returning :ignore will cause start_link/3 to return :ignore and the process will exit normally without entering the loop or calling c:terminate/2. If used when part of a supervision tree the parent supervisor will not fail to start nor immediately try to restart the GenServer. The remainder of the supervision tree will be started and so the GenServer should not be required by other processes. It can be started later with Supervisor.restart_child/2 as the child specification is saved in the parent supervisor. The main use cases for this are:

  • The GenServer is disabled by configuration but might be enabled later.
  • An error occurred and it will be handled by a different mechanism than the Supervisor. Likely this approach involves calling Supervisor.restart_child/2 after a delay to attempt a restart.

Returning {:stop, reason} will cause start_link/3 to return {:error, reason} and the process to exit with reason reason without entering the loop or calling c:terminate/2.

Callback implementation for GenServer.init/1.

Link to this function lookup(table, id) View Source
lookup(table :: :ets.tid(), id :: GenRegistry.Types.id()) ::
  {:ok, pid()} | {:error, :not_found}

Lookup a running a process.

This is a fast path to the ETS table.

Link to this function lookup_or_start(registry, id, args \\ [], timeout \\ 5000) View Source
lookup_or_start(
  registry :: GenServer.server(),
  id :: GenRegistry.Types.id(),
  args :: [any()],
  timeout :: integer()
) :: {:ok, pid()} | {:error, any()}

Attempts to lookup a running process by id.

If the id is not associated with a running process then it is spawned, the optional third argument will be passed to start_link of the worker_module to spawn a new process.

Link to this function reduce(table, acc, func) View Source
reduce(
  table :: :ets.tid(),
  acc :: any(),
  ({GenRegistry.Types.id(), pid()}, any() -> any())
) :: any()

Loop over all the processes and return result.

The function will be called with two arguments, a two-tuple of {id, pid} and then accumulator, the function should return the accumulator.

There is no ordering guarantee when reducing.

Link to this function start_link(module, opts \\ []) View Source
start_link(module(), Keyword.t()) :: {:ok, pid()} | {:error, any()}

Start a registry instance.

GenRegistry should be run under a supervision tree, it is not recommended to call this directly.

Link to this function stop(registry, id) View Source
stop(registry :: GenServer.server(), id :: GenRegistry.Types.id()) ::
  :ok | {:error, :not_found}

Safely stops a process managed by the GenRegistry

In addition to stopping the process, the id is also removed from the GenRegistry

If the id provided is not registered this will return {:error, :not_found}