Blame | Last modification | View Log | RSS feed
-module(pgapp_sup).
-behaviour(supervisor).
%% API
-export([start_link/0, add_pool/3]).
%% Supervisor callbacks
-export([init/1]).
%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
%% ===================================================================
%% API functions
%% ===================================================================
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% ===================================================================
%% Supervisor callbacks
%% ===================================================================
init([]) ->
{ok, Pools} = application:get_env(pgapp, pools),
PoolSpec = lists:map(fun ({PoolName, SizeArgs, WorkerArgs}) ->
PoolArgs = [{name, {local, PoolName}},
{worker_module, pgapp_worker}] ++ SizeArgs,
poolboy:child_spec(PoolName, PoolArgs, WorkerArgs)
end, Pools),
{ok, { {one_for_one, 10, 10}, PoolSpec} }.
add_pool(Name, PoolArgs, WorkerArgs) ->
ChildSpec = poolboy:child_spec(Name, PoolArgs, WorkerArgs),
supervisor:start_child(?MODULE, ChildSpec).