我有一个简单的应用程序target_interceptor
,它在收到注册和注销消息后,启动或终止simple_one_for_one
rpc_server_supervisor
.
主管代码rpc_server_supervisor
:
init([]) ->
MaxRestart = 5,
MaxTime = 3600,
{ok, {{simple_one_for_one, MaxRestart, MaxTime},
[{rbmq_rpc_server,
{rbmq_rpc_server, start_link, []},
temporary,
10000,
worker,
[rbmq_rpc_server]}]}}.
注册消息target_interceptor
:
handle_cast({register, Args}, S = #state{channel = Channel, supervisor=Sup, refs=R, qs_link = QSLinks}) ->
{Connection, QueueName, Node} = Args,
{Ok, Pid} = supervisor:start_child(Sup, [Connection, QueueName, Node]),
Ref = erlang:monitor(process, Pid),
{noreply, S#state{refs=gb_sets:add(Ref,R), qs_link=orddict:append(binary_to_list(QueueName),Pid,QSLinks)}};
注销消息target_interceptor
:
handle_cast({unregister,{QueueName}}, S = #state{supervisor = Sup, qs_link = QSLinks}) ->
Pid = orddict:fetch(QueueName,QSLinks)
case supervisor:terminate_child(Sup,Pid) of
ok -> Success = true;
Error -> io:format("Error ~p~n",[Error]),
Success = false
end,
{noreply, S#state{qs_link=orddict:erase(QueueName,QSLinks)}}
我的 Erlang 版本是:R15B01
第一个问题是在处理注册操作时,元组 {OK, Pid} = {error, <0.57.0>}; 尽管它表明出现问题,但 Pid 57 上的 gen_server rbmq_rpc_server 工作正常并响应消息。为什么start_child函数的返回值错误?什么地方出了错?
第二个问题是在处理注销操作时,即使我引用 Pid 而不是 ChildID,supervisor:terminate_child(Sup,Pid) 也会返回 {error,simple_one_for_one)。为什么会这样?我如何动态地单独终止主管的孩子?
编辑:两者target_interceptor
都由rpc_server_supervisor
主管rbmq_sup_sup
监督:
init({Nodeid, Node}) ->
MaxRestart = 1,
MaxTime = 3600,
{ok, {{rest_for_one, MaxRestart, MaxTime},
[{server,
{target_interceptor, start_link, [target, self(), {Nodeid, Node}]},
permanent,
5000,
worker,
[target_interceptor]}]}}.
编辑:在 target_interceptor init() 函数中调用 rpc_server_supervisor(这里是 rbmq_sup_sup 主管):
handle_info({start_worker_supervisor, Sup}, S = #state{}) ->
{ok, Pid} = supervisor:start_child(Sup, ?SPEC),
link(Pid),
{noreply, S#state{sup=Pid}};
-define(SPEC,
{rpc_server_sup,
{rpc_server_sup, start_link, []},
temporary,
10000,
supervisor,
[rpc_server_sup]}).