Erlang: how to properly send gen_server with start_child to the supervisor and call the API

I have gen_serverin my application, I must first start making a call. For this, I want to use the command manager. For a brief example, this is the API gen_server:

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}}). 

Before I can call cavv_user:change_email_address()., I need to run cavv_user. I do this as a child simple_one_for_onein a supervisor, for example:

supervisor: 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} }.

The problem I am facing is how to send commands to cavv_user. I want to make sure that the correct user is started first with start_child, and then callscavv_user:change_email_address().

anwser, : Erlang: ?

, cavv_user_dispatcher cavv_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 API cavv_user?

- 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};

API cavv_user ?

-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, .

+3
1

?

  • , , , pid ?

    • , 2 , , pid , pid

    • , : 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}]}.
+1

Source: https://habr.com/ru/post/1653463/


All Articles