fix(mqtt_bridge): the mqtt bridge hangs with an unreachable IP

This commit is contained in:
Shawn 2021-11-24 17:11:04 +08:00
parent b6064ce2c0
commit a54668e83b
1 changed files with 36 additions and 22 deletions

View File

@ -410,21 +410,35 @@ start_resource(ResId, PoolName, Options) ->
end. end.
test_resource_status(PoolName) -> test_resource_status(PoolName) ->
IsConnected = fun(Worker) -> Parent = self(),
case ecpool_worker:client(Worker) of Pids = [spawn(fun() -> Parent ! {self(), get_worker_status(Worker)} end)
{ok, Bridge} -> || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
try emqx_bridge_worker:status(Bridge) of try
connected -> true; Status = [
_ -> false receive {Pid, R} -> R
catch _Error:_Reason -> after 1000 -> %% get_worker_status/1 should be a quick operation
false throw({timeout, Pid})
end; end || Pid <- Pids],
{error, _} -> lists:any(fun(St) -> St =:= true end, Status)
false catch
end throw:Reason ->
end, ?LOG(error, "Get mqtt bridge status timeout: ~p", [Reason]),
Status = [IsConnected(Worker) || {_WorkerName, Worker} <- ecpool:workers(PoolName)], lists:foreach(fun(Pid) -> exit(Pid, kill) end, Pids),
lists:any(fun(St) -> St =:= true end, Status). false
end.
get_worker_status(Worker) ->
case ecpool_worker:client(Worker) of
{ok, Bridge} ->
try emqx_bridge_worker:status(Bridge) of
connected -> true;
_ -> false
catch _Error:_Reason ->
false
end;
{error, _} ->
false
end.
-spec(on_get_resource_status(ResId::binary(), Params::map()) -> Status::map()). -spec(on_get_resource_status(ResId::binary(), Params::map()) -> Status::map()).
on_get_resource_status(_ResId, #{<<"pool">> := PoolName}) -> on_get_resource_status(_ResId, #{<<"pool">> := PoolName}) ->
@ -433,13 +447,13 @@ on_get_resource_status(_ResId, #{<<"pool">> := PoolName}) ->
on_resource_destroy(ResId, #{<<"pool">> := PoolName}) -> on_resource_destroy(ResId, #{<<"pool">> := PoolName}) ->
?LOG(info, "Destroying Resource ~p, ResId: ~p", [?RESOURCE_TYPE_MQTT, ResId]), ?LOG(info, "Destroying Resource ~p, ResId: ~p", [?RESOURCE_TYPE_MQTT, ResId]),
case ecpool:stop_sup_pool(PoolName) of case ecpool:stop_sup_pool(PoolName) of
ok -> ok ->
?LOG(info, "Destroyed Resource ~p Successfully, ResId: ~p", [?RESOURCE_TYPE_MQTT, ResId]); ?LOG(info, "Destroyed Resource ~p Successfully, ResId: ~p", [?RESOURCE_TYPE_MQTT, ResId]);
{error, Reason} -> {error, Reason} ->
?LOG(error, "Destroy Resource ~p failed, ResId: ~p, ~p", [?RESOURCE_TYPE_MQTT, ResId, Reason]), ?LOG(error, "Destroy Resource ~p failed, ResId: ~p, ~p", [?RESOURCE_TYPE_MQTT, ResId, Reason]),
error({{?RESOURCE_TYPE_MQTT, ResId}, destroy_failed}) error({{?RESOURCE_TYPE_MQTT, ResId}, destroy_failed})
end. end.
on_action_create_data_to_mqtt_broker(ActId, Opts = #{<<"pool">> := PoolName, on_action_create_data_to_mqtt_broker(ActId, Opts = #{<<"pool">> := PoolName,
<<"forward_topic">> := ForwardTopic, <<"forward_topic">> := ForwardTopic,