启动计划时清零计数器

This commit is contained in:
2025-09-23 22:18:59 +08:00
parent 9594d08e40
commit 1764ff5598
2 changed files with 38 additions and 5 deletions

View File

@@ -390,13 +390,32 @@ func (c *Controller) StartPlan(ctx *gin.Context) {
return return
} }
// 4. 更新计划状态为“已启动” // 4. 检查并重置执行计数器,然后更新计划状态为“已启动”
// 只有当计划是从非 Enabled 状态(如 Disabled, Stopeed, Failed启动时才需要重置计数器
if plan.Status != models.PlanStatusEnabled {
// 如果计划是从停止或失败状态重新启动且计数器不为0则重置执行计数
if plan.ExecuteCount > 0 {
if err := c.planRepo.UpdateExecuteCount(plan.ID, 0); err != nil {
c.logger.Errorf("重置计划 #%d 执行计数失败: %v", plan.ID, err)
controller.SendErrorResponse(ctx, controller.CodeInternalError, "重置计划执行计数失败")
return
}
c.logger.Infof("计划 #%d 的执行计数器已重置为 0。", plan.ID)
}
// 更新计划状态为“已启动”
if err := c.planRepo.UpdatePlanStatus(plan.ID, models.PlanStatusEnabled); err != nil { if err := c.planRepo.UpdatePlanStatus(plan.ID, models.PlanStatusEnabled); err != nil {
c.logger.Errorf("更新计划 #%d 状态为 '已启动' 失败: %v", plan.ID, err) c.logger.Errorf("更新计划 #%d 状态失败: %v", plan.ID, err)
controller.SendErrorResponse(ctx, controller.CodeInternalError, "更新计划状态失败") controller.SendErrorResponse(ctx, controller.CodeInternalError, "更新计划状态失败")
return return
} }
c.logger.Infof("已成功更新计划 #%d 的状态为 '已启动'", plan.ID) c.logger.Infof("已成功更新计划 #%d 的状态为 '已启动'", plan.ID)
} else {
// 如果计划已经处于 Enabled 状态,则无需更新
c.logger.Infof("计划 #%d 已处于启动状态,无需重复操作。", plan.ID)
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "计划已处于启动状态,无需重复操作")
return
}
// 5. 为计划创建或更新触发器 // 5. 为计划创建或更新触发器
if err := c.analysisPlanTaskManager.CreateOrUpdateTrigger(plan.ID); err != nil { if err := c.analysisPlanTaskManager.CreateOrUpdateTrigger(plan.ID); err != nil {

View File

@@ -36,6 +36,8 @@ type PlanRepository interface {
UpdatePlan(plan *models.Plan) error UpdatePlan(plan *models.Plan) error
// UpdatePlanStatus 更新指定计划的状态 // UpdatePlanStatus 更新指定计划的状态
UpdatePlanStatus(id uint, status models.PlanStatus) error UpdatePlanStatus(id uint, status models.PlanStatus) error
// UpdateExecuteCount 更新指定计划的执行计数
UpdateExecuteCount(id uint, count uint) error
// DeletePlan 根据ID删除计划同时删除其关联的任务非子任务或子计划关联 // DeletePlan 根据ID删除计划同时删除其关联的任务非子任务或子计划关联
DeletePlan(id uint) error DeletePlan(id uint) error
// FlattenPlanTasks 递归展开计划,返回按执行顺序排列的所有任务列表 // FlattenPlanTasks 递归展开计划,返回按执行顺序排列的所有任务列表
@@ -745,3 +747,15 @@ func (r *gormPlanRepository) UpdatePlanStateAfterExecution(planID uint, newCount
"status": newStatus, "status": newStatus,
}).Error }).Error
} }
// UpdateExecuteCount 更新指定计划的执行计数
func (r *gormPlanRepository) UpdateExecuteCount(id uint, count uint) error {
result := r.db.Model(&models.Plan{}).Where("id = ?", id).Update("execute_count", count)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}