-
Notifications
You must be signed in to change notification settings - Fork 461
/
monitor_master.erl
369 lines (316 loc) · 13.4 KB
/
monitor_master.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
%%%-------------------------------------------------------------------
%%% @author Karl Anderson <karl@2600hz.com>
%%% @copyright (C) 2010, Karl Anderson
%%% @doc
%%% Responsible for managing the monitoring application
%%% @end
%%% Created : 11 Nov 2010 by Karl Anderson <karl@2600hz.com>
%%%-------------------------------------------------------------------
-module(monitor_master).
-behaviour(gen_server).
-include("monitor_amqp.hrl").
-include("monitor_couch.hrl").
-import(logger, [format_log/3]).
-import(proplists, [get_value/2, get_value/3]).
-import(whistle_util, [to_binary/1]).
%% API
-export([start_link/1]).
-export([set_amqp_host/1]).
-export([start_job/1, start_job/2, sync_jobs/0, sync_job/1]).
-export([rm_job/1, list_jobs/0, run_job/1]).
-export([set_interval/2, pause/1, resume/1]).
-export([update_task/4, rm_task/2, list_tasks/1]).
%% gen_server callbacks
-export([init/1, terminate/2, code_change/3]).
-export([handle_call/3, handle_cast/2, handle_info/2]).
-define(SERVER, ?MODULE).
-define(DEFAULT_INTERVAL, 300000).
-record(state, {
amqp_host = false :: string() | false
,monitor_q = false :: binary() | false
,database = ?MONITOR_DB :: string()
,db_view = ?MONITOR_VIEW :: string()
,jobs = [] :: list()
}).
-record(job, {
monitorRef = false :: reference() | false
,processID = false :: reference() | false
}).
%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link(AHost) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [AHost], []).
set_amqp_host(AHost) ->
gen_server:call(?SERVER, {set_amqp_host, AHost}, infinity).
start_job(Job_ID) ->
start_job(Job_ID, ?DEFAULT_INTERVAL).
start_job(Job_ID, Interval) ->
gen_server:call(?SERVER, {start_job, to_binary(Job_ID), Interval}, infinity).
sync_jobs() ->
gen_server:call(?SERVER, {sync_jobs}, infinity).
sync_job(Job_ID) ->
gen_server:call(?SERVER, {sync_job, to_binary(Job_ID)}, infinity).
rm_job(Job_ID) ->
gen_server:call(?SERVER, {rm_job, to_binary(Job_ID)}, infinity).
list_jobs() ->
gen_server:call(?SERVER, {list_jobs}, infinity).
run_job(Job_ID) ->
gen_server:call(?SERVER, {run_job, to_binary(Job_ID)}, infinity).
set_interval(Job_ID, Interval) ->
gen_server:call(?SERVER, {set_interval, to_binary(Job_ID), Interval}, infinity).
pause(Job_ID) ->
gen_server:call(?SERVER, {pause, to_binary(Job_ID)}, infinity).
resume(Job_ID) ->
gen_server:call(?SERVER, {resume, to_binary(Job_ID)}, infinity).
update_task(Job_ID, Task_ID, Type, Options) ->
gen_server:call(?SERVER, {update_task, to_binary(Job_ID), Task_ID, Type, Options}, infinity).
rm_task(Job_ID, Task_ID) ->
gen_server:call(?SERVER, {rm_task, to_binary(Job_ID), Task_ID}, infinity).
list_tasks(Job_ID) ->
gen_server:call(?SERVER, {list_tasks, to_binary(Job_ID)}, infinity).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([AHost]) ->
couch_mgr:add_change_handler(?MONITOR_DB, <<"">>),
couch_mgr:load_doc_from_file(?MONITOR_DB, monitor, "monitor.json"),
{ok, #state{amqp_host = AHost}}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call({set_amqp_host, AHost}, _From, #state{amqp_host = CurAHost} = State) ->
format_log(info, "MONITOR_MASTER(~p): Updating amqp host from ~p to ~p", [self(), CurAHost, AHost]),
%% Update the AMQP host of all the JOB
lists:foreach(fun({_,Pid,_,_}) ->
gen_server:call(Pid, {set_amqp_host, AHost}, infinity)
end, supervisor:which_children(monitor_job_sup)),
%% Update the AMQP host of all the Agents
lists:foreach(fun({_,Pid,_,_}) ->
gen_server:call(Pid, {set_amqp_host, AHost}, infinity)
end, supervisor:which_children(monitor_agent_sup)),
{reply, ok, State#state{amqp_host = AHost}};
handle_call({start_job, Job_ID, Interval}, _From, State) ->
case ensure_running(Job_ID, State, Interval) of
{_Pid, NewState} ->
{reply, ok, NewState};
_ ->
{reply, error, State}
end;
handle_call({sync_jobs}, _From, #state{jobs = CurJobs} = State) ->
case get_jobs(State) of
{error, _Error} = E ->
{reply, E, State};
[] ->
lists:foreach(fun({Job_ID, _Job}) -> spawn(fun() -> rm_job(Job_ID) end) end, CurJobs),
{reply, ok, State};
Jobs ->
{NewState, Zombies} = lists:foldl(fun({Job}, {StateIn, ZombiesIn}) ->
Job_ID = get_value(<<"id">>, Job),
ZombiesOut = lists:delete(to_binary(Job_ID), ZombiesIn),
case ensure_running(Job_ID, StateIn) of
{Pid, StateOut} ->
gen_server:call(Pid, {sync, get_value(<<"value">>, Job, [])}, infinity),
{StateOut, ZombiesOut};
_ ->
{StateIn, ZombiesOut}
end end, {State, proplists:get_keys(State#state.jobs)}, Jobs),
lists:foreach(fun(Job_ID) -> spawn(fun() -> rm_job(Job_ID) end) end, Zombies),
{reply, ok, NewState}
end;
handle_call({sync_job, Job_ID}, _From, State) ->
case get_jobs(State, Job_ID) of
{error, _Error} = E ->
{reply, E, State};
[] ->
spawn(fun() -> rm_job(Job_ID) end),
{reply, ok, State};
[{struct, Job} ] ->
case ensure_running(Job_ID, State) of
{Pid, NewState} ->
gen_server:call(Pid, {sync, get_value(<<"value">>, Job, [])}, infinity),
format_log(info, "~p", get_value(<<"value">>, Job, [])),
{reply, ok, NewState};
_ ->
{reply, ok, State}
end
end;
handle_call({rm_job, Job_ID}, _From, #state{jobs = Jobs} = State) ->
case get_value(Job_ID, Jobs) of
#job{processID = Pid} ->
{reply, {ok, Pid ! stop}, State};
undefined ->
{reply, {ok, not_running}, State}
end;
handle_call({list_jobs}, _From, #state{jobs = Jobs} = State) ->
{reply, Jobs, State};
handle_call({set_interval, Job_ID, Interval}, _From, #state{jobs = Jobs} = State) ->
{reply, msg_job(Job_ID, Jobs, {set_interval, Interval}), State};
handle_call({pause, Job_ID}, _From, #state{jobs = Jobs} = State) ->
{reply, msg_job(Job_ID, Jobs, {pause}), State};
handle_call({resume, Job_ID}, _From, #state{jobs = Jobs} = State) ->
{reply, msg_job(Job_ID, Jobs, {resume}), State};
handle_call({update_task, Job_ID, Task_ID, Type, Options}, _From, #state{jobs = Jobs} = State) ->
{reply, msg_job(Job_ID, Jobs, {update_task, Task_ID, Type, Options}), State};
handle_call({rm_task, Job_ID, Task_ID}, _From, #state{jobs = Jobs} = State) ->
{reply, msg_job(Job_ID, Jobs, {rm_task, Task_ID}), State};
handle_call({list_tasks, Job_ID}, _From, #state{jobs = Jobs} = State) ->
{reply, msg_job(Job_ID, Jobs, {list_tasks}), State};
handle_call({run_job, Job_ID}, _From, #state{jobs = Jobs} = State) ->
{reply, msg_job(Job_ID, Jobs, {run_job}), State};
handle_call(_Request, _From, State) ->
{reply, ignored, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info({'DOWN', _MonitorRef, _Type, Job_PID, _Info}, #state{jobs = CurJobs} = State) ->
Jobs = lists:filter(fun({_, #job{processID = Pid}}) -> Pid /= Job_PID end, CurJobs),
format_log(info, "MONITOR_MASTER(~p): Job PID ~p went down (~p)", [self(), Job_PID, _Info]),
{noreply, State#state{jobs = Jobs}};
handle_info({document_deleted, Doc_ID}, State) ->
spawn(fun() -> rm_job(Doc_ID) end),
{noreply, State};
handle_info({document_changes, Doc_ID, _Changes}, State) ->
spawn(fun() -> sync_job(Doc_ID) end),
{noreply, State};
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
format_log(error, "MONITOR_MASTER(~p): Going down (~p)...", [self(), _Reason]),
ok.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Looks up job definitions from the database with an optional
%% key, if provided will return only definitions for that key
%%
%% @spec(get_jobs/2 :: (State :: #state{}, Key :: string) ->
%% touple(error, atom()) | proplist()).
%% @end
%%--------------------------------------------------------------------
get_jobs(State) ->
get_jobs(State, "").
get_jobs(#state{database = DB, db_view = View}, Key) ->
Options = case to_binary(Key) of <<>> -> []; K -> [{"key", K}] end,
case couch_mgr:get_results(DB, View, Options) of
{error, _}=E ->
format_log(info, "MONITOR_MASTER(~p): Result not found (~p)", [self(), E]),
E;
{ok, Jobs} ->
format_log(info, "MONITOR_MASTER(~p): Found ~p jobs in the database ~p using options ~p", [self(), length(Jobs), DB, Options]),
Jobs
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Messages a job server
%%
%% @spec(msg_job/2 :: (State :: binary(), Jobs :: proplist(),
%% Msg :: any()) -> touple(ok, Msg) | tuple(error, job_not_found).
%% @end
%%--------------------------------------------------------------------
msg_job(Job_ID, Jobs, Msg) ->
case get_value(Job_ID, Jobs) of
#job{processID = Pid} ->
{ok, gen_server:call(Pid, Msg, infinity)};
undefined ->
{error, job_not_found}
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Finds the PID of a running job or starts a new job serve if it
%% does not exist
%%
%% @spec(ensure_running/3 :: (Job_ID :: binary, State :: binary(),
%% Interval :: pos_integer()) ->
%% tuple(pid(), #state{}) | tuple(error, #state{}).
%% @end
%%--------------------------------------------------------------------
ensure_running(Job_ID, State) ->
ensure_running(Job_ID, State, ?DEFAULT_INTERVAL).
ensure_running(Job_ID, #state{jobs = Jobs, amqp_host = AHost} = State, Interval) ->
case get_value(Job_ID, Jobs) of
#job{processID = Pid} ->
{Pid, State};
undefined ->
case monitor_job_sup:start_job(Job_ID, AHost, Interval) of
{ok, Pid} ->
Job = #job{processID = Pid, monitorRef = erlang:monitor(process, Pid)},
{Pid, State#state{jobs = [{Job_ID, Job}|Jobs]}};
_Else ->
{error, State}
end
end.