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 events arrive. As child processes terminate, the supervisor will accumulate 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 amount 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_seconds, 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
format_status/2
handle_call/3
handle_cancel/3
handle_cast/2
handle_events/3
handle_info/2
handle_subscribe/4
init/1
start_child/2Starts a child in the consumer supervisor.
start_link/2 Starts a consumer supervisor.
start_link/4 Starts a named consumer supervisor module with the given Args.
terminate/2
terminate_child/2Terminates the given child pid.
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()}

format_status/2

format_status(Opt, X2) -> any()

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()

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_link/2

start_link(Children::module() | [supervisor:child_spec()], Opts::any()) -> supervisor:startlink_ret()

Starts a consumer supervisor.

With {Mod, Args} style arguments: starts a supervisor module with the given Args. The init/1 callback of the given module is invoked with Args passed to it and must return a supervision specification as described in the documentation for the init/1 callback.

With a list of children: starts a supervisor with the given Children. A :strategy option is required. Furthermore, the :max_restarts, :max_seconds, and :subscribe_to values can be configured as described in the documentation for the init/1 callback. The child processes specified in Children will be started by appending the event to process to the existing function arguments in the child specification.

Unlike Elixir, this Erlang API has no :name option here: Erlang's gen_server:start_link/3 (which this delegates to) never registers a name. To start a named supervisor, use start_link/4, which takes the name explicitly as its first argument (mirroring gen_server:start_link/4).

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::{local, atom()} | {global, term()} | {via, module(), term()}, Mod::module(), Args::any(), Opts::[option()]) -> supervisor:startlink_ret()

Starts a named consumer supervisor module with the given Args.

Name registers the supervisor as described under the "Name Registration" section in the gen_server module docs ({local, atom()}, {global, term()}, or {via, module(), term()}). This is the Erlang equivalent of passing name: ... to Elixir's ConsumerSupervisor.start_link/3.

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

which_children/1

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


Generated by EDoc