重构AnalysisPlanTaskManager
This commit is contained in:
@@ -266,12 +266,26 @@ func (s *Scheduler) processTask(claimedLog *models.TaskExecutionLog) {
|
|||||||
|
|
||||||
// 更新计划的执行计数器
|
// 更新计划的执行计数器
|
||||||
plan.ExecuteCount++
|
plan.ExecuteCount++
|
||||||
|
|
||||||
|
// 如果是自动计划且达到执行次数上限,则更新计划状态为已停止
|
||||||
|
if (plan.ExecutionType == models.PlanExecutionTypeAutomatic && plan.ExecuteNum > 0 && plan.ExecuteCount >= plan.ExecuteNum) || plan.ExecutionType == models.PlanExecutionTypeManual {
|
||||||
|
plan.Status = models.PlanStatusStopeed
|
||||||
|
s.logger.Infof("计划 %d (自动执行) 已达到最大执行次数 %d,状态更新为 '执行完毕'。", planID, plan.ExecuteNum)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存更新后的计划状态和执行计数
|
||||||
if err := s.planRepo.UpdatePlan(plan); err != nil { // UpdatePlan 可以更新整个 Plan 对象
|
if err := s.planRepo.UpdatePlan(plan); err != nil { // UpdatePlan 可以更新整个 Plan 对象
|
||||||
s.logger.Errorf("更新计划 %d 的执行计数失败: %v", planID, err)
|
s.logger.Errorf("更新计划 %d 的执行计数和状态失败: %v", planID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用共享的 Manager 来处理触发器更新逻辑
|
// 更新计划执行日志状态为完成
|
||||||
|
if err := s.executionLogRepo.UpdatePlanExecutionLogStatus(claimedLog.PlanExecutionLogID, models.ExecutionStatusCompleted); err != nil {
|
||||||
|
s.logger.Errorf("更新计划执行日志 %d 状态为 '完成' 失败: %v", claimedLog.PlanExecutionLogID, err)
|
||||||
|
// 这是一个非阻塞性错误,不中断后续流程
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用共享的 Manager 来处理触发器更新逻辑 (Manager 会根据最新的 Plan 状态决定是否创建新触发器)
|
||||||
if err := s.analysisPlanTaskManager.CreateOrUpdateTrigger(planID); err != nil {
|
if err := s.analysisPlanTaskManager.CreateOrUpdateTrigger(planID); err != nil {
|
||||||
s.logger.Errorf("为计划 %d 创建/更新触发器失败: %v", planID, err)
|
s.logger.Errorf("为计划 %d 创建/更新触发器失败: %v", planID, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ type ExecutionLogRepository interface {
|
|||||||
CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error
|
CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error
|
||||||
UpdateTaskExecutionLog(log *models.TaskExecutionLog) error
|
UpdateTaskExecutionLog(log *models.TaskExecutionLog) error
|
||||||
FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error)
|
FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error)
|
||||||
|
// UpdatePlanExecutionLogStatus 更新计划执行日志的状态
|
||||||
|
UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormExecutionLogRepository 是使用 GORM 的具体实现。
|
// gormExecutionLogRepository 是使用 GORM 的具体实现。
|
||||||
@@ -86,3 +88,8 @@ func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(id uint) (*models.
|
|||||||
}
|
}
|
||||||
return &log, nil
|
return &log, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdatePlanExecutionLogStatus 更新计划执行日志的状态
|
||||||
|
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error {
|
||||||
|
return r.db.Model(&models.PlanExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user