/
logmachine_sup.erl
147 lines (133 loc) · 6.04 KB
/
logmachine_sup.erl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
%%%-------------------------------------------------------------------
%%% @author <vjache@gmail.com>
%%% @copyright (C) 2011, Vyacheslav Vorobyov. All Rights Reserved.
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%% @doc
%%% TODO: Document it.
%%% @end
%%% Created : Nov 05, 2011
%%%-------------------------------------------------------------------------------
-module(logmachine_sup).
-behaviour(supervisor).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([start_link_main/0,
start_link_instance/1,
start_link_component/2,
start_link_sub_sup/0,
start_sub_child/4]).
%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([
init/1
]).
%% --------------------------------------------------------------------
%% Macros
%% --------------------------------------------------------------------
-define(SERVER, ?MODULE).
%% --------------------------------------------------------------------
%% Records
%% --------------------------------------------------------------------
%% ====================================================================
%% External functions
%% ====================================================================
start_link_main() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, root).
start_link_instance(InstanceName) ->
SupName=logmachine_util:make_name([InstanceName,sup]),
supervisor:start_link({local, SupName}, ?MODULE, {instance, InstanceName}).
start_link_component(ComponentName, InstanceName) ->
SupName=logmachine_util:make_name([InstanceName,ComponentName,sup]),
supervisor:start_link({local, SupName}, ?MODULE, {component, ComponentName, InstanceName}).
start_link_sub_sup() ->
RegName='logmachine.sub.sup',
supervisor:start_link({local, RegName}, ?MODULE, sub_sup).
start_sub_child(InstanceName, FromTimestamp, SubPid, Marker) ->
case supervisor:start_child('logmachine.sub.sup', [InstanceName, FromTimestamp, SubPid, Marker]) of
{ok, Child} -> Child;
{ok, Child, _} -> Child;
Unexpected -> throw({failed_to_start_subscription,
[{reason,Unexpected},
{instance,InstanceName},
{start_from,FromTimestamp},
{subscriber, SubPid},
{marker, Marker}]})
end.
%% ====================================================================
%% Server functions
%% ====================================================================
%% --------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, {SupFlags, [ChildSpec]}} |
%% ignore |
%% {error, Reason}
%% --------------------------------------------------------------------
% Root supervisor
init(root) ->
Instances=logmachine_app:get_env(instances,[]),
InstancesSups=
[{InstanceName,
{?MODULE,start_link_instance,[InstanceName]},
permanent, infinity, supervisor,[?MODULE]} || InstanceName <- proplists:get_keys(Instances)],
SubMan={sub_sup,
{?MODULE,start_link_sub_sup,[]},
permanent, infinity, supervisor,[?MODULE]},
{ok,{{one_for_one,0,1}, InstancesSups ++ [SubMan]}};
% Instance supervisor
init({instance, InstanceName}) ->
Components=[recorder, cache], % logmachine_receiver_srv
ComponentsSups=
[{Component,
{?MODULE,start_link_component,[Component,InstanceName]},
permanent, infinity, supervisor, [?MODULE]}|| Component <- Components],
Receiver={logmachine_receiver_srv,
{logmachine_receiver_srv,start_link,[InstanceName]},
permanent, 10000, worker, [logmachine_receiver_srv]},
Locator={logmachine_emlocator_srv,
{logmachine_emlocator_srv,start_link,[InstanceName]},
permanent, 10000, worker, [logmachine_emlocator_srv]},
{ok,{{one_for_one,0,1}, [Receiver | ComponentsSups] ++ [Locator]}};
% Recorder supervisor
init({component, recorder, InstanceName}) ->
Mod=logmachine_recorder_srv,
Recorder={logmachine_recorder,
{Mod,start_link_recorder,[InstanceName]},
permanent, 10000, worker, [Mod]},
Archiver={'logmachine_recorder/archiver',
{Mod,start_link_archiver,[InstanceName]},
permanent, 10000, worker, [Mod]},
{ok,{{one_for_one,0,1}, [Recorder, Archiver]}};
% Cache supervisor
init({component, cache, InstanceName}) ->
Mod=logmachine_cache_srv,
Cache={logmachine_cache,
{Mod,start_link_cache,[InstanceName]},
permanent, 10000, worker, [Mod]},
Evictor={'logmachine_cache/evictor',
{Mod,start_link_evictor,[InstanceName]},
permanent, 10000, worker, [Mod]},
{ok,{{one_for_one,0,1}, [Cache, Evictor]}};
% Subscription supervisor
init(sub_sup) ->
SubWorker = {logmachine_sub_srv, {logmachine_sub_srv, start_link, []},
temporary, 10000, worker, [logmachine_sub_srv]},
{ok,{{simple_one_for_one,0,1}, [SubWorker]}}.
%% ====================================================================
%% Internal functions
%% ====================================================================