%%%------------------------------------------------------------------- %% @doc eota_streams top level supervisor. %% @end %%%------------------------------------------------------------------- -module(eredis_streams_sup). -behaviour(supervisor). -include_lib("eunit/include/eunit.hrl"). -export([start_link/0]). -export([init/1]). -export([add_stream/1]). -define(SERVER, ?MODULE). start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, []). init([]) -> SupFlags = #{strategy => one_for_one, intensity => 10, period => 1}, ChildSpecs = gen_stream_sups_specs(streams(), []), {ok, {SupFlags, ChildSpecs}}. add_stream(Declaration) -> ChildSpec = hd(gen_stream_sups_specs([Declaration], [])), supervisor:start_child(?MODULE, ChildSpec). %% internal functions gen_stream_sups_specs([], Specs) -> Specs; gen_stream_sups_specs([Stream | Streams], Specs) -> NewSpecs = [stream_sup_spec(Stream) | Specs], gen_stream_sups_specs(Streams, NewSpecs). stream_sup_spec(#{name := StreamName} = Stream) -> #{ id => {eredis_streams_stream_sup, StreamName}, start => {eredis_streams_stream_sup, start_link, [Stream]}, restart => permanent, shutdown => infinity, type => supervisor, modules => [eredis_streams_stream_sup] }. streams() -> application:get_env(eredis_streams, streams, []).