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),