diff --git a/apps/emqx_plugins/src/emqx_plugins.erl b/apps/emqx_plugins/src/emqx_plugins.erl index 44ac3075d..9252ac957 100644 --- a/apps/emqx_plugins/src/emqx_plugins.erl +++ b/apps/emqx_plugins/src/emqx_plugins.erl @@ -139,7 +139,7 @@ ensure_state(NameVsn, Position, State) -> Item = #{ name_vsn => NameVsn , enable => State }, - ensure_configured(Item, Position); + tryit("ensure_state", fun() -> ensure_configured(Item, Position) end); {error, Reason} -> {error, Reason} end. @@ -150,13 +150,18 @@ ensure_configured(#{name_vsn := NameVsn} = Item, Position) -> {Front, Rear} = lists:splitwith(SplitFun, Configured), NewConfigured = case Rear of - [_ | More] -> Front ++ [Item | More]; - [] -> add_new_configured(Configured, Position, Item) + [_ | More] when Position =:= no_move -> + Front ++ [Item | More]; + [_ | More] -> + add_new_configured(Front ++ More, Position, Item); + [] -> + add_new_configured(Configured, Position, Item) end, ok = put_configured(NewConfigured). add_new_configured(Configured, no_move, Item) -> - Configured ++ [Item]; + %% default to rear + add_new_configured(Configured, rear, Item); add_new_configured(Configured, front, Item) -> [Item | Configured]; add_new_configured(Configured, rear, Item) -> @@ -164,6 +169,11 @@ add_new_configured(Configured, rear, Item) -> add_new_configured(Configured, {before, NameVsn}, Item) -> SplitFun = fun(#{name_vsn := Nv}) -> bin(Nv) =/= bin(NameVsn) end, {Front, Rear} = lists:splitwith(SplitFun, Configured), + Rear =:= [] andalso + throw(#{error => "position_anchor_plugin_not_configured", + hint => "maybe_install_and_configure", + name_vsn => NameVsn + }), Front ++ [Item | Rear]. %% @doc Delete the package file. diff --git a/apps/emqx_plugins/test/emqx_plugins_SUITE.erl b/apps/emqx_plugins/test/emqx_plugins_SUITE.erl index df5af8883..b53fe461d 100644 --- a/apps/emqx_plugins/test/emqx_plugins_SUITE.erl +++ b/apps/emqx_plugins/test/emqx_plugins_SUITE.erl @@ -149,7 +149,7 @@ t_start_restart_and_stop(Config) -> assert_app_running(emqx_plugin_template, true), %% fake enable bar-2 - ok = emqx_plugins:ensure_state(Bar2, reat, true), + ok = emqx_plugins:ensure_state(Bar2, rear, true), %% should cause an error ?assertError(#{function := _, errors := [_ | _]}, emqx_plugins:ensure_started()), diff --git a/apps/emqx_plugins/test/emqx_plugins_tests.erl b/apps/emqx_plugins/test/emqx_plugins_tests.erl index 30368d7e9..81f85f316 100644 --- a/apps/emqx_plugins/test/emqx_plugins_tests.erl +++ b/apps/emqx_plugins/test/emqx_plugins_tests.erl @@ -31,7 +31,9 @@ test_ensure_configured() -> emqx_plugins:ensure_configured(P1, front), emqx_plugins:ensure_configured(P2, {before, <<"p-1">>}), emqx_plugins:ensure_configured(P3, {before, <<"p-1">>}), - ?assertEqual([P2, P3, P1], emqx_plugins:configured()). + ?assertEqual([P2, P3, P1], emqx_plugins:configured()), + ?assertThrow(#{error := "position_anchor_plugin_not_configured"}, + emqx_plugins:ensure_configured(P3, {before, <<"unknown-x">>})). read_plugin_test() -> with_rand_install_dir(