feat: status constant define
This commit is contained in:
@@ -122,7 +122,7 @@ func (s *AgentService) RegisterByToken(token string, machineID string, ip string
|
||||
database.DB.Model(&existing).Updates(map[string]interface{}{
|
||||
"token": token,
|
||||
"ip": ip,
|
||||
"status": "online",
|
||||
"status": constant.AgentStatusOnline,
|
||||
"last_seen": now,
|
||||
})
|
||||
s.UseToken(agentToken.ID)
|
||||
@@ -138,7 +138,7 @@ func (s *AgentService) RegisterByToken(token string, machineID string, ip string
|
||||
Token: token,
|
||||
MachineID: machineID,
|
||||
IP: ip,
|
||||
Status: "online",
|
||||
Status: constant.AgentStatusOnline,
|
||||
LastSeen: &now,
|
||||
Enabled: true,
|
||||
}
|
||||
@@ -179,7 +179,7 @@ func (s *AgentService) Register(req *models.AgentRegisterRequest, ip string) (*m
|
||||
Version: req.Version,
|
||||
BuildTime: req.BuildTime,
|
||||
IP: ip,
|
||||
Status: "online",
|
||||
Status: constant.AgentStatusOnline,
|
||||
LastSeen: &now,
|
||||
Enabled: true,
|
||||
}
|
||||
@@ -288,7 +288,7 @@ func (s *AgentService) Heartbeat(token, ip, version, buildTime, hostname, osType
|
||||
|
||||
database.DB.Model(&models.Agent{}).Where("id = ?", agent.ID).Updates(updates)
|
||||
|
||||
agent.Status = "online"
|
||||
agent.Status = constant.AgentStatusOnline
|
||||
agent.LastSeen = &now
|
||||
agent.IP = ip
|
||||
agent.Version = version
|
||||
@@ -386,15 +386,15 @@ func (s *AgentService) UpdateTaskDuration(logID uint, duration int64) error {
|
||||
func (s *AgentService) UpdateOfflineAgents() {
|
||||
cutoff := time.Now().Add(-2 * time.Minute)
|
||||
database.DB.Model(&models.Agent{}).
|
||||
Where("status = ? AND last_seen < ?", "online", cutoff).
|
||||
Update("status", "offline")
|
||||
Where("status = ? AND last_seen < ?", constant.AgentStatusOnline, cutoff).
|
||||
Update("status", constant.AgentStatusOffline)
|
||||
}
|
||||
|
||||
// ResetAllAgentsToOffline 将所有 Agents 状态重置为离线(用于服务启动时)
|
||||
func (s *AgentService) ResetAllAgentsToOffline() {
|
||||
database.DB.Model(&models.Agent{}).
|
||||
Where("status = ?", "online").
|
||||
Update("status", "offline")
|
||||
Where("status = ?", constant.AgentStatusOnline).
|
||||
Update("status", constant.AgentStatusOffline)
|
||||
}
|
||||
|
||||
// GetLatestVersion 获取最新 Agent 版本
|
||||
|
||||
@@ -292,7 +292,7 @@ func (m *AgentWSManager) cleanupLoop() {
|
||||
conn.Close()
|
||||
delete(m.connections, agentID)
|
||||
// 更新数据库状态
|
||||
database.DB.Model(&models.Agent{}).Where("id = ?", agentID).Update("status", "offline")
|
||||
database.DB.Model(&models.Agent{}).Where("id = ?", agentID).Update("status", constant.AgentStatusOffline)
|
||||
logger.Infof("[AgentWS] Agent #%d 心跳超时,已断开", agentID)
|
||||
}
|
||||
}
|
||||
@@ -301,8 +301,8 @@ func (m *AgentWSManager) cleanupLoop() {
|
||||
// 有些 Agent 虽然没有连接,但数据库状态可能是 "online"
|
||||
cutoff := now.Add(-2 * time.Minute)
|
||||
database.DB.Model(&models.Agent{}).
|
||||
Where("status = ? AND last_seen < ?", "online", cutoff).
|
||||
Update("status", "offline")
|
||||
Where("status = ? AND last_seen < ?", constant.AgentStatusOnline, cutoff).
|
||||
Update("status", constant.AgentStatusOffline)
|
||||
|
||||
// 清理过期的限流记录(超过 10 分钟未活动)
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ func (h *ServerSchedulerHandler) OnTaskExecuting(req *executor.ExecutionRequest)
|
||||
goid, err := h.es.AddRunningGo(task.ID)
|
||||
if err != nil {
|
||||
// 并发限制,更新日志状态为失败
|
||||
taskLog.Status = "failed"
|
||||
taskLog.Status = constant.TaskStatusFailed
|
||||
taskLog.Output, _ = utils.CompressToBase64("任务并发数限制,拒绝执行")
|
||||
h.es.taskLogService.SaveTaskLog(taskLog)
|
||||
return nil, nil, fmt.Errorf("任务并发限制: %v", err)
|
||||
@@ -268,7 +268,7 @@ func (h *ServerSchedulerHandler) OnTaskFailed(req *executor.ExecutionRequest, er
|
||||
Command: req.Command,
|
||||
Output: output,
|
||||
Error: err.Error(),
|
||||
Status: "failed",
|
||||
Status: constant.TaskStatusFailed,
|
||||
Duration: 0,
|
||||
ExitCode: 1,
|
||||
StartTime: &now,
|
||||
@@ -330,7 +330,7 @@ func (es *ExecutorService) ExecuteDispatcher(ctx context.Context, req *executor.
|
||||
}
|
||||
|
||||
// 特殊处理仓库同步任务
|
||||
if task.Type == "repo" {
|
||||
if task.Type == constant.TaskTypeRepo {
|
||||
cmd, workDir := es.BuildRepoCommand(task)
|
||||
if cmd != "" {
|
||||
req.Command = cmd
|
||||
@@ -475,7 +475,7 @@ func (es *ExecutorService) ExecuteTask(taskID int) *executor.ExecutionResult {
|
||||
return &executor.ExecutionResult{
|
||||
TaskID: fmt.Sprintf("%d", task.ID),
|
||||
Success: true,
|
||||
Status: "queued",
|
||||
Status: constant.TaskStatusQueued,
|
||||
StartTime: time.Now(),
|
||||
}
|
||||
}
|
||||
@@ -487,7 +487,7 @@ func (es *ExecutorService) StopTaskExecution(logID uint) error {
|
||||
return fmt.Errorf("日志不存在")
|
||||
}
|
||||
|
||||
if taskLog.Status != "running" {
|
||||
if taskLog.Status != constant.TaskStatusRunning {
|
||||
return fmt.Errorf("任务已结束")
|
||||
}
|
||||
|
||||
@@ -692,7 +692,7 @@ func (es *ExecutorService) ExecuteRemoteForScheduler(task *models.Task, logID ui
|
||||
case <-time.After(time.Duration(timeout) * time.Minute):
|
||||
end := time.Now()
|
||||
return &executor.Result{
|
||||
Status: "failed",
|
||||
Status: constant.TaskStatusFailed,
|
||||
Error: "等待 Agent 结果超时",
|
||||
Duration: end.Sub(start).Milliseconds(),
|
||||
ExitCode: -1,
|
||||
|
||||
Reference in New Issue
Block a user