Chosen (chosen v0.1.2)

Copy Markdown

Distributed singleton supervisor backed by PostgreSQL advisory locks.

Ensures a child process or supervisor runs exactly once across your entire cluster. See README.md for usage examples and architecture details.

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns supervised children count (Supervisor.count_children/1 semantics)

Starts the Chosen supervisor.

Returns supervised children info (Supervisor.which_children/1 semantics)

Types

start_opt()

@type start_opt() ::
  {:child, Supervisor.child_spec()}
  | {:name, term()}
  | {:sup_name, term()}
  | {:polling_interval, integer()}
  | {:lock_manager_name, GenServer.server()}
  | {:on_lock_acquired, (term() -> :ok)}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count_children(server)

@spec count_children(Supervisor.supervisor()) :: %{
  specs: non_neg_integer(),
  active: non_neg_integer(),
  supervisors: non_neg_integer(),
  workers: non_neg_integer()
}

Returns supervised children count (Supervisor.count_children/1 semantics)

start_link(opts)

@spec start_link([start_opt()]) :: Supervisor.on_start()

Starts the Chosen supervisor.

Options

  • :child - (required) Child spec to supervise as singleton
  • :name - (optional) Lock identifier, default: Chosen. Use unique names for multiple singletons
  • :sup_name - (optional) Supervisor name for which_children/1 and count_children/1
  • :polling_interval - (optional) Lock retry interval in ms, default: 500
  • :lock_manager_name - (optional) LockManager to use, default: Chosen.LockManager
  • :on_lock_acquired - (optional) Callback function invoked after successfully acquiring lock and starting child

Example

children = [
  {Chosen.LockManager, repo: MyApp.Repo},
  {Chosen, child: MyWorker, name: :my_singleton}
]

See README.md for more examples.

which_children(server)

@spec which_children(Supervisor.supervisor()) :: [
  {term() | :undefined, Supervisor.child(), :worker | :supervisor,
   [module()] | :dynamic}
]

Returns supervised children info (Supervisor.which_children/1 semantics)