# 取消工作流作业

请求取消指定工作流作业。HTTP 成功不表示作业已终止；请读取返回状态或查询作业详情确认最终状态。

```text
POST https://moi.matrixorigin.cn/newmoi/workflow/v2/workflow-apps/{workflow_id}/executions/{execution_id}/cancel
```

## 调用前准备

先[查询作业详情](get-workflow-job.md)，检查 `available_actions` 是否包含取消。准备有目标工作区访问权限的个人访问令牌和目标工作区 ID。

下方示例使用：

- `$AI_STUDIO_API_KEY`：实际个人访问令牌，通过 `X-API-Key` Header 传递。
- `$WORKSPACE_ID`：目标工作区 ID，通过 `X-Workspace-ID` Header 传递。
- `$WORKFLOW_ID`：作业所属的工作流 ID。
- `$EXECUTION_ID`：要取消的作业执行 ID。

请求体不能包含 `runtime_context`。

## 路径参数

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `workflow_id` | string | 工作流 ID。 |
| `execution_id` | string | 作业 ID。 |

## 请求体

| 字段 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `reason` | string | 否 | 兼容字段；当前不影响取消处理。 |

## 请求示例

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/workflow/v2/workflow-apps/$WORKFLOW_ID/executions/$EXECUTION_ID/cancel" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d '{
    "reason": "<REASON>"
  }'
```

## 成功响应

成功时返回 `200`。如果作业已经是取消状态，接口仍返回其当前详情。

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "execution": {
      "execution_id": "exec-001",
      "workflow_id": "wf-001",
      "status": "cancelled",
      "available_actions": [],
      "execution_mode": "one_shot",
      "input_payload": {},
      "vars_payload": {}
    }
  }
}
```

响应字段如下。

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `code` | string | 成功时为 `OK`。 |
| `msg` | string | 成功时为 `OK`。 |
| `data.execution.execution_id` | string | 执行 ID。 |
| `data.execution.workflow_id` | string | 工作流 ID。 |
| `data.execution.status` | string | 当前执行状态；以此确认取消是否完成。 |
| `data.execution.available_actions` | string（字符串数组） | 当前允许的操作。 |
| `data.execution.execution_mode` | string | 执行模式。 |
| `data.execution.cron_expression` | string | Cron 表达式；仅定时模式返回。 |
| `data.execution.input_payload` | object | 已展开的运行输入。 |
| `data.execution.vars_payload` | object | 已展开的运行变量。 |
| `data.execution.moi_task_id` | string | 任务标识；服务端已分配时返回。 |
| `data.execution.moi_case_id` | string | 案例标识；服务端已分配时返回。 |
| `data.execution.moi_workflow_def_id` | string | 工作流定义标识；服务端已分配时返回。 |
| `data.execution.moi_workflow_version_id` | string | 工作流版本标识；服务端已分配时返回。 |
| `data.execution.pause_scope` | string | 暂停范围；有值时返回。 |
| `data.execution.error` | string | 错误信息；有值时返回。 |
| `data.execution.started_at` | string | 开始时间；未产生时可能省略。 |
| `data.execution.ended_at` | string | 结束时间；未产生时可能省略。 |
| `data.execution.created_at` | string | 创建时间；服务端提供时返回。 |
| `data.execution.updated_at` | string | 最后更新时间；服务端提供时返回。 |

## 错误响应

```json
{
  "code": "ErrParamInvalid",
  "msg": "请求参数无效",
  "data": null
}
```

### 常见 HTTP 错误

```{list-table}
:header-rows: 1
:widths: 12 22 32 34

* - HTTP 状态码
  - 错误代码
  - 常见原因
  - 建议操作
* - `400`
  - `ErrParamInvalid`
  - 请求体无效、包含 `runtime_context`，或作业已处于不允许取消的终态。
  - 读取作业详情并根据当前状态处理。
* - `401`
  - `ErrUnauthorized`
  - 缺少或无效的访问凭据。
  - 检查 API Key 和工作区 Header。
* - `403`
  - `ErrForbidden`
  - 当前身份没有工作流运行权限。
  - 请求授予 `workflow.run` 权限。
* - `404`
  - `ErrNotFound`
  - 作业不存在或不属于路径中的工作流。
  - 核对工作流 ID 和执行 ID。
* - `409`
  - `ErrConflict`
  - 当前作业状态与取消操作冲突。
  - 刷新状态后再决定是否重试。
* - `503`
  - `ErrServiceUnavailable`
  - 依赖服务暂不可用。
  - 稍后重试。
* - `500`
  - `ErrServer`
  - 服务端未能取消作业。
  - 稍后重试。
```

## 后续操作

用同一作业标识[查询工作流作业详情](get-workflow-job.md)确认最终状态。
