fix(rule-engine): reply 500 when update resource failed (#4120)
This commit is contained in:
parent
a3206f5e0b
commit
6eff70a646
|
@ -300,7 +300,7 @@ do_update_resource(#{id := Id, type := Type, description := NewDescription, conf
|
||||||
cluster_call(init_resource, [Module, Create, Id, Config]),
|
cluster_call(init_resource, [Module, Create, Id, Config]),
|
||||||
emqx_rule_registry:add_resource(Resource);
|
emqx_rule_registry:add_resource(Resource);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
error({error, Reason})
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -273,6 +273,8 @@ do_create_resource(Create, ParsedParams) ->
|
||||||
return({ok, record_to_map(Resource)});
|
return({ok, record_to_map(Resource)});
|
||||||
{error, {resource_type_not_found, Type}} ->
|
{error, {resource_type_not_found, Type}} ->
|
||||||
return({error, 400, ?ERR_NO_RESOURCE_TYPE(Type)});
|
return({error, 400, ?ERR_NO_RESOURCE_TYPE(Type)});
|
||||||
|
{error, {init_resource_failure, _}} ->
|
||||||
|
return({error, 500, <<"Init resource failure!">>});
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
||||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||||
|
|
|
@ -196,6 +196,10 @@ on_resource_create(ResId, Conf) ->
|
||||||
{ok, _} = application:ensure_all_started(ehttpc),
|
{ok, _} = application:ensure_all_started(ehttpc),
|
||||||
Options = pool_opts(Conf),
|
Options = pool_opts(Conf),
|
||||||
PoolName = pool_name(ResId),
|
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),
|
start_resource(ResId, PoolName, Options),
|
||||||
Conf#{<<"pool">> => PoolName, options => Options}.
|
Conf#{<<"pool">> => PoolName, options => Options}.
|
||||||
|
|
||||||
|
@ -214,15 +218,8 @@ start_resource(ResId, PoolName, Options) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec(on_get_resource_status(binary(), map()) -> map()).
|
-spec(on_get_resource_status(binary(), map()) -> map()).
|
||||||
on_get_resource_status(ResId, #{<<"url">> := Url}) ->
|
on_get_resource_status(_ResId, Conf) ->
|
||||||
#{is_alive =>
|
#{is_alive => test_http_connect(Conf)}.
|
||||||
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}.
|
|
||||||
|
|
||||||
-spec(on_resource_destroy(binary(), map()) -> ok | {error, Reason::term()}).
|
-spec(on_resource_destroy(binary(), map()) -> ok | {error, Reason::term()}).
|
||||||
on_resource_destroy(ResId, #{<<"pool">> := PoolName}) ->
|
on_resource_destroy(ResId, #{<<"pool">> := PoolName}) ->
|
||||||
|
@ -385,4 +382,19 @@ parse_host(Host) ->
|
||||||
{ok, _} -> {inet6, Host};
|
{ok, _} -> {inet6, Host};
|
||||||
{error, _} -> {inet, Host}
|
{error, _} -> {inet, Host}
|
||||||
end
|
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.
|
end.
|
Loading…
Reference in New Issue