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

60 lines
1.5 KiB
Go

package lifecycle
import (
"context"
"log/slog"
"github.com/chaitin/MonkeyCode/backend/consts"
"github.com/chaitin/MonkeyCode/backend/domain"
)
// NotifyPublisher 通知发布接口
type NotifyPublisher interface {
Publish(ctx context.Context, event *domain.NotifyEvent) error
}
// VMNotifyHook VM 状态变更时发送通知
type VMNotifyHook struct {
notify NotifyPublisher
logger *slog.Logger
}
// NewVMNotifyHook 创建 VM 通知 Hook
func NewVMNotifyHook(notify NotifyPublisher, logger *slog.Logger) *VMNotifyHook {
return &VMNotifyHook{
notify: notify,
logger: logger.With("hook", "vm-notify-hook"),
}
}
func (h *VMNotifyHook) Name() string { return "vm-notify-hook" }
func (h *VMNotifyHook) Priority() int { return 50 }
func (h *VMNotifyHook) Async() bool { return true } // 异步执行,不阻塞状态转换
func (h *VMNotifyHook) OnStateChange(ctx context.Context, vmID string, from, to VMState, metadata VMMetadata) error {
var eventType consts.NotifyEventType
switch to {
case VMStateRunning:
eventType = consts.NotifyEventVMReady
case VMStateFailed:
eventType = consts.NotifyEventVMFailed
case VMStateSucceeded:
eventType = consts.NotifyEventVMCompleted
default:
return nil
}
event := &domain.NotifyEvent{
EventType: eventType,
SubjectUserID: metadata.UserID,
RefID: vmID,
Payload: domain.NotifyEventPayload{
VMID: vmID,
VMStatus: string(to),
},
}
h.logger.InfoContext(ctx, "publishing notify event", "event", eventType, "vm_id", vmID)
return h.notify.Publish(ctx, event)
}