From bbc2a73e09667b5eafd4c1f9fdb0da6edf27b6e8 Mon Sep 17 00:00:00 2001 From: Peter Lemenkov Date: Mon, 11 Mar 2013 17:17:37 +0400 Subject: [PATCH 1/1] Proper file layout Signed-off-by: Peter Lemenkov --- ebin/riak_sysmon.app | 21 ----- example/riak_sysmon_example_handler.erl | 140 ++++++++++++++++++++++++++++ src/riak_sysmon.app.src | 15 +++ src/riak_sysmon_example_handler.erl | 140 ---------------------------- src/riak_sysmon_testhandler.erl | 158 -------------------------------- test/riak_sysmon_testhandler.erl | 158 ++++++++++++++++++++++++++++++++ 6 files changed, 313 insertions(+), 319 deletions(-) delete mode 100644 ebin/riak_sysmon.app create mode 100644 example/riak_sysmon_example_handler.erl create mode 100644 src/riak_sysmon.app.src delete mode 100644 src/riak_sysmon_example_handler.erl delete mode 100644 src/riak_sysmon_testhandler.erl create mode 100644 test/riak_sysmon_testhandler.erl diff --git a/ebin/riak_sysmon.app b/ebin/riak_sysmon.app deleted file mode 100644 index 57d2ba5..0000000 --- a/ebin/riak_sysmon.app +++ /dev/null @@ -1,21 +0,0 @@ -{application, riak_sysmon, - [ - {description, "Rate-limiting system_monitor event handler"}, - {vsn, "1.1.3"}, - {modules, [ - riak_sysmon_app, - riak_sysmon_example_handler, - riak_sysmon_filter, - riak_sysmon_sup, - riak_sysmon_testhandler - ]}, - {applications, [ - kernel, - stdlib, - sasl - ]}, - {registered, []}, - {mod, {riak_sysmon_app, []}}, - {env, [ - ]} - ]}. diff --git a/example/riak_sysmon_example_handler.erl b/example/riak_sysmon_example_handler.erl new file mode 100644 index 0000000..a5d7b6f --- /dev/null +++ b/example/riak_sysmon_example_handler.erl @@ -0,0 +1,140 @@ +%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved. +%% +%% This file is provided to you under the Apache License, +%% Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, +%% software distributed under the License is distributed on an +%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +%% KIND, either express or implied. See the License for the +%% specific language governing permissions and limitations +%% under the License. + +%% @doc A simple example for adding a custom event handler (this module) +%% to the `riak_sysmon' application's `system_monitor' event manager. + +-module(riak_sysmon_example_handler). + +-behaviour(gen_event). + +%% API +-export([add_handler/0, get_call_count/0]). + +%% gen_event callbacks +-export([init/1, handle_event/2, handle_call/2, + handle_info/2, terminate/2, code_change/3]). + +-record(state, { + count = 0 :: integer() + }). + +%%%=================================================================== +%%% gen_event callbacks +%%%=================================================================== + +add_handler() -> + riak_sysmon_filter:add_custom_handler(?MODULE, []). + +get_call_count() -> + riak_sysmon_filter:call_custom_handler(?MODULE, get_call_count, infinity). + +%%%=================================================================== +%%% gen_event callbacks +%%%=================================================================== + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever a new event handler is added to an event manager, +%% this function is called to initialize the event handler. +%% +%% @spec init(Args) -> {ok, State} +%% @end +%%-------------------------------------------------------------------- +init([]) -> + {ok, #state{}}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever an event manager receives an event sent using +%% gen_event:notify/2 or gen_event:sync_notify/2, this function is +%% called for each installed event handler to handle the event. +%% +%% @spec handle_event(Event, State) -> +%% {ok, State} | +%% {swap_handler, Args1, State1, Mod2, Args2} | +%% remove_handler +%% @end +%%-------------------------------------------------------------------- +handle_event(Event, #state{count = Count} = State) -> + error_logger:info_msg("Example: event ~P\n", [Event, 20]), + {ok, State#state{count = Count + 1}}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever an event manager receives a request sent using +%% gen_event:call/3,4, this function is called for the specified +%% event handler to handle the request. +%% +%% @spec handle_call(Request, State) -> +%% {ok, Reply, State} | +%% {swap_handler, Reply, Args1, State1, Mod2, Args2} | +%% {remove_handler, Reply} +%% @end +%%-------------------------------------------------------------------- +handle_call(get_call_count, State) -> + Reply = State#state.count, + {ok, Reply, State}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% This function is called for each installed event handler when +%% an event manager receives any other message than an event or a +%% synchronous request (or a system message). +%% +%% @spec handle_info(Info, State) -> +%% {ok, State} | +%% {swap_handler, Args1, State1, Mod2, Args2} | +%% remove_handler +%% @end +%%-------------------------------------------------------------------- +handle_info(Info, State) -> + error_logger:info_msg("Example: FYI, handle_info got ~p\n", [Info]), + {ok, State}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever an event handler is deleted from an event manager, this +%% function is called. It should be the opposite of Module:init/1 and +%% do any necessary cleaning up. +%% +%% @spec terminate(Reason, State) -> void() +%% @end +%%-------------------------------------------------------------------- +terminate(Reason, State) -> + error_logger:info_msg("Example: stopping because ~p, my count ~p\n", + [Reason, State#state.count]), + 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 +%%%=================================================================== diff --git a/src/riak_sysmon.app.src b/src/riak_sysmon.app.src new file mode 100644 index 0000000..3b31fba --- /dev/null +++ b/src/riak_sysmon.app.src @@ -0,0 +1,15 @@ +{application, riak_sysmon, + [ + {description, "Rate-limiting system_monitor event handler"}, + {vsn, "1.1.3"}, + {modules, []}, + {applications, [ + kernel, + stdlib, + sasl + ]}, + {registered, []}, + {mod, {riak_sysmon_app, []}}, + {env, [ + ]} + ]}. diff --git a/src/riak_sysmon_example_handler.erl b/src/riak_sysmon_example_handler.erl deleted file mode 100644 index a5d7b6f..0000000 --- a/src/riak_sysmon_example_handler.erl +++ /dev/null @@ -1,140 +0,0 @@ -%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved. -%% -%% This file is provided to you under the Apache License, -%% Version 2.0 (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.apache.org/licenses/LICENSE-2.0 -%% -%% Unless required by applicable law or agreed to in writing, -%% software distributed under the License is distributed on an -%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -%% KIND, either express or implied. See the License for the -%% specific language governing permissions and limitations -%% under the License. - -%% @doc A simple example for adding a custom event handler (this module) -%% to the `riak_sysmon' application's `system_monitor' event manager. - --module(riak_sysmon_example_handler). - --behaviour(gen_event). - -%% API --export([add_handler/0, get_call_count/0]). - -%% gen_event callbacks --export([init/1, handle_event/2, handle_call/2, - handle_info/2, terminate/2, code_change/3]). - --record(state, { - count = 0 :: integer() - }). - -%%%=================================================================== -%%% gen_event callbacks -%%%=================================================================== - -add_handler() -> - riak_sysmon_filter:add_custom_handler(?MODULE, []). - -get_call_count() -> - riak_sysmon_filter:call_custom_handler(?MODULE, get_call_count, infinity). - -%%%=================================================================== -%%% gen_event callbacks -%%%=================================================================== - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever a new event handler is added to an event manager, -%% this function is called to initialize the event handler. -%% -%% @spec init(Args) -> {ok, State} -%% @end -%%-------------------------------------------------------------------- -init([]) -> - {ok, #state{}}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever an event manager receives an event sent using -%% gen_event:notify/2 or gen_event:sync_notify/2, this function is -%% called for each installed event handler to handle the event. -%% -%% @spec handle_event(Event, State) -> -%% {ok, State} | -%% {swap_handler, Args1, State1, Mod2, Args2} | -%% remove_handler -%% @end -%%-------------------------------------------------------------------- -handle_event(Event, #state{count = Count} = State) -> - error_logger:info_msg("Example: event ~P\n", [Event, 20]), - {ok, State#state{count = Count + 1}}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever an event manager receives a request sent using -%% gen_event:call/3,4, this function is called for the specified -%% event handler to handle the request. -%% -%% @spec handle_call(Request, State) -> -%% {ok, Reply, State} | -%% {swap_handler, Reply, Args1, State1, Mod2, Args2} | -%% {remove_handler, Reply} -%% @end -%%-------------------------------------------------------------------- -handle_call(get_call_count, State) -> - Reply = State#state.count, - {ok, Reply, State}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% This function is called for each installed event handler when -%% an event manager receives any other message than an event or a -%% synchronous request (or a system message). -%% -%% @spec handle_info(Info, State) -> -%% {ok, State} | -%% {swap_handler, Args1, State1, Mod2, Args2} | -%% remove_handler -%% @end -%%-------------------------------------------------------------------- -handle_info(Info, State) -> - error_logger:info_msg("Example: FYI, handle_info got ~p\n", [Info]), - {ok, State}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever an event handler is deleted from an event manager, this -%% function is called. It should be the opposite of Module:init/1 and -%% do any necessary cleaning up. -%% -%% @spec terminate(Reason, State) -> void() -%% @end -%%-------------------------------------------------------------------- -terminate(Reason, State) -> - error_logger:info_msg("Example: stopping because ~p, my count ~p\n", - [Reason, State#state.count]), - 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 -%%%=================================================================== diff --git a/src/riak_sysmon_testhandler.erl b/src/riak_sysmon_testhandler.erl deleted file mode 100644 index 74f77ba..0000000 --- a/src/riak_sysmon_testhandler.erl +++ /dev/null @@ -1,158 +0,0 @@ -%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved. -%% -%% This file is provided to you under the Apache License, -%% Version 2.0 (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.apache.org/licenses/LICENSE-2.0 -%% -%% Unless required by applicable law or agreed to in writing, -%% software distributed under the License is distributed on an -%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -%% KIND, either express or implied. See the License for the -%% specific language governing permissions and limitations -%% under the License. - -%% @doc Support for EUnit testing: simple event-collecting event handler. - --module(riak_sysmon_testhandler). - --ifdef(TEST). - --behaviour(gen_event). - -%% API --export([start_link/0, add_handler/1, get_events/1]). - -%% gen_event callbacks --export([init/1, handle_event/2, handle_call/2, - handle_info/2, terminate/2, code_change/3]). - --record(state, { - list = [] :: list() - }). - -%%%=================================================================== -%%% gen_event callbacks -%%%=================================================================== - -%%-------------------------------------------------------------------- -%% @doc -%% Creates an event manager -%% -%% @spec start_link() -> {ok, Pid} | {error, Error} -%% @end -%%-------------------------------------------------------------------- -start_link() -> - gen_event:start_link({local, ?MODULE}). - -%%-------------------------------------------------------------------- -%% @doc -%% Adds an event handler -%% -%% @spec add_handler(gen_event:emgr_name()) -> ok | {'EXIT', Reason} | term() -%% @end -%%-------------------------------------------------------------------- -add_handler(EventServer) -> - gen_event:add_sup_handler(EventServer, ?MODULE, []). - -get_events(EventServer) -> - gen_event:call(EventServer, ?MODULE, get_events). - -%%%=================================================================== -%%% gen_event callbacks -%%%=================================================================== - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever a new event handler is added to an event manager, -%% this function is called to initialize the event handler. -%% -%% @spec init(Args) -> {ok, State} -%% @end -%%-------------------------------------------------------------------- -init([]) -> - {ok, #state{}}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever an event manager receives an event sent using -%% gen_event:notify/2 or gen_event:sync_notify/2, this function is -%% called for each installed event handler to handle the event. -%% -%% @spec handle_event(Event, State) -> -%% {ok, State} | -%% {swap_handler, Args1, State1, Mod2, Args2} | -%% remove_handler -%% @end -%%-------------------------------------------------------------------- -handle_event(Event, #state{list = List} = State) -> - {ok, State#state{list = [Event|List]}}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever an event manager receives a request sent using -%% gen_event:call/3,4, this function is called for the specified -%% event handler to handle the request. -%% -%% @spec handle_call(Request, State) -> -%% {ok, Reply, State} | -%% {swap_handler, Reply, Args1, State1, Mod2, Args2} | -%% {remove_handler, Reply} -%% @end -%%-------------------------------------------------------------------- -handle_call(get_events, State) -> - {ok, lists:reverse(State#state.list), State#state{list = []}}; -handle_call(_Request, State) -> - Reply = not_supported, - {ok, Reply, State}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% This function is called for each installed event handler when -%% an event manager receives any other message than an event or a -%% synchronous request (or a system message). -%% -%% @spec handle_info(Info, State) -> -%% {ok, State} | -%% {swap_handler, Args1, State1, Mod2, Args2} | -%% remove_handler -%% @end -%%-------------------------------------------------------------------- -handle_info(_Info, State) -> - {ok, State}. - -%%-------------------------------------------------------------------- -%% @private -%% @doc -%% Whenever an event handler is deleted from an event manager, this -%% function is called. It should be the opposite of Module:init/1 and -%% do any necessary cleaning up. -%% -%% @spec terminate(Reason, State) -> void() -%% @end -%%-------------------------------------------------------------------- -terminate(_Reason, _State) -> - 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 -%%%=================================================================== - --endif. % TEST diff --git a/test/riak_sysmon_testhandler.erl b/test/riak_sysmon_testhandler.erl new file mode 100644 index 0000000..74f77ba --- /dev/null +++ b/test/riak_sysmon_testhandler.erl @@ -0,0 +1,158 @@ +%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved. +%% +%% This file is provided to you under the Apache License, +%% Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, +%% software distributed under the License is distributed on an +%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +%% KIND, either express or implied. See the License for the +%% specific language governing permissions and limitations +%% under the License. + +%% @doc Support for EUnit testing: simple event-collecting event handler. + +-module(riak_sysmon_testhandler). + +-ifdef(TEST). + +-behaviour(gen_event). + +%% API +-export([start_link/0, add_handler/1, get_events/1]). + +%% gen_event callbacks +-export([init/1, handle_event/2, handle_call/2, + handle_info/2, terminate/2, code_change/3]). + +-record(state, { + list = [] :: list() + }). + +%%%=================================================================== +%%% gen_event callbacks +%%%=================================================================== + +%%-------------------------------------------------------------------- +%% @doc +%% Creates an event manager +%% +%% @spec start_link() -> {ok, Pid} | {error, Error} +%% @end +%%-------------------------------------------------------------------- +start_link() -> + gen_event:start_link({local, ?MODULE}). + +%%-------------------------------------------------------------------- +%% @doc +%% Adds an event handler +%% +%% @spec add_handler(gen_event:emgr_name()) -> ok | {'EXIT', Reason} | term() +%% @end +%%-------------------------------------------------------------------- +add_handler(EventServer) -> + gen_event:add_sup_handler(EventServer, ?MODULE, []). + +get_events(EventServer) -> + gen_event:call(EventServer, ?MODULE, get_events). + +%%%=================================================================== +%%% gen_event callbacks +%%%=================================================================== + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever a new event handler is added to an event manager, +%% this function is called to initialize the event handler. +%% +%% @spec init(Args) -> {ok, State} +%% @end +%%-------------------------------------------------------------------- +init([]) -> + {ok, #state{}}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever an event manager receives an event sent using +%% gen_event:notify/2 or gen_event:sync_notify/2, this function is +%% called for each installed event handler to handle the event. +%% +%% @spec handle_event(Event, State) -> +%% {ok, State} | +%% {swap_handler, Args1, State1, Mod2, Args2} | +%% remove_handler +%% @end +%%-------------------------------------------------------------------- +handle_event(Event, #state{list = List} = State) -> + {ok, State#state{list = [Event|List]}}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever an event manager receives a request sent using +%% gen_event:call/3,4, this function is called for the specified +%% event handler to handle the request. +%% +%% @spec handle_call(Request, State) -> +%% {ok, Reply, State} | +%% {swap_handler, Reply, Args1, State1, Mod2, Args2} | +%% {remove_handler, Reply} +%% @end +%%-------------------------------------------------------------------- +handle_call(get_events, State) -> + {ok, lists:reverse(State#state.list), State#state{list = []}}; +handle_call(_Request, State) -> + Reply = not_supported, + {ok, Reply, State}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% This function is called for each installed event handler when +%% an event manager receives any other message than an event or a +%% synchronous request (or a system message). +%% +%% @spec handle_info(Info, State) -> +%% {ok, State} | +%% {swap_handler, Args1, State1, Mod2, Args2} | +%% remove_handler +%% @end +%%-------------------------------------------------------------------- +handle_info(_Info, State) -> + {ok, State}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Whenever an event handler is deleted from an event manager, this +%% function is called. It should be the opposite of Module:init/1 and +%% do any necessary cleaning up. +%% +%% @spec terminate(Reason, State) -> void() +%% @end +%%-------------------------------------------------------------------- +terminate(_Reason, _State) -> + 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 +%%%=================================================================== + +-endif. % TEST -- 1.8.1.4