【发布时间】:2014-11-26 21:06:29
【问题描述】:
我想将一些参数传递给supervisor:init/1 函数,并且希望应用程序的界面是这样的:
redis_pool:start() % start all instances
redis_pool:start(Names) % start only given instances
这是应用程序:
-module(redis_pool).
-behaviour(application).
...
start() -> % start without params
application:ensure_started(?APP_NAME, transient).
start(Names) -> % start with some params
% I want to pass Names to supervisor init function
% in order to do that I have to bypass application:ensure_started
% which is not GOOD :(
application:load(?APP_NAME),
case start(normal, [Names]) of
{ok, _Pid} -> ok;
{error, {already_started, _Pid}} -> ok
end.
start(_StartType, StartArgs) ->
redis_pool_sup:start_link(StartArgs).
这里是主管:
init([]) ->
{ok, Config} = get_config(),
Names = proplists:get_keys(Config),
init([Names]);
init([Names]) ->
{ok, Config} = get_config(),
PoolSpecs = lists:map(fun(Name) ->
PoolName = pool_utils:name_for(Name),
{[Host, Port, Db], PoolSize} = proplists:get_value(Name, Config),
PoolArgs = [{name, {local, PoolName}},
{worker_module, eredis},
{size, PoolSize},
{max_overflow, 0}],
poolboy:child_spec(PoolName, PoolArgs, [Host, Port, Db])
end, Names),
{ok, {{one_for_one, 10000, 1}, PoolSpecs}}.
如您所见,当前的实现很丑陋并且可能存在错误。问题是我如何传递一些参数并启动应用程序和主管(使用给 start/1 的参数)?
一个选项是在两个单独的阶段启动应用程序和运行 redis 池。
redis_pool:start(),
redis_pool:run([] | Names).
但是如果我想在我的应用启动时运行主管子级(redis 池)怎么办?
谢谢。
【问题讨论】:
标签: erlang erlang-otp