forked from basho/riak_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriak_core_ring_events.erl
108 lines (84 loc) · 3.09 KB
/
riak_core_ring_events.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
%% -------------------------------------------------------------------
%%
%% riak_core: Core Riak Application
%%
%% Copyright (c) 2007-2010 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.
%%
%% -------------------------------------------------------------------
-module(riak_core_ring_events).
-behaviour(gen_event).
%% API
-export([start_link/0,
add_handler/2,
add_sup_handler/2,
add_guarded_handler/2,
add_callback/1,
add_sup_callback/1,
add_guarded_callback/1,
ring_update/1,
force_update/0,
ring_sync_update/1,
force_sync_update/0]).
%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2,
handle_info/2, terminate/2, code_change/3]).
-record(state, { callback }).
%% ===================================================================
%% API functions
%% ===================================================================
start_link() ->
gen_event:start_link({local, ?MODULE}).
add_handler(Handler, Args) ->
gen_event:add_handler(?MODULE, Handler, Args).
add_sup_handler(Handler, Args) ->
gen_event:add_sup_handler(?MODULE, Handler, Args).
add_guarded_handler(Handler, Args) ->
riak_core:add_guarded_event_handler(?MODULE, Handler, Args).
add_callback(Fn) when is_function(Fn) ->
gen_event:add_handler(?MODULE, {?MODULE, make_ref()}, [Fn]).
add_sup_callback(Fn) when is_function(Fn) ->
gen_event:add_sup_handler(?MODULE, {?MODULE, make_ref()}, [Fn]).
add_guarded_callback(Fn) when is_function(Fn) ->
riak_core:add_guarded_event_handler(?MODULE, {?MODULE, make_ref()}, [Fn]).
force_update() ->
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
ring_update(Ring).
ring_update(Ring) ->
gen_event:notify(?MODULE, {ring_update, Ring}).
force_sync_update() ->
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
ring_sync_update(Ring).
ring_sync_update(Ring) ->
gen_event:sync_notify(?MODULE, {ring_update, Ring}).
%% ===================================================================
%% gen_event callbacks
%% ===================================================================
init([Fn]) ->
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
Fn(Ring),
{ok, #state { callback = Fn }}.
handle_event({ring_update, Ring}, State) ->
(State#state.callback)(Ring),
{ok, State}.
handle_call(_Request, State) ->
{ok, ok, State}.
handle_info(_Info, State) ->
{ok, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.