Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add priority plugin chain order with plugin reload #62

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/vmq_plugin/priv/vmq_plugin.schema
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@
end
end}.

{translation, "vmq_plugin.priority",
dhruvjain99 marked this conversation as resolved.
Show resolved Hide resolved
fun(Conf) ->
PluginNames = proplists:get_all_values("$name",cuttlefish_variable:fuzzy_matches(["plugins", "$name"], Conf)),
EnabledPlugins = lists:filter(fun(Name) -> cuttlefish:conf_get("plugins." ++ Name, Conf, false) end, PluginNames),
lists:foldl(fun(PluginName, Acc) ->
Priority = cuttlefish:conf_get("plugins." ++ PluginName ++ ".priority", Conf, infinity),
maps:put(list_to_atom(PluginName), Priority, Acc) end,
#{},
EnabledPlugins
)
end}.
121 changes: 120 additions & 1 deletion apps/vmq_plugin/src/vmq_plugin_mgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ wait_until_ready(#state{ready = true} = State) ->
{ok, handle_deferred_calls(State)}.

check_updated_plugins(Plugins, State) ->
case check_plugins(Plugins, []) of
OrderedPlugins = set_plugin_priority_order(Plugins),
case check_plugins(OrderedPlugins, []) of
{ok, CheckedPlugins} ->
ok = init_plugins_cli(CheckedPlugins),
ok = start_plugins(CheckedPlugins),
Expand Down Expand Up @@ -514,6 +515,28 @@ check_plugins([{application, App, Options} | Rest], Acc) ->
check_plugins([], CheckedHooks) ->
{ok, lists:reverse(CheckedHooks)}.

set_plugin_priority_order(Plugins) ->
PriorityMap = application:get_env(vmq_plugin, priority, #{}),
lists:sort(
fun({_, PluginA, _}, {_, PluginB, _}) ->
PriorityA = maps:get(PluginA, PriorityMap, undefined),
PriorityB = maps:get(PluginB, PriorityMap, undefined),
case {PriorityA, PriorityB} of
% No priority defined for either, keep original order for internal plugins
{undefined, undefined} -> true;
% No priority defined for either, keep original order from vernemq.conf
{infinity, infinity} -> true;
% Keep PluginB before PluginA if PluginB has no priority
dhruvjain99 marked this conversation as resolved.
Show resolved Hide resolved
{_, undefined} -> false;
% Keep PluginA before PluginB if PluginA has no priority
{undefined, _} -> true;
% Compare priorities
{PriorityA, PriorityB} -> PriorityA < PriorityB
end
end,
Plugins
).

check_module_plugin(Module, Options) ->
Hooks = proplists:get_value(hooks, Options, undefined),
check_module_hooks(Module, Hooks).
Expand Down Expand Up @@ -1454,4 +1477,100 @@ call_hooks() ->
%% ALL_TILL_OK Hook Tests
?assertEqual(ok, vmq_plugin:all_till_ok(sample_all_till_ok_ok_hook, [10])),
?assertEqual({error, error}, vmq_plugin:all_till_ok(sample_all_till_ok_error_hook, [10])).

plugin_priority_order_test() ->
application:set_env(vmq_plugin, priority, #{
vmq_plugin_one => 1,
vmq_plugin_two => 2,
vmq_plugin_three => 3,
vmq_plugin_four => infinity,
vmq_plugin_five => infinity
}),
% set plugin in pritority order
?assertEqual(
[
{application, vmq_plugin_one, []},
{application, vmq_plugin_two, []},
{application, vmq_plugin_three, []}
],
set_plugin_priority_order([
{application, vmq_plugin_three, []},
{application, vmq_plugin_one, []},
{application, vmq_plugin_two, []}
])
),
% set plugin order with enabled plugin less than priority map
?assertEqual(
[
{application, vmq_plugin_one, []},
{application, vmq_plugin_three, []}
],
set_plugin_priority_order([
{application, vmq_plugin_three, []},
{application, vmq_plugin_one, []}
])
),
% order unchanged without plugins with priority
?assertEqual(
[
{module, vmq_app_one, []},
{module, vmq_app_two, []},
{module, vmq_app_three, []},
{module, vmq_hook_one, []},
{module, vmq_hook_two, []}
],
set_plugin_priority_order([
{module, vmq_app_one, []},
{module, vmq_app_two, []},
{module, vmq_app_three, []},
{module, vmq_hook_one, []},
{module, vmq_hook_two, []}
])
),
% set order with plugins with and without priority
?assertEqual(
[
{module, vmq_app_one, []},
{module, vmq_app_two, []},
{application, vmq_app_three, []},
{application, vmq_hook_one, []},
{module, vmq_hook_two, []},
{application, vmq_plugin_one, []},
{application, vmq_plugin_two, []},
{application, vmq_plugin_three, []},
{application, vmq_plugin_four, []}
],
set_plugin_priority_order([
{module, vmq_app_one, []},
{application, vmq_plugin_three, []},
{module, vmq_app_two, []},
{application, vmq_plugin_one, []},
{application, vmq_app_three, []},
{application, vmq_plugin_four, []},
{application, vmq_hook_one, []},
{application, vmq_plugin_two, []},
{module, vmq_hook_two, []}
])
),
% no priority set in vernemq.conf maintain the original order for external plugins.
?assertEqual(
[
{module, vmq_app_one, []},
{module, vmq_app_two, []},
{application, vmq_hook_one, []},
{application, vmq_plugin_two, []},
{application, vmq_plugin_four, []},
{application, vmq_plugin_five, []}
],
set_plugin_priority_order([
{module, vmq_app_one, []},
{module, vmq_app_two, []},
{application, vmq_plugin_four, []},
{application, vmq_hook_one, []},
{application, vmq_plugin_two, []},
{application, vmq_plugin_five, []}
])
),
% empty list
?assertEqual([], set_plugin_priority_order([])).
-endif.
Loading