Module consumer_supervisor

A supervisor that starts children as events flow in.

Behaviours: gen_stage.

Description

A supervisor that starts children as events flow in.

A consumer_supervisor can be used as the consumer in a gen_stage pipeline. A new child process will be started per event, where the event is appended to the arguments in the child specification.

A consumer_supervisor can be attached to a producer by returning subscribe_to from init/1 or explicitly with gen_stage:sync_subscribe/3 and gen_stage:async_subscribe/2.

Once subscribed, the supervisor will ask the producer for max_demand events and start child processes as evets arrive. As child processes terminate, the supervisor will aaccumulate demand and request more events once min_demand is reached. This allows the consumer_supervisor to work similar to a pool, except a child process is started per event. The minimum amout of concurrent children per producer is specified by min_demand and maximum is given by max_demand.

Data Types

option()

option() = {registry, atom()} | {strategy, supervisor:strategy()} | {max_restarts, non_neg_integer()} | {max_records, non_neg_integer()} | {subscribe_to, [gen_stage:stage() | {gen_stage:stage(), [{atom(), any()}]}]}

sup_ref()

sup_ref() = (Name::atom()) | {Name::atom(), Node::node()} | {global, Name::term()} | {via, Module::module(), Name::any()} | pid()

Function Index

code_change/3
count_children/1
handle_call/3
handle_cancel/3
handle_cast/2
handle_events/3
handle_info/2
handle_subscribe/4
init/1
save_child/5
start_child/2Starts a child in the consumer supervisor.
start_child/3
start_link/2Starts a supervisor with the given children.
start_link/4
terminate/2
terminate_child/2Terminates the given child pid.
wait_children/6
which_children/1

Function Details

code_change/3

code_change(X1, State, X3) -> any()

count_children/1

count_children(Supervisor::sup_ref()) -> #{specs => non_neg_integer(), active => non_neg_integer(), supervisors => non_neg_integer(), workers => non_neg_integer()}

handle_call/3

handle_call(X1, From, State) -> any()

handle_cancel/3

handle_cancel(X1, X2, State) -> any()

handle_cast/2

handle_cast(Msg, State) -> any()

handle_events/3

handle_events(Events, From, State) -> any()

handle_info/2

handle_info(Msg, State) -> any()

handle_subscribe/4

handle_subscribe(X1, Opts, From, State) -> any()

init/1

init(X1) -> any()

save_child/5

save_child(X1, Producer, Pid, Args, State) -> any()

start_child/2

start_child(Supervisor::sup_ref(), Args::[term()]) -> supervisor:startchild_ret()

Starts a child in the consumer supervisor.

The child process will be started by appending the given list of Args to the existing function arguments in the child specification.

This child is started separately from any producer and does not count towards the demand of any of them.

If the child process starts, function return {ok, Child} or {ok, Child, Info}, the pid is added to the supervisor, and the function returns the same value.

If the child process start function returns ignore, an error tuple, or an erroneous value, or if it fails, the child is discarded and ignore or {error, Reason} where Reason is a term containing information about the error is returned.

start_child/3

start_child(M, F, A) -> any()

start_link/2

start_link(Mod::module(), Args::any()) -> supervisor:startlink_ret()

Starts a supervisor with the given children.

A strategy is required to be given as an option. Furthermore, the max_restarts, max_seconds, and subscribe_to values can be configured as described in the documentation for the init/1 callback.

The options can also be used to register a supervisor name. The supported values are described under the "Name Registration" section in the gen_server module docs. The child processes specified in children will be started by appending the event to process to the existing function arguments in the child specification.

Note that the consumer supervisor is linked to the parent process and will exit not only on crashes but also if the parent process exits with normal reason.

start_link/4

start_link(Name::term(), Mod::module(), Args::any(), Opts::[option()]) -> supervisor:startlink_ret()

terminate/2

terminate(X1, State) -> any()

terminate_child/2

terminate_child(Supervisor::sup_ref(), Pid::pid()) -> ok | {error, not_found}

Terminates the given child pid.

If successful, the function returns ok. If there is no such pid, the function returns {error, not_found}.

wait_children/6

wait_children(Restart, Shutdown, Pids, Size, Timer, Stacks) -> any()

which_children/1

which_children(Supervisor::sup_ref()) -> [{undefined, pid() | restarting, worker | supervisor, dynamic | [module()]}]


Generated by EDoc