add kill container

This commit is contained in:
anlicheng 2025-10-28 22:39:45 +08:00
parent 184909aea3
commit dc50a33d88
3 changed files with 58 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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}},