fix(config): rename callback functions for emqx_config_handler

This commit is contained in:
Shawn 2021-07-23 18:56:30 +08:00
parent bbf40a85bf
commit 935ef0db20
4 changed files with 33 additions and 33 deletions

View File

@ -30,7 +30,7 @@
-boot_mnesia({mnesia, [boot]}).
-copy_mnesia({mnesia, [copy]}).
-export([handle_update_config/2]).
-export([pre_config_update/2]).
-export([ start_link/0
, stop/0
@ -153,10 +153,10 @@ get_alarms(activated) ->
get_alarms(deactivated) ->
gen_server:call(?MODULE, {get_alarms, deactivated}).
handle_update_config(#{<<"validity_period">> := Period0} = NewConf, OldConf) ->
pre_config_update(#{<<"validity_period">> := Period0} = NewConf, OldConf) ->
?MODULE ! {update_timer, hocon_postprocess:duration(Period0)},
maps:merge(OldConf, NewConf);
handle_update_config(NewConf, OldConf) ->
pre_config_update(NewConf, OldConf) ->
maps:merge(OldConf, NewConf).
format(#activated_alarm{name = Name, message = Message, activate_at = At, details = Details}) ->

View File

@ -29,7 +29,7 @@
]).
%% emqx_config_handler callbacks
-export([ handle_update_config/2
-export([ pre_config_update/2
]).
%% gen_server callbacks
@ -45,14 +45,14 @@
-type handler_name() :: module().
-type handlers() :: #{emqx_config:config_key() => handlers(), ?MOD => handler_name()}.
-optional_callbacks([ handle_update_config/2
, post_update_config/2
-optional_callbacks([ pre_config_update/2
, post_config_update/2
]).
-callback handle_update_config(emqx_config:update_request(), emqx_config:raw_config()) ->
-callback pre_config_update(emqx_config:update_request(), emqx_config:raw_config()) ->
emqx_config:update_request().
-callback post_update_config(emqx_config:config(), emqx_config:config()) -> any().
-callback post_config_update(emqx_config:config(), emqx_config:config()) -> any().
-type state() :: #{
handlers := handlers(),
@ -88,7 +88,7 @@ handle_call({update_config, ConfKeyPath, UpdateReq, RawConf}, _From,
OldConf = emqx_config:get(),
try {RootKeys, Conf} = do_update_config(ConfKeyPath, Handlers, RawConf, UpdateReq),
Result = emqx_config:save_configs(Conf, #{overridden_keys => RootKeys}),
do_post_update_config(ConfKeyPath, Handlers, OldConf, emqx_config:get()),
do_post_config_update(ConfKeyPath, Handlers, OldConf, emqx_config:get()),
{reply, Result, State}
catch
Error : Reason : ST ->
@ -113,43 +113,43 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
do_update_config([], Handlers, OldRawConf, UpdateReq) ->
call_handle_update_config(Handlers, OldRawConf, UpdateReq);
call_pre_config_update(Handlers, OldRawConf, UpdateReq);
do_update_config([ConfKey | ConfKeyPath], Handlers, OldRawConf, UpdateReq) ->
SubOldRawConf = get_sub_config(bin(ConfKey), OldRawConf),
SubHandlers = maps:get(ConfKey, Handlers, #{}),
NewUpdateReq = do_update_config(ConfKeyPath, SubHandlers, SubOldRawConf, UpdateReq),
call_handle_update_config(Handlers, OldRawConf, #{bin(ConfKey) => NewUpdateReq}).
call_pre_config_update(Handlers, OldRawConf, #{bin(ConfKey) => NewUpdateReq}).
do_post_update_config([], Handlers, OldConf, NewConf) ->
call_post_update_config(Handlers, OldConf, NewConf);
do_post_update_config([ConfKey | ConfKeyPath], Handlers, OldConf, NewConf) ->
do_post_config_update([], Handlers, OldConf, NewConf) ->
call_post_config_update(Handlers, OldConf, NewConf);
do_post_config_update([ConfKey | ConfKeyPath], Handlers, OldConf, NewConf) ->
SubOldConf = get_sub_config(ConfKey, OldConf),
SubNewConf = get_sub_config(ConfKey, NewConf),
SubHandlers = maps:get(ConfKey, Handlers, #{}),
_ = do_post_update_config(ConfKeyPath, SubHandlers, SubOldConf, SubNewConf),
call_post_update_config(Handlers, OldConf, NewConf).
_ = do_post_config_update(ConfKeyPath, SubHandlers, SubOldConf, SubNewConf),
call_post_config_update(Handlers, OldConf, NewConf).
get_sub_config(ConfKey, Conf) when is_map(Conf) ->
maps:get(ConfKey, Conf, undefined);
get_sub_config(_, _Conf) -> %% the Conf is a primitive
undefined.
call_handle_update_config(Handlers, OldRawConf, UpdateReq) ->
call_pre_config_update(Handlers, OldRawConf, UpdateReq) ->
HandlerName = maps:get(?MOD, Handlers, undefined),
case erlang:function_exported(HandlerName, handle_update_config, 2) of
true -> HandlerName:handle_update_config(UpdateReq, OldRawConf);
case erlang:function_exported(HandlerName, pre_config_update, 2) of
true -> HandlerName:pre_config_update(UpdateReq, OldRawConf);
false -> merge_to_old_config(UpdateReq, OldRawConf)
end.
call_post_update_config(Handlers, OldConf, NewConf) ->
call_post_config_update(Handlers, OldConf, NewConf) ->
HandlerName = maps:get(?MOD, Handlers, undefined),
case erlang:function_exported(HandlerName, post_update_config, 2) of
true -> _ = HandlerName:post_update_config(NewConf, OldConf);
case erlang:function_exported(HandlerName, post_config_update, 2) of
true -> _ = HandlerName:post_config_update(NewConf, OldConf);
false -> ok
end.
%% callbacks for the top-level handler
handle_update_config(UpdateReq, OldConf) ->
pre_config_update(UpdateReq, OldConf) ->
FullRawConf = merge_to_old_config(UpdateReq, OldConf),
{maps:keys(UpdateReq), FullRawConf}.

View File

@ -31,7 +31,7 @@
, match/4
]).
-export([post_update_config/2, handle_update_config/2]).
-export([post_config_update/2, pre_config_update/2]).
-define(CONF_KEY_PATH, [emqx_authz, rules]).
@ -52,21 +52,21 @@ update(Cmd, Rules) ->
emqx_config:update(?CONF_KEY_PATH, {Cmd, Rules}).
%% For now we only support re-creating the entire rule list
handle_update_config({head, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
pre_config_update({head, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
[Rule | OldConf];
handle_update_config({tail, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
pre_config_update({tail, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
OldConf ++ [Rule];
handle_update_config({_, NewConf}, _OldConf) ->
pre_config_update({_, NewConf}, _OldConf) ->
%% overwrite the entire config!
case is_list(NewConf) of
true -> NewConf;
false -> [NewConf]
end.
post_update_config(undefined, _OldConf) ->
post_config_update(undefined, _OldConf) ->
%_ = [release_rules(Rule) || Rule <- OldConf],
ok;
post_update_config(NewRules, _OldConf) ->
post_config_update(NewRules, _OldConf) ->
%_ = [release_rules(Rule) || Rule <- OldConf],
InitedRules = [init_rule(Rule) || Rule <- NewRules],
Action = find_action_in_hooks(),

View File

@ -19,7 +19,7 @@
-behaviour(emqx_config_handler).
-export([start/2, stop/1, handle_update_config/2]).
-export([start/2, stop/1, pre_config_update/2]).
start(_StartType, _StartArgs) ->
{ok, Sup} = emqx_data_bridge_sup:start_link(),
@ -31,11 +31,11 @@ stop(_State) ->
ok.
%% internal functions
handle_update_config({update, Bridge = #{<<"name">> := Name}}, OldConf) ->
pre_config_update({update, Bridge = #{<<"name">> := Name}}, OldConf) ->
[Bridge | remove_bridge(Name, OldConf)];
handle_update_config({delete, Name}, OldConf) ->
pre_config_update({delete, Name}, OldConf) ->
remove_bridge(Name, OldConf);
handle_update_config(NewConf, _OldConf) when is_list(NewConf) ->
pre_config_update(NewConf, _OldConf) when is_list(NewConf) ->
%% overwrite the entire config!
NewConf.