Fix the issue that configuration was not loaded when plugin started

This commit is contained in:
zhouzb 2020-03-25 10:30:08 +08:00
parent 3b537760d5
commit 9398865741
1 changed files with 27 additions and 16 deletions

View File

@ -73,14 +73,7 @@ load(PluginName) when is_atom(PluginName) ->
?LOG(notice, "Plugin ~s is already started", [PluginName]), ?LOG(notice, "Plugin ~s is already started", [PluginName]),
{error, already_started}; {error, already_started};
{_, false} -> {_, false} ->
try load_plugin(PluginName, true)
Configs = generate_configs(PluginName),
apply_configs(Configs),
load_plugin(PluginName, true)
catch _ : Error : Stacktrace ->
?LOG(alert, "Plugin ~s load failed with ~p", [PluginName, {Error, Stacktrace}]),
{error, parse_config_file_failed}
end
end. end.
%% @doc Unload all plugins before broker stopped. %% @doc Unload all plugins before broker stopped.
@ -222,9 +215,20 @@ load_plugins(Names, Persistent) ->
end, NeedToLoad). end, NeedToLoad).
generate_configs(App) -> generate_configs(App) ->
Schema = cuttlefish_schema:files([filename:join([code:priv_dir(App), App]) ++ ".schema"]), ConfigFile = filename:join([emqx:get_env(plugins_etc_dir), App]) ++ ".config",
Conf = cuttlefish_conf:file(filename:join([emqx:get_env(plugins_etc_dir), App]) ++ ".conf"), ConfFile = filename:join([emqx:get_env(plugins_etc_dir), App]) ++ ".conf",
cuttlefish_generator:map(Schema, Conf). SchemaFile = filename:join([code:priv_dir(App), App]) ++ ".schema",
case {filelib:is_file(ConfigFile), filelib:is_file(ConfFile) andalso filelib:is_file(SchemaFile)} of
{true, _} ->
{ok, [Configs]} = file:consult(ConfigFile),
Configs;
{_, true} ->
Schema = cuttlefish_schema:files([SchemaFile]),
Conf = cuttlefish_conf:file(ConfFile),
cuttlefish_generator:map(Schema, Conf);
{false, false} ->
error(no_avaliable_configuration)
end.
apply_configs([]) -> apply_configs([]) ->
ok; ok;
@ -247,11 +251,18 @@ plugin(AppName, Type) ->
end. end.
load_plugin(Name, Persistent) -> load_plugin(Name, Persistent) ->
case load_app(Name) of try
ok -> Configs = generate_configs(Name),
start_app(Name, fun(App) -> plugin_loaded(App, Persistent) end); apply_configs(Configs),
{error, Error} -> case load_app(Name) of
{error, Error} ok ->
start_app(Name, fun(App) -> plugin_loaded(App, Persistent) end);
{error, Error0} ->
{error, Error0}
end
catch _ : Error : Stacktrace ->
?LOG(alert, "Plugin ~s load failed with ~p", [Name, {Error, Stacktrace}]),
{error, parse_config_file_failed}
end. end.
load_app(App) -> load_app(App) ->