-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathleo_watchdog.erl
223 lines (186 loc) · 7.42 KB
/
leo_watchdog.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
%%======================================================================
%%
%% Leo Watchdog
%%
%% Copyright (c) 2012-2017 Rakuten, Inc.
%%
%% 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(leo_watchdog).
-author('Yosuke Hara').
-behaviour(gen_server).
-include("leo_watchdog.hrl").
-include_lib("eunit/include/eunit.hrl").
%% API
-export([start_link/4,
stop/1,
set_interval/2,
update_property/3,
suspend/1,
resume/1,
state/1
]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {
id :: atom(),
callback_mod :: module(),
properties = [] :: [{atom(), any()}],
interval = ?DEF_WATCH_INTERVAL :: pos_integer(),
is_suspending = false :: boolean()
}).
%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
%% @doc Start the server
-spec(start_link(Id, CallbackMod, Props, IntervalTime) ->
{ok,Pid} | ignore | {error,Error} when Id::atom(),
CallbackMod::module(),
Props::tuple(),
IntervalTime::pos_integer(),
Pid::pid(),
Error::{already_started,Pid} | term()).
start_link(Id, CallbackMod, Props, IntervalTime) ->
%% Takes conflict execution measures
timer:sleep(erlang:phash2(leo_date:clock(), timer:seconds(1))),
gen_server:start_link({local, Id}, ?MODULE,
[Id, CallbackMod, Props, IntervalTime], []).
%% @doc Stop the server
-spec(stop(Id) ->
ok when Id::atom()).
stop(Id) ->
gen_server:call(Id, stop).
%% @doc Suspend the server
-spec(set_interval(Id, Interval) ->
ok when Id::atom(),
Interval::pos_integer()).
set_interval(Id, Interval) ->
gen_server:call(Id, {set_interval, Interval}).
%% @doc Suspend the server
-spec(update_property(Id, Item, Value) ->
ok when Id::atom(),
Item::atom(),
Value::any()).
update_property(Id, Item, Value) ->
gen_server:call(Id, {update_property, Item, Value}).
%% @doc Suspend the server
-spec(suspend(Id) ->
ok when Id::atom()).
suspend(Id) ->
gen_server:call(Id, suspend).
%% @doc Resume the server
-spec(resume(Id) ->
ok when Id::atom()).
resume(Id) ->
gen_server:call(Id, resume).
%% @doc Retrieve the state
-spec(state(Id) ->
{ok, State} when Id::atom(),
State::[{atom(), any()}]).
state(Id) ->
gen_server:call(Id, state).
%%--------------------------------------------------------------------
%% GEN_SERVER CALLBACKS
%%--------------------------------------------------------------------
%% @doc Initiates the server
init([Id, CallbackMod, Props, Interval]) ->
Id = ets:new(Id, [named_table, set,
public, {read_concurrency, true}]),
ok = CallbackMod:init(Props),
erlang:send_after(Interval, self(), timeout),
{ok, #state{id = Id,
callback_mod = CallbackMod,
properties = Props,
interval = Interval}}.
%% @doc gen_server callback - Module:handle_call(Request, From, State) -> Result
handle_call(stop, _From, State) ->
{stop, normal, stopped, State};
%% @doc Modify the interval
handle_call({set_interval, Interval},_From, State) ->
erlang:send_after(Interval, self(), timeout),
{reply, ok, State#state{interval = Interval}};
%% @doc Update a property
handle_call({update_property, Item, Value},_From, #state{callback_mod = CallbackMod,
properties = Props} = State) ->
Props_1 = erlang:apply(CallbackMod, update_property, [Item, Value, Props]),
{reply, ok, State#state{properties = Props_1}};
%% @doc Suspend the server
handle_call(suspend,_From, State) ->
{reply, ok, State#state{is_suspending = true}};
%% @doc Resume the server
handle_call(resume,_From, #state{is_suspending = false} = State) ->
{reply, ok, State};
handle_call(resume,_From, State) ->
{reply, ok, State#state{is_suspending = false}};
%% @doc Retrieve the state
handle_call(state,_From, State) ->
State_1 = lists:zip(record_info(fields, state),tl(tuple_to_list(State))),
{reply, {ok, State_1}, State}.
%% @doc Handling cast message
%% <p>
%% gen_server callback - Module:handle_cast(Request, State) -> Result.
%% </p>
handle_cast(_Msg, State) ->
{noreply, State}.
%% @doc Handling all non call/cast messages
%% <p>
%% gen_server callback - Module:handle_info(Info, State) -> Result.
%% </p>
handle_info(timeout, #state{interval = Interval,
is_suspending = true} = State) ->
erlang:send_after(Interval, self(), timeout),
{noreply, State};
handle_info(timeout, #state{id = Id,
callback_mod = CallbackMod,
properties = Props,
interval = Interval} = State) ->
Props_1 = case catch erlang:apply(
CallbackMod, handle_call, [Id, Props]) of
{'EXIT', Cause} ->
error_logger:error_msg(
"~p,~p,~p,~p~n",
[{module, ?MODULE_STRING},
{function, "handle_info/2"},
{line, ?LINE}, {body, {CallbackMod, Cause}}]),
case catch erlang:apply(
CallbackMod, handle_fail, [Id, Cause]) of
{_, Reason} ->
error_logger:info_msg(
"~p,~p,~p,~p~n",
[{module, ?MODULE_STRING},
{function, "handle_info/2"},
{line, ?LINE}, {body, {CallbackMod, Reason}}]),
Props;
_ ->
Props
end;
{_Ret, NewProps} ->
catch ets:insert(Id, NewProps),
NewProps
end,
erlang:send_after(Interval, self(), timeout),
{noreply, State#state{properties = Props_1}};
handle_info(_, State) ->
{noreply, State}.
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
terminate(_Reason, _State) ->
ok.
%% @doc Convert process state when code is changed
code_change(_OldVsn, State, _Extra) ->
{ok, State}.