87 lines
2.8 KiB
Go
87 lines
2.8 KiB
Go
// Package handler provides HTTP handlers for various endpoints.
|
||
package handler
|
||
|
||
import (
|
||
"time"
|
||
|
||
"modelRT/constants"
|
||
"modelRT/database"
|
||
"modelRT/logger"
|
||
"modelRT/orm"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/gofrs/uuid"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// AsyncTaskCancelHandler handles cancellation of an async task
|
||
// @Summary 取消异步任务
|
||
// @Description 取消指定ID的异步任务(如果任务尚未开始执行)
|
||
// @Tags AsyncTask
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param task_id path string true "任务ID"
|
||
// @Success 200 {object} network.SuccessResponse "任务取消成功"
|
||
// @Failure 200 {object} network.FailureResponse "请求参数错误或任务无法取消"
|
||
// @Router /task/async/{task_id}/cancel [post]
|
||
func AsyncTaskCancelHandler(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
taskIDStr := c.Param("task_id")
|
||
if taskIDStr == "" {
|
||
logger.Error(ctx, "task_id parameter is required")
|
||
renderRespFailure(c, constants.RespCodeInvalidParams, "task_id parameter is required", nil)
|
||
return
|
||
}
|
||
|
||
taskID, err := uuid.FromString(taskIDStr)
|
||
if err != nil {
|
||
logger.Error(ctx, "invalid task ID format", "task_id", taskIDStr, "error", err)
|
||
renderRespFailure(c, constants.RespCodeInvalidParams, "invalid task ID format", nil)
|
||
return
|
||
}
|
||
|
||
pgClient := database.GetPostgresDBClient()
|
||
if pgClient == nil {
|
||
logger.Error(ctx, "database connection not found in context")
|
||
renderRespFailure(c, constants.RespCodeServerError, "database connection error", nil)
|
||
return
|
||
}
|
||
|
||
asyncTask, err := database.GetAsyncTaskByID(ctx, pgClient, taskID)
|
||
if err != nil {
|
||
if err == gorm.ErrRecordNotFound {
|
||
logger.Error(ctx, "async task not found", "task_id", taskID)
|
||
renderRespFailure(c, constants.RespCodeInvalidParams, "task not found", nil)
|
||
return
|
||
}
|
||
logger.Error(ctx, "failed to query async task from database", "error", err)
|
||
renderRespFailure(c, constants.RespCodeServerError, "failed to query task", nil)
|
||
return
|
||
}
|
||
|
||
if asyncTask.Status != orm.AsyncTaskStatusSubmitted {
|
||
logger.Error(ctx, "task cannot be cancelled", "task_id", taskID, "status", asyncTask.Status)
|
||
renderRespFailure(c, constants.RespCodeInvalidParams, "task cannot be cancelled, already running or completed", nil)
|
||
return
|
||
}
|
||
|
||
timestamp := time.Now().Unix()
|
||
err = database.FailAsyncTask(ctx, pgClient, taskID, timestamp)
|
||
if err != nil {
|
||
logger.Error(ctx, "failed to cancel async task", "task_id", taskID, "error", err)
|
||
renderRespFailure(c, constants.RespCodeServerError, "failed to cancel task", nil)
|
||
return
|
||
}
|
||
|
||
err = database.UpdateAsyncTaskResultWithError(ctx, pgClient, taskID, 40009, "task cancelled by user", orm.JSONMap{
|
||
"cancelled_at": timestamp,
|
||
"cancelled_by": "user",
|
||
})
|
||
if err != nil {
|
||
logger.Error(ctx, "failed to update task result with cancellation error", "task_id", taskID, "error", err)
|
||
}
|
||
|
||
renderRespSuccess(c, constants.RespCodeSuccess, "task cancelled successfully", nil)
|
||
}
|