完善host的删除逻辑

This commit is contained in:
anlicheng 2025-11-27 16:14:51 +08:00
parent 381b44df9a
commit 5d143be3ef
2 changed files with 26 additions and 4 deletions

View File

@ -22,7 +22,7 @@
%% API
-export([start_link/2, get_name/1, get_alias_name/1, get_pid/1, handle/2, activate/2]).
-export([get_metric/1, get_status/1]).
-export([get_metric/1, get_status/1, kill/1]).
%%
-export([pub/3, attach_channel/2, command/3]).
-export([deploy_container/3, start_container/2, stop_container/2, remove_container/2, kill_container/2, config_container/3, get_containers/1, await_reply/2]).
@ -61,6 +61,15 @@ get_alias_name(HostId0) when is_integer(HostId0) ->
HostId = integer_to_binary(HostId0),
binary_to_atom(<<"iot_host_id:", HostId/binary>>).
-spec kill(UUID :: binary()) -> no_return().
kill(UUID) when is_binary(UUID) ->
case whereis(get_name(UUID)) of
undefined ->
ok;
Pid ->
exit(Pid, kill)
end.
%%
-spec handle(Pid :: pid(), Packet :: {atom(), any()}) -> no_return().
handle(Pid, Packet) when is_pid(Pid) ->

View File

@ -35,10 +35,23 @@ ensured_host_started(UUID) when is_binary(UUID) ->
{ok, Pid}
end.
delete_host(UUID) when is_binary(UUID) ->
delete_host(UUID) ->
Id = iot_host:get_name(UUID),
ok = supervisor:terminate_child(?MODULE, Id),
supervisor:delete_child(?MODULE, Id).
case supervisor:terminate_child(?MODULE, Id) of
{error, running} ->
iot_host:kill(UUID),
ok;
_ ->
ok
end,
case supervisor:delete_child(?MODULE, Id) of
{error, running} ->
%% ensure killed then delete again
iot_host:kill(UUID),
supervisor:delete_child(?MODULE, Id);
_ ->
ok
end.
child_spec(UUID) ->
Id = iot_host:get_name(UUID),