View Source SuperWorker.Supervisor (SuperWorker v0.0.7)

Documentation for SuperWorker.Supervisor. This module is new model supervisor. That fix some issues in the old model. That is an all-in-one supervisor for Elixir application.

New supervisor supports the following features:

  • Group processes
  • Chain processes
  • Freedom processes

Group processes

Group processes are a set of processes that are started together. If one of the processes dies, all the processes in the group will be stopped. Each group has a seperated restart strategy that determines how to restart the group when a process dies.

Chain processes

Chain processes are a set of processes that support for chain prcessing. Each process in a chain has order to process data. The output of the previous process is passed to the next process.

Freedom processes

Freedom processes are independent processes that are started separately. Each process has its own restart strategy.

All type of processes can be started in parallel & can be stopped individually or in a group.

Examples

# Start a supervisor with 2 partitions & 2 groups:
alias SuperWorker.Supervisor, as: Sup

# Config for supervisor
opts = [id: :sup1, number_of_partitions: 2, link: false]

# Start supervisor
Sup.start(opts)

# Add group in runtime, you also can add group in config.
Sup.add_group(:sup1, [id: :group1, restart_strategy: :one_for_all])
Sup.add_group_worker(:sup1, :group1, {Dev, :task, [15]}, [id: :g1_1])

Sup.add_group(:sup1, [id: :group2, restart_strategy: :one_for_one])
Sup.add_group_worker(:sup1, :group2, fn ->
  receice do
  msg ->
    :ok
  end
end, [id: :g2_2])

Summary

Functions

Add a chain to the supervisor. Chain's options follow docs in Chain module.

Add a group to the supervisor. Group's options follow docs in Group module.

Add a worker to a group in the supervisor. Function's options follow Worker module.

Add a standalone worker process to the supervisor. function for start worker can be a function or a {module, function, arguments}. Standalone worker is run independently from other workers follow :one_to_one strategy. If worker crashes, it will check the restart strategy of worker then act accordingly.

Send data to all workers in current group of worker. Using for communite between workers in the same group.

get group structure from supervisor.

Check if supervisor is running. return true if supervisor is running, otherwise return false.

Send data to the entry worker in the chain. If chain doesn't has any worker, it will be dropped.

Send data to a random worker in the group.

Send data to other worker in the same group.

Send data directly to the worker (standalone, group, chain) in the supervisor.

Start supervisor for run standalone please set option :link to false. result format: {:ok, pid} or {:error, reason}

Stop supervisor. Type of shutdown

Functions

add_chain(sup_id, opts, timeout \\ 5000)

Add a chain to the supervisor. Chain's options follow docs in Chain module.

add_chain_worker(sup_id, chain_id, mfa_or_fun, opts, timeout \\ 3000)

@spec add_chain_worker(
  atom(),
  atom(),
  {module(), atom(), list()} | fun(),
  list(),
  integer()
) ::
  {:ok, atom()} | {:error, any()}

Add a worker to the chain in supervisor.

add_group(sup_id, opts, timeout \\ 3000)

@spec add_group(atom(), list(), integer()) :: {:ok, atom()} | {:error, any()}

Add a group to the supervisor. Group's options follow docs in Group module.

add_group_worker(sup_id, group_id, mfa_or_fun, opts, timeout \\ 3000)

@spec add_group_worker(
  atom(),
  atom(),
  {module(), atom(), list()} | fun(),
  list(),
  integer()
) ::
  {:ok, atom()} | {:error, any()}

Add a worker to a group in the supervisor. Function's options follow Worker module.

add_standalone_worker(sup_id, mfa_or_fun, opts \\ [], timeout \\ 3000)

@spec add_standalone_worker(
  atom(),
  {module(), atom(), list()} | fun(),
  list(),
  integer()
) ::
  {:ok, atom()} | {:error, any()}

Add a standalone worker process to the supervisor. function for start worker can be a function or a {module, function, arguments}. Standalone worker is run independently from other workers follow :one_to_one strategy. If worker crashes, it will check the restart strategy of worker then act accordingly.

broadcast_to_group(sup_id, group_id, data, timeout \\ 3000)

Send data to all workers in a group.

broadcast_to_my_group(data)

Send data to all workers in current group of worker. Using for communite between workers in the same group.

child_spec(opts)

get_chain(sup_id, chain_id)

get_group(sup_id, group_id)

@spec get_group(atom(), atom()) ::
  {:ok, SuperWorker.Supervisor.Group.t()} | {:error, any()}

get group structure from supervisor.

get_host_partition(sup_id, data)

@spec get_host_partition(atom(), any()) :: {:error, atom()} | {:ok, atom(), pid()}

get_my_group()

get_my_supervisor()

init(opts, ref)

is_running?(sup_id)

@spec is_running?(atom()) :: boolean()

Check if supervisor is running. return true if supervisor is running, otherwise return false.

main_loop(state, sup_opts)

remove_group_worker(sup_id, group_id, worker_id, timeout \\ 3000)

send_to_chain(sup_id, chain_id, data, timeout \\ 3000)

Send data to the entry worker in the chain. If chain doesn't has any worker, it will be dropped.

send_to_group(sup_id, group_id, worker_id, data, timeout \\ 3000)

Send data to a worker in the group.

send_to_group_random(sup_id, group_id, data, timeout \\ 3000)

Send data to a random worker in the group.

send_to_my_group(worker_id, data)

Send data to other worker in the same group.

send_to_my_group_random(data)

send_to_worker(sup_id, worker_id, data, timeout \\ 3000)

Send data directly to the worker (standalone, group, chain) in the supervisor.

start(opts, timeout \\ 3000)

Start supervisor for run standalone please set option :link to false. result format: {:ok, pid} or {:error, reason}

start_partition(state, opts)

stop(sup_id, shutdown_type \\ :kill, timeout \\ 3000)

@spec stop(atom(), shutdown_type :: atom(), timeout :: integer()) ::
  {:ok, atom()} | {:error, any()}

Stop supervisor. Type of shutdown:

  • :normal supervisor will send a message to worker for graceful shutdown. Not support for spawn process by function.
  • :kill supervisor will kill worker.