refactor(knowledge): route single delete through async pipeline
Reuse enqueueKnowledgeListDelete inside DeleteKnowledge so that single-item delete shares the same hardening as BatchDeleteKnowledge / ClearKnowledgeBase: asynq retries, business-aware queue routing, and marking-as-deleting inside the worker. The endpoint now returns 200 once the delete task has been enqueued; the response body carries the asynq task_id and the message is updated to "Delete task submitted". Swagger annotations, generated docs and the Go client SDK comment are updated to reflect the new asynchronous semantics. Note: this is a behavior change. Callers that previously assumed the knowledge was already gone on a 200 response should poll the task status or accept eventual consistency, matching the existing BatchDeleteKnowledge contract.
This commit is contained in:
+3
-1
@@ -353,7 +353,9 @@ func (c *Client) ListKnowledgeWithFilter(ctx context.Context,
|
|||||||
return response.Data, response.Total, nil
|
return response.Data, response.Total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteKnowledge deletes a knowledge entry by its ID
|
// DeleteKnowledge enqueues an asynchronous delete for the given knowledge entry.
|
||||||
|
// The server returns 200 once the task has been submitted; the actual deletion is
|
||||||
|
// performed by a background worker (same pipeline as BatchDeleteKnowledge).
|
||||||
func (c *Client) DeleteKnowledge(ctx context.Context, knowledgeID string) error {
|
func (c *Client) DeleteKnowledge(ctx context.Context, knowledgeID string) error {
|
||||||
path := fmt.Sprintf("/api/v1/knowledge/%s", knowledgeID)
|
path := fmt.Sprintf("/api/v1/knowledge/%s", knowledgeID)
|
||||||
resp, err := c.doRequest(ctx, http.MethodDelete, path, nil, nil)
|
resp, err := c.doRequest(ctx, http.MethodDelete, path, nil, nil)
|
||||||
|
|||||||
+2
-2
@@ -5926,7 +5926,7 @@ const docTemplate = `{
|
|||||||
"ApiKeyAuth": []
|
"ApiKeyAuth": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"description": "根据ID删除知识条目",
|
"description": "根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq);\n接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。",
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
@@ -5948,7 +5948,7 @@ const docTemplate = `{
|
|||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "删除成功",
|
"description": "任务已提交,返回 task_id",
|
||||||
"schema": {
|
"schema": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"additionalProperties": true
|
"additionalProperties": true
|
||||||
|
|||||||
+2
-2
@@ -5919,7 +5919,7 @@
|
|||||||
"ApiKeyAuth": []
|
"ApiKeyAuth": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"description": "根据ID删除知识条目",
|
"description": "根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq);\n接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。",
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
@@ -5941,7 +5941,7 @@
|
|||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "删除成功",
|
"description": "任务已提交,返回 task_id",
|
||||||
"schema": {
|
"schema": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"additionalProperties": true
|
"additionalProperties": true
|
||||||
|
|||||||
+4
-2
@@ -7901,7 +7901,9 @@ paths:
|
|||||||
delete:
|
delete:
|
||||||
consumes:
|
consumes:
|
||||||
- application/json
|
- application/json
|
||||||
description: 根据ID删除知识条目
|
description: |-
|
||||||
|
根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq);
|
||||||
|
接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。
|
||||||
parameters:
|
parameters:
|
||||||
- description: 知识ID
|
- description: 知识ID
|
||||||
in: path
|
in: path
|
||||||
@@ -7912,7 +7914,7 @@ paths:
|
|||||||
- application/json
|
- application/json
|
||||||
responses:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: 删除成功
|
description: 任务已提交,返回 task_id
|
||||||
schema:
|
schema:
|
||||||
additionalProperties: true
|
additionalProperties: true
|
||||||
type: object
|
type: object
|
||||||
|
|||||||
@@ -651,12 +651,13 @@ func (h *KnowledgeHandler) ListKnowledge(c *gin.Context) {
|
|||||||
|
|
||||||
// DeleteKnowledge godoc
|
// DeleteKnowledge godoc
|
||||||
// @Summary 删除知识
|
// @Summary 删除知识
|
||||||
// @Description 根据ID删除知识条目
|
// @Description 根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq);
|
||||||
|
// @Description 接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。
|
||||||
// @Tags 知识管理
|
// @Tags 知识管理
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param id path string true "知识ID"
|
// @Param id path string true "知识ID"
|
||||||
// @Success 200 {object} map[string]interface{} "删除成功"
|
// @Success 200 {object} map[string]interface{} "任务已提交,返回 task_id"
|
||||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
@@ -678,18 +679,32 @@ func (h *KnowledgeHandler) DeleteKnowledge(c *gin.Context) {
|
|||||||
c.Error(err)
|
c.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Infof(ctx, "Deleting knowledge, ID: %s", secutils.SanitizeForLog(id))
|
|
||||||
err = h.kgService.DeleteKnowledge(effCtx, id)
|
// Reuse the batch async pipeline so single-item delete shares the same
|
||||||
if err != nil {
|
// hardening (asynq retries, business-aware queue routing, marking-as-deleting
|
||||||
logger.ErrorWithFields(ctx, err, nil)
|
// inside the worker) as BatchDeleteKnowledge / ClearKnowledgeBaseContents.
|
||||||
c.Error(errors.NewInternalServerError(err.Error()))
|
effectiveTenantID, _ := effCtx.Value(types.TenantIDContextKey).(uint64)
|
||||||
|
if effectiveTenantID == 0 {
|
||||||
|
logger.Error(ctx, "Effective tenant ID missing after access validation")
|
||||||
|
c.Error(errors.NewInternalServerError("tenant context unavailable"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Infof(ctx, "Knowledge deleted successfully, ID: %s", secutils.SanitizeForLog(id))
|
logger.Infof(ctx, "Enqueuing knowledge delete, ID: %s", secutils.SanitizeForLog(id))
|
||||||
|
taskID, err := h.enqueueKnowledgeListDelete(effCtx, effectiveTenantID, []string{id})
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf(ctx, "Failed to enqueue knowledge delete task: %v", err)
|
||||||
|
c.Error(errors.NewInternalServerError("Failed to enqueue delete task"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "Knowledge delete task enqueued: %s, knowledge_id: %s", taskID, secutils.SanitizeForLog(id))
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "Deleted successfully",
|
"message": "Delete task submitted",
|
||||||
|
"data": gin.H{
|
||||||
|
"task_id": taskID,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user