summaryrefslogtreecommitdiff
path: root/src/plop_sup.erl
blob: c125cc036ce0408b0938fa4010f1e76406b04732 (plain)
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
%%% Copyright (c) 2014, NORDUnet A/S.
%%% See LICENSE for licensing information.

-module(plop_sup).
-behaviour(supervisor).

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

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

%% For testing.
start_in_shell(Args) ->
    {ok, Pid} = start_link(Args),
    unlink(Pid).


permanent_worker(Name, {Module, Function, Args}) ->
    permanent_worker(Name, {Module, Function, Args}, [Module]).

permanent_worker(Name, StartFunc, Modules) ->
    {Name,
     StartFunc,
     permanent,
     10000,
     worker, Modules}.

%% Supervisor callback
init([]) ->
    Services = application:get_env(plop, services, []),
    Children = [permanent_worker(the_db, {db, start_link, []}, [db]),
                permanent_worker(the_storagedb, {storagedb, start_link, []}),
                permanent_worker(fsync, {fsyncport, start_link, []})],
    OptionalChildren = lists:map(fun (ServiceName) ->
                                         case ServiceName of
                                             ht ->
                                                 permanent_worker(the_ht, {ht, start_link, []});
                                             sign ->
                                                 permanent_worker(the_signing, {sign, start_link, []})
                                         end
                                 end, Services),
    {ok, {{one_for_one, 3, 10},
          Children ++ OptionalChildren
         }}.