Source

chut / src / usr_sup.erl

Full commit
%%%===================================================================
%%% Supervises a given user and acts as an entry point for the internal
%%% FSM to kill the user.
%%%===================================================================
-module(usr_sup).
-behaviour(supervisor).

%% supervisor callback
-export([init/1]).
%% API
-export([start_link/2]).

start_link(UserId, TimeOut) ->
    {ok, Pid} = supervisor:start_link({global, UserId}, ?MODULE, [UserId, TimeOut]),
    %% unlink the process so the one that starts it (usually a connection)
    %% doesn't bring it down when it ends. Should be unnecessary when an
    %% otp structure is added.
    erlang:unlink(Pid).


%%--------------------------------------------------------------------
%% Function: init([UserId, Timeout]) -> [ChildSpec]
%% Description: Creates the children specification for the event
%% manager and the fsm constituting a user. A one for all restart
%% strategy is used because only if the FSM and the event handler can
%% a user work properly.
%%--------------------------------------------------------------------
init([UserId, TimeOut]) ->
    MonitorId = {monitor, UserId},
    ManagerId = {manager, UserId},
    MonitorSpec = {MonitorId,
                   {usr_monitor, start_link, [UserId, ManagerId, TimeOut]},
                   transient,
                   TimeOut,
                   worker,
                   [usr_monitor]},
    ManagerSpec = {ManagerId,
                   {usr_manager, start_link, [UserId]},
                   transient,
                   TimeOut,
                   worker,
                   dynamic},
    {ok, {{one_for_all, 1, TimeOut},
         [MonitorSpec, ManagerSpec]}}.