From 935ef0db20c0ee34e06f1e5300e7684066898659 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 23 Jul 2021 18:56:30 +0800 Subject: [PATCH] fix(config): rename callback functions for emqx_config_handler --- apps/emqx/src/emqx_alarm.erl | 6 +-- apps/emqx/src/emqx_config_handler.erl | 40 +++++++++---------- apps/emqx_authz/src/emqx_authz.erl | 12 +++--- .../src/emqx_data_bridge_app.erl | 8 ++-- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/apps/emqx/src/emqx_alarm.erl b/apps/emqx/src/emqx_alarm.erl index ea96f14ff..086447c8c 100644 --- a/apps/emqx/src/emqx_alarm.erl +++ b/apps/emqx/src/emqx_alarm.erl @@ -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}) -> diff --git a/apps/emqx/src/emqx_config_handler.erl b/apps/emqx/src/emqx_config_handler.erl index 627cb4c98..e051b27eb 100644 --- a/apps/emqx/src/emqx_config_handler.erl +++ b/apps/emqx/src/emqx_config_handler.erl @@ -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}. diff --git a/apps/emqx_authz/src/emqx_authz.erl b/apps/emqx_authz/src/emqx_authz.erl index 00b2a8e93..8a85dc417 100644 --- a/apps/emqx_authz/src/emqx_authz.erl +++ b/apps/emqx_authz/src/emqx_authz.erl @@ -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(), diff --git a/apps/emqx_data_bridge/src/emqx_data_bridge_app.erl b/apps/emqx_data_bridge/src/emqx_data_bridge_app.erl index ccd98f010..26128841b 100644 --- a/apps/emqx_data_bridge/src/emqx_data_bridge_app.erl +++ b/apps/emqx_data_bridge/src/emqx_data_bridge_app.erl @@ -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.