-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathejobman_handler.erl
367 lines (320 loc) · 11.4 KB
/
ejobman_handler.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
%%%
%%% ejobman_handler: gen_server that handles messages from ejobman_receiver
%%%
%%% Copyright (c) 2011 Megaplan Ltd. (Russia)
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"),
%%% to deal in the Software without restriction, including without limitation
%%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%%% and/or sell copies of the Software, and to permit persons to whom
%%% the Software is furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included
%%% in all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
%%% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
%%% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
%%% CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
%%% TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
%%% SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%%%
%%% @author arkdro <[email protected]>
%%% @since 2011-07-15 10:00
%%% @license MIT
%%% @doc a gen_server that gets messages from ejobman_receiver and calls
%%% ejobman_child_supervisor to spawn a new child to do all the dirty work
%%%
-module(ejobman_handler).
-behaviour(gen_server).
%%%----------------------------------------------------------------------------
%%% Exports
%%%----------------------------------------------------------------------------
-export([start/0, start_link/0, stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]).
-export([terminate/2, code_change/3]).
-export([cmd/1, remove_child/1]).
-export([cmd_result/2]).
-export([get_job_log_filename/0]).
%%%----------------------------------------------------------------------------
%%% Includes
%%%----------------------------------------------------------------------------
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-include("ejobman.hrl").
-include("job.hrl").
-include("amqp_client.hrl").
%%%----------------------------------------------------------------------------
%%% gen_server callbacks
%%%----------------------------------------------------------------------------
init(_) ->
C = ejobman_conf:get_config_hdl(),
St = prepare_all(C),
% trap_exit is necessary because the job log is opened
% trap_exit is unnecessary. Children are ripped by supervisor
%process_flag(trap_exit, true),
mpln_p_debug:pr({?MODULE, 'init done', ?LINE}, St#ejm.debug, run, 1),
{ok, St, ?T}.
%%-----------------------------------------------------------------------------
%%
%% Handling call messages
%% @since 2011-07-15 11:00
%%
-spec handle_call(any(), any(), #ejm{}) ->
{noreply, #ejm{}, non_neg_integer()}
| {any(), any(), #ejm{}, non_neg_integer()}.
%% @doc returns job log file name
handle_call(get_job_log_filename, _From, St) ->
New = do_smth(St),
{reply, New#ejm.jlog_f, New, ?T};
%% @doc deletes disposable child from the state
handle_call({remove_child, Pid}, _From, St) ->
St_d = do_smth(St),
New = remove_child(St_d, Pid),
{reply, ok, New, ?T};
%% @doc calls disposable child
handle_call({cmd, Job}, From, St) ->
St_d = do_smth(St),
New = ejobman_handler_cmd:do_command(St_d, From, Job),
{noreply, New, ?T};
handle_call(stop, _From, St) ->
{stop, normal, ok, St};
handle_call(status, _From, St) ->
{reply, St, St, ?T};
handle_call(_N, _From, St) ->
mpln_p_debug:pr({?MODULE, 'other', ?LINE, _N}, St#ejm.debug, run, 2),
New = do_smth(St),
{reply, {error, unknown_request}, New, ?T}.
%%-----------------------------------------------------------------------------
%%
%% Handling cast messages
%% @since 2011-07-15 11:00
%%
-spec handle_cast(any(), #ejm{}) -> any().
handle_cast(stop, St) ->
{stop, normal, St};
handle_cast({cmd_result, Res, Id}, St) ->
mpln_p_debug:pr({?MODULE, 'cast cmd res', ?LINE, Id, Res}, St#ejm.debug, run, 4),
ejobman_handler_cmd:do_command_result(St, Res, Id),
New = do_smth(St),
{noreply, New, ?T};
handle_cast(_N, St) ->
mpln_p_debug:pr({?MODULE, 'cast other', ?LINE, _N}, St#ejm.debug, run, 2),
New = do_smth(St),
{noreply, New, ?T}.
%%-----------------------------------------------------------------------------
%%
%% @doc Note: it won't be called unless trap_exit is set
%%
terminate(_, State) ->
close_job_log(State),
mpln_p_debug:pr({?MODULE, 'terminate', ?LINE}, State#ejm.debug, run, 1),
ok.
%%-----------------------------------------------------------------------------
%%
%% Handling all non call/cast messages
%% @since 2011-07-15 11:00
%%
-spec handle_info(any(), #ejm{}) -> {noreply, #ejm{}, non_neg_integer()}.
handle_info(timeout, State) ->
mpln_p_debug:pr({?MODULE, info_timeout, ?LINE}, State#ejm.debug, run, 6),
New = do_smth(State),
{noreply, New, ?T};
handle_info(_Req, State) ->
mpln_p_debug:pr({?MODULE, other, ?LINE, _Req}, State#ejm.debug, run, 2),
New = do_smth(State),
{noreply, New, ?T}.
%%-----------------------------------------------------------------------------
code_change(_Old_vsn, State, _Extra) ->
{ok, State}.
%%%----------------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------------
-spec start() -> any().
%%
%% @doc starts handler gen_server
%% @since 2011-07-15 11:00
%%
start() ->
start_link().
%%-----------------------------------------------------------------------------
-spec start_link() -> any().
%%
%% @doc starts handler gen_server with pre-defined config
%% @since 2011-07-15 11:00
%%
start_link() ->
start_link(?CONF).
-spec start_link(string()) -> any().
%%
%% @doc starts handler gen_server with given config
%% @since 2011-07-15 11:00
%%
start_link(Config) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, Config, []).
%%-----------------------------------------------------------------------------
-spec stop() -> any().
%%
%% @doc stops handler gen_server
%% @since 2011-07-15 11:00
%%
stop() ->
gen_server:call(?MODULE, stop).
%%-----------------------------------------------------------------------------
%%
%% @doc calls any received command to be executed by disposable child
%% @since 2011-07-15 11:00
%%
-spec cmd(#job{}) -> ok.
cmd(Job) ->
gen_server:call(?MODULE, {cmd, Job}).
%%-----------------------------------------------------------------------------
%%
%% @doc sends message to server to log cmd result
%% @since 2011-07-15 11:00
%%
-spec cmd_result(tuple(), pid()) -> ok.
cmd_result(Res, Pid) ->
gen_server:cast(?MODULE, {cmd_result, Res, Pid}).
%%-----------------------------------------------------------------------------
%%
%% @doc asks ejobman_handler for job log file name
%%
-spec get_job_log_filename() -> string() | undefined.
get_job_log_filename() ->
gen_server:call(?MODULE, get_job_log_filename).
%%-----------------------------------------------------------------------------
%%
%% @doc asks ejobman_handler to remove child from the list
%%
-spec remove_child(pid()) -> ok.
remove_child(Pid) ->
gen_server:cast(?MODULE, {remove_child, Pid}).
%%%----------------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------------
%%
%% @doc removes child from the list of children
%%
-spec remove_child(#ejm{}, pid()) -> #ejm{}.
remove_child(#ejm{ch_data=Ch} = St, Pid) ->
F = fun(#chi{pid=X}) when X == Pid ->
false;
(_) ->
true
end,
New = lists:filter(F, Ch),
St#ejm{ch_data=New}.
%%-----------------------------------------------------------------------------
%%
%% @doc does miscellaneous periodic checks. E.g.: check for children. Returns
%% updated state.
%%
-spec do_smth(#ejm{}) -> #ejm{}.
do_smth(State) ->
St = job_log_rotate(State),
mpln_p_debug:pr({?MODULE, 'do_smth', ?LINE}, St#ejm.debug, run, 5),
Stc = check_children(St),
check_queued_commands(Stc).
%%-----------------------------------------------------------------------------
%%
%% @doc calls to process all the queued commands
%%
-spec check_queued_commands(#ejm{}) -> #ejm{}.
check_queued_commands(St) ->
ejobman_handler_cmd:do_short_commands(St).
%%-----------------------------------------------------------------------------
%%
%% @doc checks that all the children are alive. Returns new state with
%% live children only
%%
-spec check_children(#ejm{}) -> #ejm{}.
check_children(#ejm{ch_data=Ch} = State) ->
New = lists:filter(fun check_child/1, Ch),
State#ejm{ch_data = New}.
%%-----------------------------------------------------------------------------
%%
%% @doc checks whether the given child does something
%%
-spec check_child(#chi{}) -> boolean().
check_child(#chi{pid=Pid}) ->
case process_info(Pid, reductions) of
{reductions, _N} ->
true;
_ ->
false
end.
%%-----------------------------------------------------------------------------
%%
%% @doc prepares necessary things
%%
-spec prepare_all(#ejm{}) -> #ejm{}.
prepare_all(St) ->
prepare_job_log(St).
%%-----------------------------------------------------------------------------
%%
%% @doc prepares log for jobs and results
%%
-spec prepare_job_log(#ejm{}) -> #ejm{}.
prepare_job_log(#ejm{job_log=undefined} = St) ->
St;
prepare_job_log(#ejm{job_log=Base} = St) ->
File = mpln_misc_log:get_fname(Base),
filelib:ensure_dir(File),
case file:open(File, [raw, append, binary]) of
{ok, Fd} ->
St#ejm{jlog=Fd, jlog_f=File};
{error, Reason} ->
mpln_p_debug:pr({?MODULE, "prepare_job_log error", ?LINE, Reason},
St#ejm.debug, run, 0),
St#ejm{jlog_f=undefined}
end.
close_job_log(#ejm{jlog = undefined}) ->
ok;
close_job_log(#ejm{jlog = Fd}) ->
file:close(Fd).
%%-----------------------------------------------------------------------------
%%
%% @doc rotates job log only. Does not touch error log.
%%
-spec job_log_rotate(#ejm{}) -> #ejm{}.
job_log_rotate(#ejm{job_log_last=Last, job_log_rotate=Dur} = St) ->
case mpln_misc_log:need_rotate(Last, Dur) of
true ->
close_job_log(St),
St_f = prepare_job_log(St),
St_f#ejm{job_log_last = calendar:local_time()};
false ->
St
end.
%%%----------------------------------------------------------------------------
%%% EUnit tests
%%%----------------------------------------------------------------------------
-ifdef(TEST).
remove_child_test() ->
Pid = self(),
Me = #chi{pid=Pid, start=now()},
Ch = make_fake_children(),
St = #ejm{ch_data = [Me | Ch]},
?assert(#ejm{ch_data=Ch} =:= remove_child(St, Pid)).
check_mix_children_test() ->
Me = #chi{pid=self(), start=now()},
Ch = make_fake_children(),
St = #ejm{ch_data = [Me | Ch]},
?assert(#ejm{ch_data=[Me]} =:= check_children(St)).
check_fake_children_test() ->
Ch = make_fake_children(),
St = #ejm{ch_data = Ch},
?assert(#ejm{ch_data=[]} =:= check_children(St)).
make_fake_children() ->
L = [
"<0.12340.5678>",
"<0.32767.7136>",
"<0.7575.5433>"
],
lists:map(fun(X) -> #chi{pid=list_to_pid(X), start=now()} end, L).
-endif.
%%-----------------------------------------------------------------------------