fix(rule_engine): delete resource crashes when dependent rule exists
This commit is contained in:
parent
3646e76a7a
commit
e3501acc02
|
@ -356,9 +356,12 @@ delete_resource(ResId) ->
|
||||||
{ok, #resource_type{on_destroy = {ModD, Destroy}}}
|
{ok, #resource_type{on_destroy = {ModD, Destroy}}}
|
||||||
= emqx_rule_registry:find_resource_type(ResType),
|
= emqx_rule_registry:find_resource_type(ResType),
|
||||||
try
|
try
|
||||||
ok = emqx_rule_registry:remove_resource(ResId),
|
case emqx_rule_registry:remove_resource(ResId) of
|
||||||
_ = ?CLUSTER_CALL(clear_resource, [ModD, Destroy, ResId]),
|
ok ->
|
||||||
ok
|
_ = ?CLUSTER_CALL(clear_resource, [ModD, Destroy, ResId]),
|
||||||
|
ok;
|
||||||
|
{error, _} = R -> R
|
||||||
|
end
|
||||||
catch
|
catch
|
||||||
throw:Reason -> {error, Reason}
|
throw:Reason -> {error, Reason}
|
||||||
end;
|
end;
|
||||||
|
|
|
@ -350,7 +350,7 @@ update_resource(#{id := Id}, NewParams) ->
|
||||||
return({error, 400, ?ERR_DEP_RULES_EXISTS(RuleIds)});
|
return({error, 400, ?ERR_DEP_RULES_EXISTS(RuleIds)});
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?LOG(error, "Resource update failed: ~0p", [Reason]),
|
?LOG(error, "Resource update failed: ~0p", [Reason]),
|
||||||
return({error, 500, <<"Resource update failed!">>})
|
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
delete_resource(#{id := Id}, _Params) ->
|
delete_resource(#{id := Id}, _Params) ->
|
||||||
|
|
|
@ -120,6 +120,6 @@ retry_loop(resource, ResId, Interval) ->
|
||||||
enable_rules_of_resource(ResId) ->
|
enable_rules_of_resource(ResId) ->
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun (#rule{enabled = false} = Rule) ->
|
fun (#rule{enabled = false} = Rule) ->
|
||||||
emqx_rule_registry:add_rule(Rule#rule{enabled = false});
|
emqx_rule_registry:add_rule(Rule#rule{enabled = true});
|
||||||
(_) -> ok
|
(_) -> ok
|
||||||
end, emqx_rule_registry:find_rules_depends_on_resource(ResId)).
|
end, emqx_rule_registry:find_rules_depends_on_resource(ResId)).
|
||||||
|
|
|
@ -356,7 +356,7 @@ find_resource_params(Id) ->
|
||||||
[] -> not_found
|
[] -> not_found
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec(remove_resource(emqx_rule_engine:resource() | emqx_rule_engine:resource_id()) -> ok).
|
-spec(remove_resource(emqx_rule_engine:resource() | emqx_rule_engine:resource_id()) -> ok | {error, term()}).
|
||||||
remove_resource(Resource) when is_record(Resource, resource) ->
|
remove_resource(Resource) when is_record(Resource, resource) ->
|
||||||
trans(fun delete_resource/1, [Resource#resource.id]);
|
trans(fun delete_resource/1, [Resource#resource.id]);
|
||||||
|
|
||||||
|
@ -371,11 +371,10 @@ remove_resource_params(ResId) ->
|
||||||
%% @private
|
%% @private
|
||||||
delete_resource(ResId) ->
|
delete_resource(ResId) ->
|
||||||
case find_enabled_rules_depends_on_resource(ResId) of
|
case find_enabled_rules_depends_on_resource(ResId) of
|
||||||
[] -> ok;
|
[] -> mnesia:delete(?RES_TAB, ResId, write);
|
||||||
Rules ->
|
Rules ->
|
||||||
throw({dependent_rules_exists, [Id || #rule{id = Id} <- Rules]})
|
{error, {dependent_rules_exists, [Id || #rule{id = Id} <- Rules]}}
|
||||||
end,
|
end.
|
||||||
mnesia:delete(?RES_TAB, ResId, write).
|
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
insert_resource(Resource) ->
|
insert_resource(Resource) ->
|
||||||
|
@ -490,6 +489,6 @@ get_all_records(Tab) ->
|
||||||
trans(Fun) -> trans(Fun, []).
|
trans(Fun) -> trans(Fun, []).
|
||||||
trans(Fun, Args) ->
|
trans(Fun, Args) ->
|
||||||
case mnesia:transaction(Fun, Args) of
|
case mnesia:transaction(Fun, Args) of
|
||||||
{atomic, ok} -> ok;
|
{atomic, Result} -> Result;
|
||||||
{aborted, Reason} -> error(Reason)
|
{aborted, Reason} -> error(Reason)
|
||||||
end.
|
end.
|
||||||
|
|
Loading…
Reference in New Issue