feat: add task cancell

This commit is contained in:
engigu
2026-02-10 16:47:18 +08:00
parent ea4f7be11e
commit aef124defc
10 changed files with 335 additions and 111 deletions
+20
View File
@@ -33,6 +33,7 @@ const (
WSTypeTaskLog = "task_log"
WSTypeExecute = "execute"
WSTypeTaskHeartbeat = "task_heartbeat"
WSTypeStop = "stop"
)
type WSMessage struct {
@@ -370,6 +371,8 @@ func (a *Agent) handleWSMessage(msg *WSMessage) {
a.fetchTasks()
case WSTypeExecute:
a.handleExecute(msg.Data)
case WSTypeStop:
a.handleStop(msg.Data)
}
}
@@ -505,6 +508,23 @@ func (a *Agent) handleExecute(data json.RawMessage) {
a.scheduler.EnqueueOrExecute(execReq)
}
func (a *Agent) handleStop(data json.RawMessage) {
var req struct {
LogID uint `json:"log_id"`
}
if err := json.Unmarshal(data, &req); err != nil {
logger.Errorf("解析停止请求失败: %v", err)
return
}
logger.Infof("[Agent] 收到停止指令 LogID: %d", req.LogID)
if a.scheduler.StopLog(req.LogID) {
logger.Infof("[Agent] 任务执行 #%d 已成功停止", req.LogID)
} else {
logger.Warnf("[Agent] 任务执行 #%d 停止失败(可能已完成或不在运行队列中)", req.LogID)
}
}
// RealTimeLogWriter 实时日志写入器,通过 WebSocket 发送日志
type RealTimeLogWriter struct {
agent *Agent