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
|
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (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.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developers of the Original Code are LShift Ltd,
%% Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
%%
%% Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
%% Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
%% are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
%% Technologies LLC, and Rabbit Technologies Ltd.
%%
%% Portions created by LShift Ltd are Copyright (C) 2007-2010 LShift
%% Ltd. Portions created by Cohesive Financial Technologies LLC are
%% Copyright (C) 2007-2010 Cohesive Financial Technologies
%% LLC. Portions created by Rabbit Technologies Ltd are Copyright
%% (C) 2007-2010 Rabbit Technologies Ltd.
%%
%% All Rights Reserved.
%%
%% Contributor(s): ______________________________________.
%%
-module(rabbit_event).
-include("rabbit.hrl").
-export([init_stats_timer/0, ensure_stats_timer/3, stop_stats_timer/2]).
-export([ensure_stats_timer_after/2, reset_stats_timer_after/1]).
-export([stats_level/1]).
-export([notify/2]).
-ifdef(use_specs).
-type(level() :: 'none' | 'coarse' | 'fine').
-opaque(state() :: #state {
level :: level(),
timer :: atom()
}).
-type(timer_fun() :: fun (() -> 'ok')).
-spec(init_stats_timer/0 :: () -> state()).
-spec(ensure_stats_timer/3 :: (state(), timer_fun(), timer_fun()) -> state()).
-spec(stop_stats_timer/2 :: (state(), timer_fun()) -> state()).
-spec(ensure_stats_timer_after/2 :: (state(), timer_fun()) -> state()).
-spec(reset_stats_timer_after/1 :: (state()) -> 'ok').
-spec(stats_level/1 :: (state()) -> level()).
-spec(notify/2 :: (atom(), term()) -> 'ok').
-endif.
-record(state, {level, timer}).
%%----------------------------------------------------------------------------
init_stats_timer() ->
{ok, StatsLevel} = application:get_env(rabbit, collect_statistics),
#state{level = StatsLevel,
timer = undefined}.
ensure_stats_timer(State = #state{level = none}, _NowFun, _TimerFun) ->
State;
ensure_stats_timer(State = #state{timer = undefined}, NowFun, TimerFun) ->
NowFun(),
{ok, TRef} = timer:apply_interval(?STATS_INTERVAL,
erlang, apply, [TimerFun, []]),
State#state{timer = TRef};
ensure_stats_timer(State, _NowFun, _TimerFun) ->
State.
stop_stats_timer(State = #state{level = none}, _NowFun) ->
State;
stop_stats_timer(State = #state{timer = undefined}, _NowFun) ->
State;
stop_stats_timer(State = #state{timer = TRef}, NowFun) ->
{ok, cancel} = timer:cancel(TRef),
NowFun(),
State#state{timer = undefined}.
ensure_stats_timer_after(State = #state{level = none}, _TimerFun) ->
State;
ensure_stats_timer_after(State = #state{timer = undefined}, TimerFun) ->
{ok, TRef} = timer:apply_after(?STATS_INTERVAL,
erlang, apply, [TimerFun, []]),
State#state{timer = TRef};
ensure_stats_timer_after(State, _TimerFun) ->
State.
reset_stats_timer_after(State) ->
State#state{timer = undefined}.
stats_level(#state{level = Level}) ->
Level.
notify(Type, Props) ->
try
gen_event:notify(rabbit_event, #event{type = Type,
props = Props,
timestamp = os:timestamp()})
catch error:badarg ->
%% badarg means rabbit_event is no longer registered. We never
%% unregister it so the great likelihood is that we're shutting
%% down the broker but some events were backed up. Ignore it.
ok
end.
|