fix(rule-engine): reply 500 when update resource failed (#4120)

This commit is contained in:
wwhai 2021-02-01 09:47:08 +08:00 committed by GitHub
parent a3206f5e0b
commit 6eff70a646
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 10 deletions

View File

@ -300,7 +300,7 @@ do_update_resource(#{id := Id, type := Type, description := NewDescription, conf
cluster_call(init_resource, [Module, Create, Id, Config]),
emqx_rule_registry:add_resource(Resource);
{error, Reason} ->
{error, Reason}
error({error, Reason})
end
end.

View File

@ -273,6 +273,8 @@ do_create_resource(Create, ParsedParams) ->
return({ok, record_to_map(Resource)});
{error, {resource_type_not_found, Type}} ->
return({error, 400, ?ERR_NO_RESOURCE_TYPE(Type)});
{error, {init_resource_failure, _}} ->
return({error, 500, <<"Init resource failure!">>});
{error, Reason} ->
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
return({error, 400, ?ERR_BADARGS(Reason)})

View File

@ -196,6 +196,10 @@ on_resource_create(ResId, Conf) ->
{ok, _} = application:ensure_all_started(ehttpc),
Options = pool_opts(Conf),
PoolName = pool_name(ResId),
case test_http_connect(Conf) of
true -> ok;
false -> error({error, check_http_connectivity_failed})
end,
start_resource(ResId, PoolName, Options),
Conf#{<<"pool">> => PoolName, options => Options}.
@ -214,15 +218,8 @@ start_resource(ResId, PoolName, Options) ->
end.
-spec(on_get_resource_status(binary(), map()) -> map()).
on_get_resource_status(ResId, #{<<"url">> := Url}) ->
#{is_alive =>
case emqx_rule_utils:http_connectivity(Url) of
ok -> true;
{error, Reason} ->
?LOG(error, "Connectivity Check for ~p failed, ResId: ~p, ~0p",
[?RESOURCE_TYPE_WEBHOOK, ResId, Reason]),
false
end}.
on_get_resource_status(_ResId, Conf) ->
#{is_alive => test_http_connect(Conf)}.
-spec(on_resource_destroy(binary(), map()) -> ok | {error, Reason::term()}).
on_resource_destroy(ResId, #{<<"pool">> := PoolName}) ->
@ -385,4 +382,19 @@ parse_host(Host) ->
{ok, _} -> {inet6, Host};
{error, _} -> {inet, Host}
end
end.
test_http_connect(Conf) ->
Url = fun() -> maps:get(<<"url">>, Conf) end,
try
emqx_rule_utils:http_connectivity(Url())
of
ok -> true;
{error, _Reason} ->
?LOG(error, "check http_connectivity failed: ~p", [Url()]),
false
catch
Err:Reason:ST ->
?LOG(error, "check http_connectivity failed: ~p, ~0p", [Conf, {Err, Reason, ST}]),
false
end.