Erlang:如何在管理程序中使用start_child正确分派gen_server并调用API

时间:2016-09-02 16:43:34

标签: erlang dispatcher otp supervisor gen-server

我的 cavv 应用程序中有一个gen_server,我需要先启动它来执行调用。我想为此使用命令调度程序。举一个简短的例子,这是gen_server的API:

gen_server:cavv_user

-module(cavv_user).

-behavior(gen_server).

-define(SERVER(UserId), {via, gproc, {n, l, {?MODULE, UserId}}}).

start_link(UserId) ->
    gen_server:start_link(?SERVER(UserId), ?MODULE, [UserId], []).

change_email_address(UserId, EmailAddress) ->
    gen_server:call(?SERVER(AggregateId), {execute_command, #change_user_email_address{user_id=UserId, email_address=EmailAddress}}). 

在我致电cavv_user:change_email_address().之前,我需要启动cavv_user。我这样做是作为主管的simple_one_for_one孩子,如下:

主管:cavv_user_sup

-module(cavv_user_sup).

-behaviour(supervisor).

-define(CHILD(ChildName, Type, Args), {ChildName, {ChildName, start_link, Args}, temporary, 5000, Type, [ChildName]}).

start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

start_child(UserId) ->
    supervisor:start_child(?SERVER, [UserId]).

init([]) ->
    RestartStrategy = {simple_one_for_one, 1, 5},

    Children = [?CHILD(cavv_user, worker, [])],

    {ok, { RestartStrategy, Children} }.

我现在面临的问题是如何命令发送到cavv_user。我想确保首先使用start_child启动正确的用户,然后调用cavv_user:change_email_address().

我发现这是anwser,使用调度程序Erlang: what supervision tree should I end with writing a task scheduler?

所以我创建了一个命令调度程序,最后得到cavv_user_dispatchercavv_user_dispatcher_sup,后者又包含cavv_user_dispatcher和更早的cavv_user_sup

         cavv_user_dispatch_sup
            |               |
 cavv_user_dispatcher       |
       (gen_server)         |
                            |
                            |
                      cavv_user_sup
                        |   |   |
                 cavv_user_1...cavv_user_N

cavv_user_dispatcher

这很有效。

我现在面临的问题是,如何在cavv_user_dispatcher中正确编写代码?我遇到了代码重复的问题。如何正确调用start_child并调用适当的cavv_user API?

我应该使用某种Fun吗?

-module(cavv_user_dispatcher).

dispatch_command(UserId, Fun) ->
    gen_server:call(?SERVER, {dispatch_command, {UserId, Fun}}).

handle_call({dispatch_command, {UserId, Fun}}, _From, State) ->
    cavv_user_sup:start_child(UserId),
    Fun(), %% How to pass: cavv_user:change_email_address(..,..)?
    {reply, ok, State};

或像这样复制cavv_user的API?

-module(cavv_user_dispatcher).

change_user_email_address(UserId, EmailAddress) ->
    gen_server:call(?SERVER, {change_user_email_address, {UserId, EmailAddress}}).

handle_call({change_user_email_address, {UserId, EmailAddress}}, _From, State) ->
    cavv_user_sup:start_child(UserId),
    cavv_user:change_email_address(UserId, EmailAddress),
    {reply, ok, State};

或者我应该重新使用cavv_user中的命令记录到某种类型的工具来正确构建它们并传递它们?也许更好的方法来传递我想在cavv_user调用的函数?

我想尽可能以最好的Erlang方式解决问题,而不需要代码重复。

1 个答案:

答案 0 :(得分:1)

您的调度员是否应该处理其他命令?

  • 如果是,那么下一个命令将如何发生,我的意思是请求者是否知道用户的进程pid?

    • 如果是,则需要2个函数,一个用于创建用户,它将pid返回给请求者以进行下一次调用,另一个用于通过将命令发送到给定pid来处理下一个请求

    • 如果不是,那么你还需要2个函数,一个用于创建用户并将user_id与用户进程pid一起存储,另一个用于通过检索进程pid来处理下一个请求,然后将命令转发给它(我想这就是你想要做的事情。)

  • 如果不是,那么您不需要处理任何命令,并且应该在创建用户进程时直接传递电子邮件地址。请注意,这适用于所有情况,因为您需要一个不同的界面来创建用户。

我会用这种方式修改你的代码(没有经过测试,为时已晚:o)!)

-module(cavv_user_dispatcher).

create_user(UserId,UserMail) ->
    gen_server:call(?SERVER,{new_user,UserId,UserMail}).

% Args is a list of argument, empty if
% F needs only one argument (the user Pid)
dispatch_command(UserId, Fun, Args) ->  
    gen_server:call(?SERVER, {dispatch_command, {UserId, Fun,Args}}).

handle_call({dispatch_command, {UserId, Fun,Args}}, _From, State) ->
    Pid = get_pid(UserId,State),
    Answer = case Pid of
        unknown_user_id -> unknown_user_id;
        _ -> apply(Fun,[Pid|Args]),
             ok
    end,
    {reply, Answer, State};
handle_call({new_user,UserId,UserMail},_From,State) ->
    % verify that the user id does not already exists
    CheckId = check_id(UserId,State),
    {Answer,NewState} = case CheckId of 
        false -> {already_exist,State};
        true  -> {ok,Pid} = cavv_user_sup:start_child(UserId,UserMail)
                 {ok,[{UserId,Pid}|State]}
                 % State must be initialized as an empty list in the init function.
    {reply, Answer, NewState};
...
get_pid(UserId,State) ->
    proplists:get_value(UserId, State, unknown_user_id).
check_id(UserId,State) -> 
    not proplists:is_defined(UserId, State).

用户主管可以这样修改:

start_child(UserId,UserMail) -> % change arity in the export
supervisor:start_child(?SERVER, [UserId,UserMail]).

然后是用户服务器:

start_link(UserId,UserMail) ->
gen_server:start_link(?SERVER(UserId), ?MODULE, [UserId,UserMail],[]).

init([UserId,UserMail]) ->
    {ok,[{user_id,UserId},{user_mail,UserMail}]}.