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 worker to the chain in supervisor.
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 a group.
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 worker in the group.
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 a chain to the supervisor.
Chain's options follow docs in Chain
module.
@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 a group to the supervisor.
Group's options follow docs in Group
module.
@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.
@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.
Send data to all workers in a group.
Send data to all workers in current group of worker. Using for communite between workers in the same group.
@spec get_group(atom(), atom()) :: {:ok, SuperWorker.Supervisor.Group.t()} | {:error, any()}
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 worker in the group.
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}
@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.