Add src/hk_browser_session_sup.erl
Browse files
src/hk_browser_session_sup.erl
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
%%%-------------------------------------------------------------------
|
| 2 |
+
%%% @doc HK-BROWSER :: supervises one `hk_browser_session' child per
|
| 3 |
+
%%% active session.
|
| 4 |
+
%%%
|
| 5 |
+
%%% Sessions are `simple_one_for_one', `transient': a session that
|
| 6 |
+
%%% terminates normally (an explicit `close_session') is not
|
| 7 |
+
%%% restarted, but a session whose Chromium process crashed
|
| 8 |
+
%%% (`hk_browser_session' stops with an abnormal reason, see its
|
| 9 |
+
%%% `handle_info' clause for `exit_status') *is* restarted with the
|
| 10 |
+
%%% same start arguments -- a fresh Chromium process comes up under
|
| 11 |
+
%%% the same `session_id'. This is the concrete demonstration the
|
| 12 |
+
%%% spec asks for: one browser session crashing never takes down
|
| 13 |
+
%%% `hk_agent_sup', `hk_search_sup', or any other session -- and,
|
| 14 |
+
%%% because this pool is its own supervisor nested under
|
| 15 |
+
%%% `hk_browser_sup' rather than sharing a supervisor with the
|
| 16 |
+
%%% session registry, restart storms in the session pool cannot
|
| 17 |
+
%%% (via `hk_browser_sup''s own intensity/period) take the registry
|
| 18 |
+
%%% down with them either.
|
| 19 |
+
%%% @end
|
| 20 |
+
%%%-------------------------------------------------------------------
|
| 21 |
+
-module(hk_browser_session_sup).
|
| 22 |
+
-behaviour(supervisor).
|
| 23 |
+
|
| 24 |
+
-export([start_link/0, init/1, start_session/1]).
|
| 25 |
+
|
| 26 |
+
start_link() ->
|
| 27 |
+
hk_sup_util:start_link_retry(
|
| 28 |
+
fun() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []) end).
|
| 29 |
+
|
| 30 |
+
%% @doc Start a new supervised browser session. `Args' is the map
|
| 31 |
+
%% `hk_browser_session:init/1' expects (`session_id', `owner_agent_id',
|
| 32 |
+
%% `profile').
|
| 33 |
+
-spec start_session(map()) -> {ok, pid()} | {error, term()}.
|
| 34 |
+
start_session(Args) ->
|
| 35 |
+
supervisor:start_child(?MODULE, [Args]).
|
| 36 |
+
|
| 37 |
+
init([]) ->
|
| 38 |
+
SupFlags = #{strategy => simple_one_for_one, intensity => 10, period => 60},
|
| 39 |
+
Children = [
|
| 40 |
+
#{id => hk_browser_session,
|
| 41 |
+
start => {hk_browser_session, start_link, []},
|
| 42 |
+
restart => transient,
|
| 43 |
+
shutdown => 10000,
|
| 44 |
+
type => worker,
|
| 45 |
+
modules => [hk_browser_session]}
|
| 46 |
+
],
|
| 47 |
+
{ok, {SupFlags, Children}}.
|