From 5d143be3ef2515d396444ac14a3ae549ad952086 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 27 Nov 2025 16:14:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84host=E7=9A=84=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/iot/src/iot_host.erl | 11 ++++++++++- apps/iot/src/iot_host_sup.erl | 19 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index d8adbfa..3eea328 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -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) -> diff --git a/apps/iot/src/iot_host_sup.erl b/apps/iot/src/iot_host_sup.erl index 26d875e..16f8b4e 100644 --- a/apps/iot/src/iot_host_sup.erl +++ b/apps/iot/src/iot_host_sup.erl @@ -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),