View Source SuperWorker.Supervisor (SuperWorker v0.0.2)

Documentation for SuperWorker.Supervisor. This module is a new model for the Supervisor module. 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.

Chain processes

Chain processes are a set of processes that are started one after another. The output of the previous process is passed to the next process.

Freedom processes

Freedom processes are independent processes that are started separately.

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 opts = [id: :sup1, number_of_partitions: 2, link: false] Sup.start(opts)

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_all]) 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 \\ 5000)

@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 \\ 5000)

@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 \\ 5000)

@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 \\ 5000)

@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 \\ 5000)

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, timeout \\ 5000)

get_group(sup_id, group_id, timeout \\ 5000)

@spec get_group(atom(), atom(), integer()) :: {:ok, map()} | {:error, any()}

get group structure from supervisor.

get_host_partition(sup_id, data)

@spec get_host_partition(atom(), any()) :: {:error, atom()} | {:ok, 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 \\ 5000)

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

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 \\ 5000)

Send data to a worker in the group.

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

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 \\ 5000)

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

start(opts, timeout \\ 5000)

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

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

@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.