-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnkserver_callbacks.erl
255 lines (177 loc) · 7.71 KB
/
nkserver_callbacks.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2019 Carlos Gonzalez Florido. 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 Default plugin callbacks
-module(nkserver_callbacks).
-author('Carlos Gonzalez <[email protected]>').
-export([msg/1, msg/2, i18n/3]).
-export([srv_init/2, srv_handle_call/4, srv_handle_cast/3,
srv_handle_info/3, srv_code_change/4, srv_terminate/3,
srv_timed_check/2]).
-export([srv_master_init/2, srv_master_handle_call/4, srv_master_handle_cast/3,
srv_master_handle_info/3, srv_master_code_change/4, srv_master_terminate/3,
srv_master_timed_check/3, srv_master_become_leader/2]).
-export_type([continue/0]).
-include("nkserver.hrl").
%% ===================================================================
%% Types
%% ===================================================================
-type continue() :: continue | {continue, list()}.
%-type req() :: #nkreq{}.
-type id() :: nkserver:id().
-type user_state() :: map().
-type service() :: nkserver:service().
%% ===================================================================
%% Callbacks
%% ===================================================================
%% Called when the service starts, to update the start options.
-callback config(map()) -> map().
-optional_callbacks([config/1]).
%% ===================================================================
%% Errors Callbacks
%% ===================================================================
%% @doc
-spec msg(nkserver:lang(), nkserver:msg()) ->
atom() |
tuple() |
list() |
{atom(), string()} |
{Fmt::string(), Vals::string()} |
{atom(), Fmt::string(), Vals::string()}.
msg(SrvId, Msg) ->
?CALL_SRV(SrvId, msg, [Msg]).
%% @doc
-spec msg(nkserver:msg()) ->
atom() |
tuple() |
list() |
{atom(), string()} |
{Fmt::string(), Vals::string()} |
{atom(), Fmt::string(), Vals::string()}.
msg({field_missing, Txt}) -> {"Missing field: '~s'", [Txt]};
msg({field_invalid, Txt}) -> {"Field '~s' is invalid", [Txt]};
msg({field_unknown, Txt}) -> {"Unknown field: '~s'", [Txt]};
msg(file_read_error) -> "File read error";
msg(internal_error) -> "Internal error";
msg({internal_error, Ref}) -> {"Internal error: ~s", [Ref]};
msg(invalid_parameters) -> "Invalid parameters";
msg(leader_is_down) -> "Service leader is down";
msg(normal_termination) -> "Normal termination";
msg(not_found) -> "Not found";
msg(not_implemented) -> "Not implemented";
msg(ok) -> "OK";
msg(process_down) -> "Process failed";
msg(process_not_found) -> "Process not found";
msg(service_not_found) -> "Service not found";
msg({syntax_error, Txt}) -> {"Syntax error: '~s'", [Txt]};
msg({tls_alert, Txt}) -> {"Error TTL: ~s", [Txt]};
msg(timeout) -> "Timeout";
msg(unauthorized) -> "Unauthorized";
msg(_) -> continue.
%% ===================================================================
%% i18n
%% ===================================================================
%% @doc
-spec i18n(id(), nklib_i18n:key(), nklib_i18n:lang()) ->
<<>> | binary().
i18n(SrvId, Key, Lang) ->
nklib_i18n:get(SrvId, Key, Lang).
%% ===================================================================
%% Service Server Callbacks
%% ===================================================================
%% @doc Called when a new service starts, first for the top-level plugin
-spec srv_init(service(), user_state()) ->
{ok, user_state()} | {stop, term()}.
srv_init(_Service, UserState) ->
{ok, UserState}.
%% @doc Called when the service process receives a handle_call/3.
-spec srv_handle_call(term(), {pid(), reference()}, id(), user_state()) ->
{reply, term(), user_state()} | {noreply, user_state()} | continue().
srv_handle_call(_Msg, _From, _Service, _State) ->
continue.
%% @doc Called when the NkApp process receives a handle_cast/3.
-spec srv_handle_cast(term(), service(), user_state()) ->
{noreply, user_state()} | continue().
srv_handle_cast(_Msg, _Service, _State) ->
continue.
%% @doc Called when the NkApp process receives a handle_info/3.
-spec srv_handle_info(term(), service(), user_state()) ->
{noreply, user_state()} | continue().
srv_handle_info({'EXIT', _, normal}, _Service, State) ->
{noreply, State};
srv_handle_info(_Msg, _Service, _State) ->
continue.
-spec srv_code_change(term()|{down, term()}, service(), user_state(), term()) ->
ok | {ok, service()} | {error, term()} | continue().
srv_code_change(OldVsn, _Service, State, Extra) ->
{continue, [OldVsn, State, Extra]}.
%% @doc Called when a service is stopped
-spec srv_terminate(term(), service(), service()) ->
{ok, service()}.
srv_terminate(_Reason, _Service, State) ->
{ok, State}.
%% @doc Called periodically
-spec srv_timed_check(service(), user_state()) ->
{ok, user_state()}.
srv_timed_check(_Service, State) ->
{ok, State}.
%% ===================================================================
%% Service Server MASTER Callbacks
%% ===================================================================
%% @doc Called when a new service starts, first for the top-level plugin
-spec srv_master_init(id(), user_state()) ->
{ok, user_state()} | {stop, term()} | continue().
srv_master_init(_SrvId, UserState) ->
{ok, UserState}.
%% @doc Called when the service process receives a handle_call/3.
-spec srv_master_handle_call(term(), {pid(), reference()}, id(), user_state()) ->
{reply, term(), user_state()} | {noreply, user_state()} | continue().
srv_master_handle_call(_Msg, _From, _SrvId, _State) ->
continue.
%% @doc Called when the NkApp process receives a handle_cast/3.
-spec srv_master_handle_cast(term(), id(), user_state()) ->
{noreply, user_state()} | continue().
srv_master_handle_cast(_Msg, _SrvId, _State) ->
continue.
%% @doc Called when the NkApp process receives a handle_info/3.
-spec srv_master_handle_info(term(), id(), user_state()) ->
{noreply, user_state()} | continue().
srv_master_handle_info({'EXIT', _, normal}, _SrvId, State) ->
{noreply, State};
srv_master_handle_info(_Msg, _SrvId, _State) ->
continue.
-spec srv_master_code_change(term()|{down, term()}, id(), user_state(), term()) ->
ok | {ok, service()} | {error, term()} | continue().
srv_master_code_change(OldVsn, _SrvId, State, Extra) ->
{continue, [OldVsn, State, Extra]}.
%% @doc Called when a service is stopped
-spec srv_master_terminate(term(), service(), service()) ->
{ok, service()}.
srv_master_terminate(_Reason, _SrvId, State) ->
{ok, State}.
%% @doc Called periodically
-spec srv_master_timed_check(IsMaster::boolean(), id(), user_state()) ->
{ok, user_state()}.
srv_master_timed_check(_IsMaster, _SrvId, State) ->
{ok, State}.
%% @doc Called when this node tries to become leader
-spec srv_master_become_leader(id(), user_state()) ->
{yes|no, user_state()}.
srv_master_become_leader(SrvId, State) ->
{nkserver_master:strategy_min_nodes(SrvId), State}.