fix(rule_engine): remove resource with clean alarms

This commit is contained in:
DDDHuang 2022-05-18 10:37:54 +08:00
parent a5716318b6
commit f3bef3c81c
2 changed files with 30 additions and 4 deletions

View File

@ -48,7 +48,7 @@
-export([ init_resource/4
, init_action/4
, clear_resource/3
, clear_resource/4
, clear_rule/1
, clear_actions/1
, clear_action/3
@ -60,6 +60,10 @@
-export([ fetch_resource_status/3
]).
-ifdef(TEST).
-export([alarm_name_of_resource_down/2]).
-endif.
-type(rule() :: #rule{}).
-type(action() :: #action{}).
-type(resource() :: #resource{}).
@ -450,7 +454,7 @@ delete_resource(ResId) ->
try
case emqx_rule_registry:remove_resource(ResId) of
ok ->
_ = ?CLUSTER_CALL(clear_resource, [ModD, Destroy, ResId]),
_ = ?CLUSTER_CALL(clear_resource, [ModD, Destroy, ResId, ResType]),
ok;
{error, _} = R -> R
end
@ -652,9 +656,13 @@ init_action(Module, OnCreate, ActionInstId, Params) ->
#action_instance_params{id = ActionInstId, params = Params, apply = Apply})
end.
clear_resource(_Module, undefined, ResId) ->
clear_resource(_Module, undefined, Type, ResId) ->
Name = alarm_name_of_resource_down(Type, ResId),
_ = emqx_alarm:deactivate(Name),
ok = emqx_rule_registry:remove_resource_params(ResId);
clear_resource(Module, Destroy, ResId) ->
clear_resource(Module, Destroy, Type, ResId) ->
Name = alarm_name_of_resource_down(Type, ResId),
_ = emqx_alarm:deactivate(Name),
case emqx_rule_registry:find_resource_params(ResId) of
{ok, #resource_params{params = Params}} ->
?RAISE(Module:Destroy(ResId, Params),

View File

@ -327,6 +327,24 @@ t_create_resource(_Config) ->
emqx_rule_registry:remove_resource(ResId),
ok.
t_clean_resource_alarms(_Config) ->
ok = emqx_rule_engine:load_providers(),
{ok, #resource{id = ResId}} = emqx_rule_engine:create_resource(
#{type => built_in,
config => #{},
description => <<"debug resource">>}),
?assert(true, is_binary(ResId)),
Name = emqx_rule_engine:alarm_name_of_resource_down(built_in, ResId),
_ = emqx_alarm:activate(Name, #{id => ResId, type => built_in}),
AlarmExist = fun(#{name := AName}) -> AName == Name end,
Len = length(lists:filter(AlarmExist, emqx_alarm:get_alarms())),
?assert(Len == 1),
ok = emqx_rule_engine:unload_providers(),
emqx_rule_registry:remove_resource(ResId),
LenAfterRemove = length(lists:filter(AlarmExist, emqx_alarm:get_alarms())),
?assert(LenAfterRemove == 0),
ok.
%%------------------------------------------------------------------------------
%% Test cases for rule actions
%%------------------------------------------------------------------------------