fix(config): start the top level config handler at emqx bootup

This commit is contained in:
Shawn 2021-06-15 19:07:50 +08:00 committed by turtleDeng
parent 378a2b7b9e
commit b11afc6997
4 changed files with 11 additions and 24 deletions

View File

@ -23,7 +23,7 @@
-export([ start_link/0 -export([ start_link/0
, start_handler/3 , start_handler/3
, update_config/2 , update_config/2
, child_spec/2 , child_spec/3
]). ]).
%% emqx_config_handler callbacks %% emqx_config_handler callbacks
@ -40,7 +40,7 @@
-type config() :: term(). -type config() :: term().
-type config_map() :: #{atom() => config()} | [config_map()]. -type config_map() :: #{atom() => config()} | [config_map()].
-type handler_name() :: module(). -type handler_name() :: module() | top.
-type key_path() :: [atom()]. -type key_path() :: [atom()].
-optional_callbacks([handle_update_config/2]). -optional_callbacks([handle_update_config/2]).
@ -61,10 +61,10 @@ start_link() ->
start_handler(HandlerName, Parent, ConfKeyPath) -> start_handler(HandlerName, Parent, ConfKeyPath) ->
gen_server:start_link({local, HandlerName}, ?MODULE, {HandlerName, Parent, ConfKeyPath}, []). gen_server:start_link({local, HandlerName}, ?MODULE, {HandlerName, Parent, ConfKeyPath}, []).
-spec child_spec(module(), key_path()) -> supervisor:child_spec(). -spec child_spec(module(), handler_name(), key_path()) -> supervisor:child_spec().
child_spec(Mod, KeyPath) -> child_spec(Mod, Parent, KeyPath) ->
#{id => Mod, #{id => Mod,
start => {?MODULE, start_handler, [Mod, ?MODULE, KeyPath]}, start => {?MODULE, start_handler, [Mod, Parent, KeyPath]},
restart => permanent, restart => permanent,
type => worker, type => worker,
modules => [?MODULE]}. modules => [?MODULE]}.

View File

@ -33,7 +33,9 @@ init([]) ->
child_spec(emqx_stats, worker), child_spec(emqx_stats, worker),
child_spec(emqx_metrics, worker), child_spec(emqx_metrics, worker),
child_spec(emqx_ctl, worker), child_spec(emqx_ctl, worker),
child_spec(emqx_zone, worker)]}}. child_spec(emqx_zone, worker),
child_spec(emqx_config_handler, worker)
]}}.
child_spec(M, Type) -> child_spec(M, Type) ->
child_spec(M, Type, []). child_spec(M, Type, []).

View File

@ -36,7 +36,8 @@ init([]) ->
restart => permanent, restart => permanent,
type => worker, type => worker,
modules => [emqx_data_bridge_monitor]}, modules => [emqx_data_bridge_monitor]},
emqx_config_handler:child_spec(emqx_data_bridge_config_handler, config_key_path()) emqx_config_handler:child_spec(emqx_data_bridge_config_handler, emqx_config_handler,
config_key_path())
], ],
{ok, {SupFlags, ChildSpecs}}. {ok, {SupFlags, ChildSpecs}}.

View File

@ -90,16 +90,6 @@ create_local(InstId, ResourceType, InstConf) ->
Error -> Error Error -> Error
end. end.
save_config_to_disk(InstId, ResourceType, Config) ->
%% TODO: send an event to the config handler, and the hander (single process)
%% will dump configs for all instances (from an ETS table) to a file.
file:write_file(filename:join([emqx_data_dir(), binary_to_list(InstId) ++ ".conf"]),
jsx:encode(#{id => InstId, resource_type => ResourceType,
config => emqx_resource:call_jsonify(ResourceType, Config)})).
emqx_data_dir() ->
"data".
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% gen_server callbacks %% gen_server callbacks
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -180,13 +170,7 @@ do_create(InstId, ResourceType, Config) ->
#{mod => ResourceType, config => Config, #{mod => ResourceType, config => Config,
state => ResourceState, status => stopped}}), state => ResourceState, status => stopped}}),
_ = do_health_check(InstId), _ = do_health_check(InstId),
case save_config_to_disk(InstId, ResourceType, Config) of {ok, force_lookup(InstId)};
ok -> {ok, force_lookup(InstId)};
{error, Reason} ->
logger:error("save config for ~p resource ~p to disk failed: ~p",
[ResourceType, InstId, Reason]),
{error, Reason}
end;
{error, Reason} -> {error, Reason} ->
logger:error("start ~s resource ~s failed: ~p", [ResourceType, InstId, Reason]), logger:error("start ~s resource ~s failed: ~p", [ResourceType, InstId, Reason]),
{error, Reason} {error, Reason}