mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-22 19:39:31 +08:00
核心改动: - TaskUsecase: 移除 tasker 依赖,改用 lifecycle.Transition - CreateTaskReq 临时存储到 Redis (10 分钟过期) - InternalHostHandler: 移除 tasker 依赖 - TaskRepo: 实现 UpdateStatus 方法 完整流程: 1. TaskUsecase.Create() 存储 CreateTaskReq 到 Redis 2. VM 就绪时 TaskCreateHook 从 Redis 读取并创建 taskflow 任务 3. VMTaskHook 自动同步任务状态
103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
package lifecycle
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"github.com/chaitin/MonkeyCode/backend/consts"
|
|
"github.com/chaitin/MonkeyCode/backend/db"
|
|
"github.com/chaitin/MonkeyCode/backend/domain"
|
|
"github.com/chaitin/MonkeyCode/backend/pkg/taskflow"
|
|
)
|
|
|
|
// TaskHook 用于管理任务的整个状态
|
|
type TaskHook struct {
|
|
redis *redis.Client
|
|
taskflow taskflow.Clienter
|
|
repo domain.TaskRepo
|
|
logger *slog.Logger
|
|
taskLifecycle *Manager[uuid.UUID, consts.TaskStatus, TaskMetadata]
|
|
}
|
|
|
|
// NewTaskCreateHook 创建 TaskCreateHook
|
|
func NewTaskCreateHook(
|
|
redis *redis.Client,
|
|
taskflow taskflow.Clienter,
|
|
logger *slog.Logger,
|
|
taskLifecycle *Manager[uuid.UUID, consts.TaskStatus, TaskMetadata],
|
|
repo domain.TaskRepo,
|
|
) *TaskHook {
|
|
return &TaskHook{
|
|
redis: redis,
|
|
taskflow: taskflow,
|
|
logger: logger.With("hook", "task-hook"),
|
|
taskLifecycle: taskLifecycle,
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (h *TaskHook) Name() string { return "task-create-hook" }
|
|
func (h *TaskHook) Priority() int { return 80 }
|
|
func (h *TaskHook) Async() bool { return false }
|
|
|
|
func (h *TaskHook) OnStateChange(ctx context.Context, id uuid.UUID, from, to consts.TaskStatus, metadata TaskMetadata) error {
|
|
|
|
switch to {
|
|
case consts.TaskStatusProcessing:
|
|
return h.handleProcessing(ctx, id, metadata)
|
|
case consts.TaskStatusError:
|
|
return h.handleError(ctx, id, metadata.UserID)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h *TaskHook) withError(ctx context.Context, id, uid uuid.UUID, fn func() error) {
|
|
if err := fn(); err != nil {
|
|
if err := h.taskLifecycle.Transition(ctx, id, consts.TaskStatusError, TaskMetadata{
|
|
TaskID: id,
|
|
UserID: uid,
|
|
Error: err.Error(),
|
|
}); err != nil {
|
|
h.logger.With("error", err).ErrorContext(ctx, "failed to transition task to error status")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (h *TaskHook) handleError(ctx context.Context, id, uid uuid.UUID) error {
|
|
u := domain.User{ID: uid}
|
|
return h.repo.Update(ctx, &u, id, func(up *db.TaskUpdateOne) error {
|
|
up.SetStatus(consts.TaskStatusError)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (h *TaskHook) handleProcessing(ctx context.Context, id uuid.UUID, metadata TaskMetadata) error {
|
|
h.withError(ctx, id, metadata.UserID, func() error {
|
|
reqKey := fmt.Sprintf("task:create_req:%s", id.String())
|
|
val, err := h.redis.Get(ctx, reqKey).Result()
|
|
if err != nil {
|
|
h.logger.With("task_id", id, "error", err).ErrorContext(ctx, "failed to get CreateTaskReq from redis")
|
|
return fmt.Errorf("failed to get CreateTaskReq from Redis: %w", err)
|
|
}
|
|
|
|
defer h.redis.Del(ctx, reqKey)
|
|
|
|
var createReq taskflow.CreateTaskReq
|
|
if err := json.Unmarshal([]byte(val), &createReq); err != nil {
|
|
h.logger.With("task_id", id, "error", err).ErrorContext(ctx, "failed to unmarshal CreateTaskReq")
|
|
return fmt.Errorf("failed to unmarshal CreateTaskReq: %w", err)
|
|
}
|
|
|
|
h.logger.With("task_id", id).InfoContext(ctx, "creating taskflow task")
|
|
return h.taskflow.TaskManager().Create(ctx, createReq)
|
|
})
|
|
|
|
return nil
|
|
}
|