fix heartbeat

This commit is contained in:
anlicheng 2026-05-09 17:12:56 +08:00
parent aa4a62f3e8
commit 7fd82ac61d
3 changed files with 22 additions and 5 deletions

View File

@ -307,7 +307,8 @@ handle_event(cast, {handle, {ping, Metrics}}, ?STATE_ACTIVATED, State = #state{u
{keep_state, State#state{metrics = Metrics}}; {keep_state, State#state{metrics = Metrics}};
%% %%
handle_event(cast, heartbeat, _, State = #state{heartbeat_counter = HeartbeatCounter}) -> handle_event(cast, heartbeat, _, State = #state{uuid = UUID, heartbeat_counter = HeartbeatCounter}) ->
maybe_mark_host_online(UUID),
{keep_state, State#state{heartbeat_counter = HeartbeatCounter + 1}}; {keep_state, State#state{heartbeat_counter = HeartbeatCounter + 1}};
%% 线, %% 线,
@ -403,3 +404,18 @@ maybe_mark_host_offline(UUID) ->
logger:warning("[iot_host] host: ~p, load status failed while marking offline: ~p", [UUID, Other]), logger:warning("[iot_host] host: ~p, load status failed while marking offline: ~p", [UUID, Other]),
ok ok
end. end.
-spec maybe_mark_host_online(binary()) -> ok.
maybe_mark_host_online(UUID) ->
case iot_api_client:get_host_by_uuid(UUID) of
{ok, #host_info{status = ?HOST_OFFLINE}} ->
_ = iot_api_client:change_host_status(UUID, ?HOST_ONLINE),
ok;
{ok, #host_info{status = ?HOST_ONLINE}} ->
ok;
{ok, #host_info{status = ?HOST_NOT_JOINED}} ->
ok;
Other ->
logger:warning("[iot_host] host: ~p, load status failed while marking online: ~p", [UUID, Other]),
ok
end.

View File

@ -21,6 +21,7 @@ loop(Transport = {udp, Server, _Sock}, Peer) ->
Pid = iot_host:get_pid(HostUUID), Pid = iot_host:get_pid(HostUUID),
iot_host:heartbeat(Pid), iot_host:heartbeat(Pid),
loop(Transport, Peer); loop(Transport, Peer);
{datagram, Server, _} -> {datagram, Server, Packet} ->
exit(normal) logger:warning("[udp_server] ignore invalid heartbeat packet from peer: ~p, packet: ~p", [Peer, Packet]),
loop(Transport, Peer)
end. end.