Skip to content

Commit fbb86f9

Browse files
author
Arkady Drovosekov
committed
update stat on starting new job. It keeps max number of running
and queued jobs.
1 parent e7e2029 commit fbb86f9

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

src/ejobman_group_handler_cmd.erl

+3-1
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,12 @@ do_one_command(#egh{ch_queue=Q, ch_run=Ch, max=Max, group=Gid,
246246
conn=Conn, queue=Rqueue} = St, Len) ->
247247
{{value, Job}, Q2} = queue:out(Q),
248248
N = ejobman_rb:queue_len(Conn, Rqueue),
249+
Queued = N + queue:len(Q),
250+
ejobman_stat:upd_stat_t(Gid, Len, Queued),
249251
ejobman_stat:add(Job#job.id, 'from_queue',
250252
[{max, Max},
251253
{running, Len},
252-
{queued, N + queue:len(Q)},
254+
{queued, Queued},
253255
{group, Gid}]),
254256
New_ch = do_one_command_real(St, Ch, Job),
255257
St#egh{ch_queue=Q2, ch_run=New_ch}.

src/ejobman_handler.erl

+1-18
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]).
4040
-export([terminate/2, code_change/3]).
4141

42-
-export([stat_q/0, stat_t/0, stat_t/1]).
42+
-export([stat_q/0]).
4343

4444
%%%----------------------------------------------------------------------------
4545
%%% Defines
@@ -90,11 +90,6 @@ handle_call(stat_q, _From, St) ->
9090
Res = ejobman_print_stat:make_stat_cur_info(St),
9191
{reply, Res, St};
9292

93-
%% @doc returns time statistic
94-
handle_call({stat_t, Type}, _From, St) ->
95-
Res = ejobman_print_stat:make_stat_t_info(St, Type),
96-
{reply, Res, St};
97-
9893
handle_call(stop, _From, St) ->
9994
{stop, normal, ok, St};
10095
handle_call(status, _From, St) ->
@@ -190,18 +185,6 @@ start_link(Config) ->
190185
stop() ->
191186
gen_server:call(?MODULE, stop).
192187

193-
%%-----------------------------------------------------------------------------
194-
%%
195-
%% @doc asks ejobman_handler for time statistic
196-
%%
197-
-spec stat_t() -> string().
198-
199-
stat_t() ->
200-
stat_t(raw).
201-
202-
stat_t(Type) ->
203-
gen_server:call(?MODULE, {stat_t, Type}).
204-
205188
%%-----------------------------------------------------------------------------
206189
%%
207190
%% @doc asks ejobman_handler for state of queues

src/ejobman_stat.erl

+74
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
-export([terminate/2, code_change/3]).
4141
-export([add/2, add/3, add/5]).
4242
-export([get/2]).
43+
-export([stat_t/0, stat_t/1, add_stat_t/2, upd_stat_t/3, upd_stat_t/4]).
4344

4445
%%%----------------------------------------------------------------------------
4546
%%% Includes
@@ -80,6 +81,11 @@ handle_call(stop, _From, St) ->
8081
handle_call(status, _From, St) ->
8182
{reply, St, St};
8283

84+
%% @doc returns time statistic
85+
handle_call({stat_t, Type}, _From, St) ->
86+
Res = ejobman_print_stat:make_stat_t_info(St, Type),
87+
{reply, Res, St};
88+
8389
handle_call({get, Start, Stop}, _From, St) ->
8490
mpln_p_debug:pr({?MODULE, get1, ?LINE, Start, Stop}, St#est.debug, run, 2),
8591
Res = get_items(St, Start, Stop),
@@ -116,6 +122,12 @@ handle_cast({add_job, Time, Tag}, St) ->
116122
add_job_stat(Time, Tag),
117123
{noreply, St};
118124

125+
handle_cast({upd_job, Time, Tag, Work, Queued}, St) ->
126+
mpln_p_debug:pr({?MODULE, 'cast upd_job', ?LINE, Time, Tag},
127+
St#est.debug, run, 4),
128+
upd_job_stat(Time, Tag, Work, Queued),
129+
{noreply, St};
130+
119131
handle_cast(_Other, St) ->
120132
mpln_p_debug:pr({?MODULE, 'cast other', ?LINE, _Other},
121133
St#est.debug, run, 2),
@@ -232,6 +244,43 @@ add(Id1, Id2, Time, Now, Data) ->
232244
get(Start, Stop) ->
233245
gen_server:call(?MODULE, {get, Start, Stop}).
234246

247+
%%-----------------------------------------------------------------------------
248+
%%
249+
%% @doc asks ejobman_stat for time statistic
250+
%% @since 2012-02-02 14:09
251+
%%
252+
-spec stat_t() -> string().
253+
254+
stat_t() ->
255+
stat_t(raw).
256+
257+
stat_t(Type) ->
258+
gen_server:call(?MODULE, {stat_t, Type}).
259+
260+
%%-----------------------------------------------------------------------------
261+
%%
262+
%% @doc api call to add time statistic
263+
%% @since 2012-02-02 14:09
264+
%%
265+
-spec add_stat_t(tuple(), any()) -> ok.
266+
267+
add_stat_t(Time, Tag) ->
268+
gen_server:cast(?MODULE, {add_job, Time, Tag}).
269+
270+
%%
271+
%% @doc api call to update time statistic
272+
%% @since 2012-02-02 14:09
273+
%%
274+
-spec upd_stat_t(any(), non_neg_integer(), non_neg_integer()) -> ok.
275+
276+
upd_stat_t(Tag, Work, Queued) ->
277+
upd_stat_t(now(), Tag, Work, Queued).
278+
279+
-spec upd_stat_t(tuple(), any(), non_neg_integer(), non_neg_integer()) -> ok.
280+
281+
upd_stat_t(Time, Tag, Work, Queued) ->
282+
gen_server:cast(?MODULE, {upd_job, Time, Tag, Work, Queued}).
283+
235284
%%%----------------------------------------------------------------------------
236285
%%% Internal functions
237286
%%%----------------------------------------------------------------------------
@@ -342,11 +391,36 @@ add_item(#est{storage=S} = St, Id, {Time, Now}, Data) ->
342391
New = St#est{storage = [Item | S]},
343392
check_flush(New).
344393

394+
%%-----------------------------------------------------------------------------
395+
%%
396+
%% @doc updates items in job statistic (minute and hourly)
397+
%%
398+
-spec upd_job_stat(tuple(), any(), non_neg_integer(), non_neg_integer()) ->
399+
true.
400+
401+
upd_job_stat(Time, Tag, Work, Queued) ->
402+
upd_minute_job_stat(Time, Tag, Work, Queued),
403+
upd_hourly_job_stat(Time, Tag, Work, Queued).
404+
405+
upd_minute_job_stat(Time, Tag, Work, Queued) ->
406+
estat_misc:set_max_timed_stat(?STAT_TAB_M, 'minute', Time, {Tag, 'work'},
407+
Work),
408+
estat_misc:set_max_timed_stat(?STAT_TAB_M, 'minute', Time, {Tag, 'queued'},
409+
Queued).
410+
411+
upd_hourly_job_stat(Time, Tag, Work, Queued) ->
412+
estat_misc:set_max_timed_stat(?STAT_TAB_H, 'hour', Time, {Tag, 'work'},
413+
Work),
414+
estat_misc:set_max_timed_stat(?STAT_TAB_H, 'hour', Time, {Tag, 'queued'},
415+
Queued).
416+
417+
345418
%%-----------------------------------------------------------------------------
346419
%%
347420
%% @doc adds item to job statistic (minute and hourly)
348421
%%
349422
-spec add_job_stat(tuple(), any()) -> true.
423+
350424
add_job_stat(Time, Tag) ->
351425
add_minute_job_stat(Time, Tag),
352426
add_hourly_job_stat(Time, Tag).

0 commit comments

Comments
 (0)