94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
// Package handler provides HTTP handlers for various endpoints.
|
|
package handler
|
|
|
|
import (
|
|
"modelRT/constants"
|
|
"modelRT/database"
|
|
"modelRT/logger"
|
|
"modelRT/network"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gofrs/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AsyncTaskResultDetailHandler handles detailed query of a single async task result
|
|
// @Summary 查询异步任务详情
|
|
// @Description 根据任务ID查询异步任务的详细状态和结果
|
|
// @Tags AsyncTask
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param task_id path string true "任务ID"
|
|
// @Success 200 {object} network.SuccessResponse{payload=network.AsyncTaskResult} "查询成功"
|
|
// @Failure 200 {object} network.FailureResponse "请求参数错误"
|
|
// @Router /task/async/{task_id} [get]
|
|
func AsyncTaskResultDetailHandler(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
|
|
}
|
|
|
|
taskResult, err := database.GetAsyncTaskResult(ctx, pgClient, taskID)
|
|
if err != nil {
|
|
logger.Error(ctx, "failed to query async task result from database", "error", err)
|
|
renderRespFailure(c, constants.RespCodeServerError, "failed to query task result", nil)
|
|
return
|
|
}
|
|
|
|
responseTask := network.AsyncTaskResult{
|
|
TaskID: asyncTask.TaskID,
|
|
TaskType: string(asyncTask.TaskType),
|
|
Status: string(asyncTask.Status),
|
|
CreatedAt: asyncTask.CreatedAt,
|
|
FinishedAt: asyncTask.FinishedAt,
|
|
Progress: asyncTask.Progress,
|
|
}
|
|
|
|
if taskResult != nil {
|
|
if taskResult.Result != nil {
|
|
responseTask.Result = map[string]any(taskResult.Result)
|
|
}
|
|
if taskResult.ErrorCode != nil {
|
|
responseTask.ErrorCode = taskResult.ErrorCode
|
|
}
|
|
if taskResult.ErrorMessage != nil {
|
|
responseTask.ErrorMessage = taskResult.ErrorMessage
|
|
}
|
|
if taskResult.ErrorDetail != nil {
|
|
responseTask.ErrorDetail = map[string]any(taskResult.ErrorDetail)
|
|
}
|
|
}
|
|
|
|
renderRespSuccess(c, constants.RespCodeSuccess, "query completed", responseTask)
|
|
}
|