random tick

This commit is contained in:
Ery Lee 2015-03-08 19:19:32 +08:00
parent 00f39607f1
commit b26f6f1b1d
2 changed files with 15 additions and 13 deletions

View File

@ -79,6 +79,7 @@ datetime() ->
%% gen_server Function Definitions %% gen_server Function Definitions
%% ------------------------------------------------------------------ %% ------------------------------------------------------------------
init([Options]) -> init([Options]) ->
random:seed(now()),
SysInterval = proplists:get_value(sys_interval, Options, 60), SysInterval = proplists:get_value(sys_interval, Options, 60),
% Create $SYS Topics % Create $SYS Topics
[{atomic, _} = create(systop(Name)) || Name <- ?SYSTOP_BROKERS], [{atomic, _} = create(systop(Name)) || Name <- ?SYSTOP_BROKERS],
@ -88,8 +89,8 @@ init([Options]) ->
[ets:insert(?TABLE, {Name, 0}) || Name <- ?SYSTOP_CLIENTS], [ets:insert(?TABLE, {Name, 0}) || Name <- ?SYSTOP_CLIENTS],
[ets:insert(?TABLE, {Name, 0}) || Name <- ?SYSTOP_PUBSUB], [ets:insert(?TABLE, {Name, 0}) || Name <- ?SYSTOP_PUBSUB],
% retain version, description % retain version, description
gen_server:cast(self(), prepare), State = #state{started_at = os:timestamp(), sys_interval = SysInterval},
{ok, tick(#state{started_at = os:timestamp(), sys_interval = SysInterval})}. {ok, tick(random:uniform(SysInterval), State)}.
handle_call(uptime, _From, State) -> handle_call(uptime, _From, State) ->
{reply, uptime(State), State}; {reply, uptime(State), State};
@ -97,15 +98,12 @@ handle_call(uptime, _From, State) ->
handle_call(_Request, _From, State) -> handle_call(_Request, _From, State) ->
{reply, ok, State}. {reply, ok, State}.
handle_cast(prepare, State) ->
retain(systop(version), list_to_binary(version())),
retain(systop(description), list_to_binary(description())),
{noreply, State};
handle_cast(_Msg, State) -> handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State}.
handle_info(tick, State) -> handle_info(tick, State) ->
retain(systop(version), list_to_binary(version())),
retain(systop(description), list_to_binary(description())),
publish(systop(uptime), list_to_binary(uptime(State))), publish(systop(uptime), list_to_binary(uptime(State))),
publish(systop(datetime), list_to_binary(datetime())), publish(systop(datetime), list_to_binary(datetime())),
%%TODO... call emqtt_cm here? %%TODO... call emqtt_cm here?
@ -161,7 +159,10 @@ uptime(days, D) ->
[integer_to_list(D), " days,"]. [integer_to_list(D), " days,"].
tick(State = #state{sys_interval = SysInterval}) -> tick(State = #state{sys_interval = SysInterval}) ->
State#state{tick_timer = erlang:send_after(SysInterval * 1000, self(), tick)}. tick(SysInterval, State).
tick(Delay, State) ->
State#state{tick_timer = erlang:send_after(Delay * 1000, self(), tick)}.
i2b(I) when is_integer(I) -> i2b(I) when is_integer(I) ->
list_to_binary(integer_to_list(I)). list_to_binary(integer_to_list(I)).

View File

@ -142,6 +142,7 @@ key(Metric) ->
%% gen_server Function Definitions %% gen_server Function Definitions
%% ------------------------------------------------------------------ %% ------------------------------------------------------------------
init(Options) -> init(Options) ->
random:seed(now()),
Topics = ?SYSTOP_BYTES ++ ?SYSTOP_PACKETS ++ ?SYSTOP_MESSAGES, Topics = ?SYSTOP_BYTES ++ ?SYSTOP_PACKETS ++ ?SYSTOP_MESSAGES,
% $SYS Topics for metrics % $SYS Topics for metrics
[{atomic, _} = emqtt_pubsub:create(systop(Topic)) || Topic <- Topics], [{atomic, _} = emqtt_pubsub:create(systop(Topic)) || Topic <- Topics],
@ -150,10 +151,7 @@ init(Options) ->
% Init metrics % Init metrics
[new_metric(Topic) || Topic <- Topics], [new_metric(Topic) || Topic <- Topics],
PubInterval = proplists:get_value(pub_interval, Options, 60), PubInterval = proplists:get_value(pub_interval, Options, 60),
{ok, tick(#state{pub_interval = PubInterval}), hibernate}. {ok, tick(random:uniform(PubInterval), #state{pub_interval = PubInterval}), hibernate}.
handle_call(get_metrics, _From, State) ->
{reply, [], State};
handle_call(_Request, _From, State) -> handle_call(_Request, _From, State) ->
{reply, ok, State}. {reply, ok, State}.
@ -190,7 +188,10 @@ new_metric(Name) ->
[ets:insert(?TABLE, {{Name, I}, 0}) || I <- Schedulers]. [ets:insert(?TABLE, {{Name, I}, 0}) || I <- Schedulers].
tick(State = #state{pub_interval = PubInterval}) -> tick(State = #state{pub_interval = PubInterval}) ->
State#state{tick_timer = erlang:send_after(PubInterval * 1000, self(), tick)}. tick(PubInterval, State).
tick(Delay, State) ->
State#state{tick_timer = erlang:send_after(Delay * 1000, self(), tick)}.
i2b(I) -> i2b(I) ->
list_to_binary(integer_to_list(I)). list_to_binary(integer_to_list(I)).