Skip to content

feat: 实现任务的休眠逻辑#435

Merged
yokowu merged 1 commit intomainfrom
feat-task-hibernate
Mar 24, 2026
Merged

feat: 实现任务的休眠逻辑#435
yokowu merged 1 commit intomainfrom
feat-task-hibernate

Conversation

@yokowu
Copy link
Collaborator

@yokowu yokowu commented Mar 24, 2026

No description provided.

Copy link

@monkeycode-ai monkeycode-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是 MonkeyCode AI 编程助手,你可以在 GitHub 仓库的 PR 中 at @MonkeyCode-AI 来呼唤我。

任务执行细节请参考: https://monkeycode-ai.com/tasks/public?id=51c23f00-0de7-418f-ade3-572fa8d85a34

代码审查结果

实现较完整但存在关键健壮性缺陷,建议修复错误处理与参数校验后再合并。

✨ 代码亮点

  • 将 VM 空闲治理拆分为休眠/通知/回收三段延迟队列,并加了 Redis 防抖键避免高频重复入队。
  • Loki Tail 从轮询切到 WebSocket,包含历史补齐、断线重连、心跳与去重,实时性和完整性明显提升。
  • 回收流程新增任务状态收敛与通知事件构建,补齐了 VM 生命周期结束后的业务闭环。
🚨 Critical ⚠️ Warning 💡 Suggestion
0 2 0

Comment on lines +935 to +937
if err != nil || info == nil || info.Status != taskflow.VirtualMachineStatusSleeping {
return nil
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

⚠️ 恢复休眠 VM 时吞掉 Info 查询错误

resumeVMIfSleeping 在查询 VM 状态失败时直接返回 nil,会把真实错误(网络异常、鉴权失败、taskflow 不可用)静默吞掉,导致后续流程继续执行并在更深层失败,增加排障难度,也可能让休眠 VM 未被恢复却继续执行端口/终端操作。

建议: 仅在“查询成功且非 sleeping”时返回 nil;查询失败应向上返回错误。

Suggested change
if err != nil || info == nil || info.Status != taskflow.VirtualMachineStatusSleeping {
return nil
}
if err != nil {
return fmt.Errorf("query vm info for resume: %w", err)
}
if info == nil || info.Status != taskflow.VirtualMachineStatusSleeping {
return nil
}

Comment on lines +433 to +440
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := h.hostUsecase.RefreshIdleTimers(ctx, req.VMID); err != nil {
h.logger.With("error", err).ErrorContext(ctx, "failed to refresh idel timers")
}
}()
return c.Success(nil)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

⚠️ VMActivity 未校验 vm_id,非法请求也返回成功

VMActivity 当前无任何参数校验,req.VMID 为空时仍异步执行并立即返回 c.Success(nil)。这会让调用方误以为活动上报成功,实际后台只会记录错误日志,造成状态不一致和问题隐蔽。

建议: 至少在入口校验 vm_id 非空,对无效请求返回错误;再执行异步刷新逻辑。

Suggested change
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := h.hostUsecase.RefreshIdleTimers(ctx, req.VMID); err != nil {
h.logger.With("error", err).ErrorContext(ctx, "failed to refresh idel timers")
}
}()
return c.Success(nil)
if req.VMID == "" {
return fmt.Errorf("vm_id is required")
}
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := h.hostUsecase.RefreshIdleTimers(ctx, req.VMID); err != nil {
h.logger.With("error", err).ErrorContext(ctx, "failed to refresh idel timers")
}
}()
return c.Success(nil)

@yokowu yokowu merged commit d80931c into main Mar 24, 2026
2 checks passed
@yokowu yokowu deleted the feat-task-hibernate branch March 24, 2026 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant