snapkitty
developer-tools
c++
File size: 2,121 Bytes
0196580
 
 
30251bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
% SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
% Licensing: see LICENSE in the repository root.

%%%-------------------------------------------------------------------
%%% @doc HK-BROWSER :: supervises one `hk_browser_session' child per
%%% active session.
%%%
%%% Sessions are `simple_one_for_one', `transient': a session that
%%% terminates normally (an explicit `close_session') is not
%%% restarted, but a session whose Chromium process crashed
%%% (`hk_browser_session' stops with an abnormal reason, see its
%%% `handle_info' clause for `exit_status') *is* restarted with the
%%% same start arguments -- a fresh Chromium process comes up under
%%% the same `session_id'. This is the concrete demonstration the
%%% spec asks for: one browser session crashing never takes down
%%% `hk_agent_sup', `hk_search_sup', or any other session -- and,
%%% because this pool is its own supervisor nested under
%%% `hk_browser_sup' rather than sharing a supervisor with the
%%% session registry, restart storms in the session pool cannot
%%% (via `hk_browser_sup''s own intensity/period) take the registry
%%% down with them either.
%%% @end
%%%-------------------------------------------------------------------
-module(hk_browser_session_sup).
-behaviour(supervisor).

-export([start_link/0, init/1, start_session/1]).

start_link() ->
    hk_sup_util:start_link_retry(
        fun() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []) end).

%% @doc Start a new supervised browser session. `Args' is the map
%% `hk_browser_session:init/1' expects (`session_id', `owner_agent_id',
%% `profile').
-spec start_session(map()) -> {ok, pid()} | {error, term()}.
start_session(Args) ->
    supervisor:start_child(?MODULE, [Args]).

init([]) ->
    SupFlags = #{strategy => simple_one_for_one, intensity => 10, period => 60},
    Children = [
        #{id => hk_browser_session,
          start => {hk_browser_session, start_link, []},
          restart => transient,
          shutdown => 10000,
          type => worker,
          modules => [hk_browser_session]}
    ],
    {ok, {SupFlags, Children}}.