后端:
1.阶段 5 task 服务边界落地
- 新增 cmd/task 与 services/task/{dao,rpc,sv},承载 task zrpc、tasks 表迁移和 task outbox 消费边界
- 新增 gateway/client/task、shared/contracts/task 和 task port,gateway /api/v1/task/* 切到 task zrpc client
- 将 task.urgency.promote.requested handler / relay / retry loop 迁入 cmd/task,单体 worker 不再消费 task outbox
- 保留单体 Agent 残留 task 查询的 publish-only 写入能力,避免迁移期 task 事件丢失
- active-scheduler task facts / due job scanner 切到 task RPC,并移除启动期 tasks 表依赖检查
- 更新阶段 5 文档,记录 task 切流点、旧实现保留、跨域 DB 依赖缩减和下一轮建议
- 补充 task rpc 示例配置
155 lines
4.4 KiB
Go
155 lines
4.4 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/respond"
|
||
taskcontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/task"
|
||
"github.com/LoveLosita/smartflow/backend/shared/ports"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
const taskRequestTimeout = 6 * time.Second
|
||
|
||
type TaskHandler struct {
|
||
client ports.TaskCommandClient
|
||
}
|
||
|
||
// NewTaskHandler 创建 task HTTP 门面。
|
||
func NewTaskHandler(client ports.TaskCommandClient) *TaskHandler {
|
||
return &TaskHandler{client: client}
|
||
}
|
||
|
||
func (th *TaskHandler) AddTask(c *gin.Context) {
|
||
var req taskcontracts.AddTaskRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), taskRequestTimeout)
|
||
defer cancel()
|
||
resp, err := th.client.AddTask(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
func (th *TaskHandler) GetUserTasks(c *gin.Context) {
|
||
userID := c.GetInt("user_id")
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), taskRequestTimeout)
|
||
defer cancel()
|
||
resp, err := th.client.GetUserTasks(ctx, userID)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
// BatchTaskStatus 批量查询当前用户任务的实时完成状态。
|
||
//
|
||
// 职责边界:
|
||
// 1. 负责解析 ids 与读取鉴权上下文中的 user_id;
|
||
// 2. 负责调用 task 服务,不直接读取任务缓存或数据库;
|
||
// 3. 不修改任务、不触发幂等中间件、不反写 NewAgent timeline 历史 payload。
|
||
func (th *TaskHandler) BatchTaskStatus(c *gin.Context) {
|
||
var req taskcontracts.BatchTaskStatusRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), taskRequestTimeout)
|
||
defer cancel()
|
||
resp, err := th.client.BatchTaskStatus(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
// CompleteTask 标记任务为已完成。
|
||
func (th *TaskHandler) CompleteTask(c *gin.Context) {
|
||
var req taskcontracts.CompleteTaskRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), taskRequestTimeout)
|
||
defer cancel()
|
||
resp, err := th.client.CompleteTask(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
// UndoCompleteTask 取消任务“已完成”勾选。
|
||
func (th *TaskHandler) UndoCompleteTask(c *gin.Context) {
|
||
var req taskcontracts.UndoCompleteTaskRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), taskRequestTimeout)
|
||
defer cancel()
|
||
resp, err := th.client.UndoCompleteTask(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
// UpdateTask 更新任务属性(部分更新)。
|
||
func (th *TaskHandler) UpdateTask(c *gin.Context) {
|
||
var req taskcontracts.UpdateTaskRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), taskRequestTimeout)
|
||
defer cancel()
|
||
resp, err := th.client.UpdateTask(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
// DeleteTask 永久删除指定任务。
|
||
func (th *TaskHandler) DeleteTask(c *gin.Context) {
|
||
var req taskcontracts.DeleteTaskRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), taskRequestTimeout)
|
||
defer cancel()
|
||
resp, err := th.client.DeleteTask(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|