summaryrefslogtreecommitdiff
path: root/src/plop_sup.erl
blob: 442eabc96dd76f4f7c8873e00890bd363a944974 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
%%% Copyright (c) 2014-2015, 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}.

perm_database_children(DB) ->
    lists:filtermap(fun ({Name, DBName, ConfigName}) ->
                            case application:get_env(plop, ConfigName) of
                                {ok, Path} ->
                                    {true, permanent_worker(Name, {perm, start_link, [DBName, Path]})};
                                undefined ->
                                    false
                            end
                    end, DB).

%% Supervisor callback
init([]) ->
    Services = application:get_env(plop, services, []),
    DBChildren = perm_database_children([
                                         {the_entryhash_db, entryhash_db, entryhash_root_path},
                                         {the_indexforhash_db, indexforhash_db, indexforhash_root_path},
                                         {the_entry_db, entry_db, entry_root_path}
                                        ]),
    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},
          DBChildren ++ Children ++ OptionalChildren
         }}.