fix(rule_engine): fix resource update maybe failure

This commit is contained in:
wwhai 2020-12-31 11:20:23 +08:00 committed by Shawn
parent 72148c9e20
commit 903ce29ce0
1 changed files with 18 additions and 12 deletions

View File

@ -245,6 +245,24 @@ create_resource(#{type := Type, config := Config0} = Params) ->
{error, {resource_type_not_found, Type}}
end.
update_resource(#{id := Id, type := Type, config := NewConfig, description := Description}
= NewResource) ->
case emqx_rule_registry:find_resource_type(Type) of
{ok, #resource_type{on_create = {Module, Create},
params_spec = ParamSpec}} ->
Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec),
delete_resource(Id),
emqx_rule_registry:add_resource(#resource{id = Id,
config = Config,
type = Type,
description = Description,
created_at = erlang:system_time(millisecond)}),
catch cluster_call(init_resource, [Module, Create, Id, Config]),
{ok, NewResource};
not_found ->
{error, {resource_type_not_found, Type}}
end.
-spec(start_resource(resource_id()) -> ok | {error, Reason :: term()}).
start_resource(ResId) ->
case emqx_rule_registry:find_resource(ResId) of
@ -310,18 +328,6 @@ delete_resource(ResId) ->
{error, {resource_not_found, ResId}}
end.
update_resource(#{id := Id,
config := Config,
type := Type,
description := Description,
created_at := CreatedAt} = NewResource) ->
ok = refresh_resource(#resource{id = Id,
config = Config,
type = Type,
description = Description,
created_at = CreatedAt}),
create_resource(NewResource).
%% @doc Ensure resource deleted. `resource_not_found` error is discarded.
-spec(ensure_resource_deleted(resource_id()) -> ok).
ensure_resource_deleted(ResId) ->