Просмотр исходного кода

fix(rule_engine): fix resource update maybe failure

wwhai 5 лет назад
Родитель
Сommit
903ce29ce0
1 измененных файлов с 18 добавлено и 12 удалено
  1. 18 12
      apps/emqx_rule_engine/src/emqx_rule_engine.erl

+ 18 - 12
apps/emqx_rule_engine/src/emqx_rule_engine.erl

@@ -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) ->