Merge pull request #9334 from terry-xiaoyu/fix_500_when_resource_unavailable

fix: make init_resource_with_retrier/4 only 'throw' when failed
This commit is contained in:
Xinyu Liu 2022-11-10 14:31:25 +08:00 committed by GitHub
commit 89b6cce0bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 11 deletions

View File

@ -82,12 +82,15 @@
%% redefine this macro to confine the appup scope %% redefine this macro to confine the appup scope
-undef(RAISE). -undef(RAISE).
-define(RAISE(_EXP_, _ERROR_CONTEXT_), -define(RAISE(_EXP_, _ERROR_CONTEXT_),
?RAISE(_EXP_, do_nothing, _ERROR_CONTEXT_)).
-define(RAISE(_EXP_, _EXP_ON_FAIL_, _ERROR_CONTEXT_),
fun() -> fun() ->
try (_EXP_) try (_EXP_)
catch catch
throw : Reason -> throw : Reason ->
throw({_ERROR_CONTEXT_, Reason}); throw({_ERROR_CONTEXT_, Reason});
_EXCLASS_:_EXCPTION_:_ST_ -> _EXCLASS_:_EXCPTION_:_ST_ ->
_EXP_ON_FAIL_,
throw({_ERROR_CONTEXT_, {_EXCLASS_, _EXCPTION_, _ST_}}) throw({_ERROR_CONTEXT_, {_EXCLASS_, _EXCPTION_, _ST_}})
end end
end()). end()).
@ -496,7 +499,12 @@ refresh_resource(Type) when is_atom(Type) ->
refresh_resource(#resource{id = ResId, type = Type, config = Config}) -> refresh_resource(#resource{id = ResId, type = Type, config = Config}) ->
{ok, #resource_type{on_create = {M, F}}} = {ok, #resource_type{on_create = {M, F}}} =
emqx_rule_registry:find_resource_type(Type), emqx_rule_registry:find_resource_type(Type),
ok = emqx_rule_engine:init_resource_with_retrier(M, F, ResId, Config). try
init_resource_with_retrier(M, F, ResId, Config)
catch
throw:Reason ->
?LOG_SENSITIVE(warning, "refresh_resource failed: ~0p", [Reason])
end.
-spec(refresh_rules_when_boot() -> ok). -spec(refresh_rules_when_boot() -> ok).
refresh_rules_when_boot() -> refresh_rules_when_boot() ->
@ -675,16 +683,12 @@ init_resource(Module, OnCreate, ResId, Config) ->
emqx_rule_registry:add_resource_params(ResParams). emqx_rule_registry:add_resource_params(ResParams).
init_resource_with_retrier(Module, OnCreate, ResId, Config) -> init_resource_with_retrier(Module, OnCreate, ResId, Config) ->
try Params = ?RAISE(Module:OnCreate(ResId, Config),
Params = Module:OnCreate(ResId, Config), emqx_rule_monitor:ensure_resource_retrier(ResId), {Module, OnCreate}),
ResParams = #resource_params{id = ResId, ResParams = #resource_params{id = ResId,
params = Params, params = Params,
status = #{is_alive => true}}, status = #{is_alive => true}},
emqx_rule_registry:add_resource_params(ResParams) emqx_rule_registry:add_resource_params(ResParams).
catch Class:Reason:ST ->
emqx_rule_monitor:ensure_resource_retrier(ResId),
erlang:raise(Class, {init_resource, Reason}, ST)
end.
init_action(Module, OnCreate, ActionInstId, Params) -> init_action(Module, OnCreate, ActionInstId, Params) ->
ok = emqx_rule_metrics:create_metrics(ActionInstId), ok = emqx_rule_metrics:create_metrics(ActionInstId),