Supervisors
auto_shutdown => any_significant | all_significant lets a supervisor stop itself when significant children exit — useful for task-like subtrees.
28.1 A supervisor module
-module(myapp_sup).
-behaviour(supervisor).
-export([start_link/0, init/1]).
start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
SupFlags = #{strategy => one_for_one,
intensity => 5, % max 5 restarts...
period => 10}, % ...in 10 seconds, else the supervisor dies
Children = [
#{id => kv_store,
start => {kv_store, start_link, []},
restart => permanent, % permanent | transient | temporary
shutdown => 5000, % ms | brutal_kill | infinity
type => worker, % worker | supervisor
modules => [kv_store]},
#{id => worker_sup,
start => {worker_sup, start_link, []},
type => supervisor,
shutdown => infinity}
],
{ok, {SupFlags, Children}}.28.2 Restart strategies
restart only the failed child restart all children restart the failed child and those started after it dynamic, identical children added at runtime with supervisor:start_child(Sup, ExtraArgs)
28.4 Dynamic children
supervisor:start_child(myapp_sup, ChildSpec),
supervisor:terminate_child(myapp_sup, Id),
supervisor:restart_child(myapp_sup, Id),
supervisor:delete_child(myapp_sup, Id),
supervisor:which_children(myapp_sup),
supervisor:count_children(myapp_sup).28.5 Significant children & auto_shutdown (OTP 24+)
auto_shutdown => any_significant | all_significant lets a supervisor stop itself when significant children exit — useful for task-like subtrees.
28.6 Designing supervision trees
Principles: start dependencies first; put stable processes near the root and volatile ones in the leaves; group processes that must restart together.
myapp_sup (one_for_one)
├── myapp_config (permanent worker)
├── myapp_db_sup (rest_for_one)
│ ├── db_pool
│ └── db_cache
└── myapp_conn_sup (simple_one_for_one: one process per client connection)