Files
yokowu 6644bc1d4b refactor: 用 Lifecycle Manager 替换 Tasker
核心改动:
- TaskUsecase: 移除 tasker 依赖,改用 lifecycle.Transition
- CreateTaskReq 临时存储到 Redis (10 分钟过期)
- InternalHostHandler: 移除 tasker 依赖
- TaskRepo: 实现 UpdateStatus 方法

完整流程:
1. TaskUsecase.Create() 存储 CreateTaskReq 到 Redis
2. VM 就绪时 TaskCreateHook 从 Redis 读取并创建 taskflow 任务
3. VMTaskHook 自动同步任务状态
2026-03-19 16:51:04 +08:00

53 lines
1.4 KiB
Go

package lifecycle
import (
"context"
"log/slog"
"github.com/google/uuid"
"github.com/chaitin/MonkeyCode/backend/consts"
"github.com/chaitin/MonkeyCode/backend/domain"
)
// TaskNotifyHook 任务状态变更时发送通知
type TaskNotifyHook struct {
notify NotifyPublisher
logger *slog.Logger
}
// NewTaskNotifyHook 创建任务通知 Hook
func NewTaskNotifyHook(notify NotifyPublisher, logger *slog.Logger) *TaskNotifyHook {
return &TaskNotifyHook{
notify: notify,
logger: logger.With("hook", "task-notify-hook"),
}
}
func (h *TaskNotifyHook) Name() string { return "task-notify-hook" }
func (h *TaskNotifyHook) Priority() int { return 50 }
func (h *TaskNotifyHook) Async() bool { return true } // 异步执行,不阻塞状态转换
func (h *TaskNotifyHook) OnStateChange(ctx context.Context, taskID uuid.UUID, from, to consts.TaskStatus, metadata TaskMetadata) error {
var eventType consts.NotifyEventType
switch to {
case consts.TaskStatusPending:
eventType = consts.NotifyEventTaskCreated
default:
return nil
}
event := &domain.NotifyEvent{
EventType: eventType,
SubjectUserID: metadata.UserID,
RefID: taskID.String(),
Payload: domain.NotifyEventPayload{
TaskID: taskID.String(),
TaskStatus: string(to),
},
}
h.logger.InfoContext(ctx, "publishing notify event", "event", eventType, "task_id", taskID)
return h.notify.Publish(ctx, event)
}