From dc50a33d88d98e54e21b6b192d66e7834f142544 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 28 Oct 2025 22:39:45 +0800 Subject: [PATCH] add kill container --- HTTP_API_README.md | 33 +++++++++++++++++++ .../src/http_handlers/container_handler.erl | 18 ++++++++++ apps/iot/src/iot_host.erl | 8 ++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/HTTP_API_README.md b/HTTP_API_README.md index a498f66..e5c02b7 100644 --- a/HTTP_API_README.md +++ b/HTTP_API_README.md @@ -578,6 +578,39 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) } ``` +### 3️⃣ 强制停止 Endpoint + +**URL**:`/endpoint/kill` +**Method**:`POST` + +#### 请求参数 +| 参数名 | 类型 | 必填 | 说明 | +|--------|------|------|------| +| id | integer | ✅ | Endpoint 唯一 ID | + +#### 响应参数 +| 字段 | 类型 | 说明 | +|------|------|------| +| result | string | 停止结果,如 `"success"` | + +#### 示例响应 +```json +{ + "result": "success" +} +``` + +#### 错误响应 +```json +{ + "error": { + "code": 404, + "message": "stop endpoint error" + } +} +``` + + --- ### 4️⃣ 重启 Endpoint diff --git a/apps/iot/src/http_handlers/container_handler.erl b/apps/iot/src/http_handlers/container_handler.erl index 65e2e87..d384e46 100644 --- a/apps/iot/src/http_handlers/container_handler.erl +++ b/apps/iot/src/http_handlers/container_handler.erl @@ -116,6 +116,24 @@ handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"container_ end end; +handle_request("POST", "/container/kill", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName}) when is_binary(UUID), is_binary(ContainerName) -> + case iot_host:get_pid(UUID) of + undefined -> + {ok, 200, iot_util:json_error(404, <<"host not found">>)}; + Pid when is_pid(Pid) -> + case iot_host:kill_container(Pid, ContainerName) of + {ok, Ref} -> + case iot_host:await_reply(Ref, 5000) of + {ok, Result} -> + {ok, 200, iot_util:json_data(Result)}; + {error, Reason} -> + {ok, 200, iot_util:json_error(400, Reason)} + end; + {error, Reason} when is_binary(Reason) -> + {ok, 200, iot_util:json_error(400, Reason)} + end + end; + %% 删除容器 handle_request("POST", "/container/remove", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName}) when is_binary(UUID), is_binary(ContainerName) -> case iot_host:get_pid(UUID) of diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index cc541f6..41569f1 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -25,7 +25,7 @@ -export([get_metric/1, get_status/1]). %% 通讯相关 -export([pub/3, attach_channel/2, command/3]). --export([deploy_container/3, start_container/2, stop_container/2, remove_container/2, config_container/3, get_containers/1, await_reply/2]). +-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]). %% 设备管理 -export([reload_device/2, delete_device/2, activate_device/3]). -export([heartbeat/1]). @@ -119,6 +119,12 @@ stop_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), gen_statem:call(Pid, {jsonrpc_call, self(), EncCallBin}). +-spec kill_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. +kill_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> + Request = #jsonrpc_request{method = <<"kill_container">>, params = #{<<"container_name">> => ContainerName}}, + EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), + gen_statem:call(Pid, {jsonrpc_call, self(), EncCallBin}). + -spec remove_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. remove_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> Request = #jsonrpc_request{method = <<"remove_container">>, params = #{<<"container_name">> => ContainerName}},