初始化待执行队列

This commit is contained in:
2025-09-20 23:50:27 +08:00
parent 74e42de7aa
commit 769a0432c8
4 changed files with 169 additions and 1 deletions

View File

@@ -18,6 +18,12 @@ type ExecutionLogRepository interface {
FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error)
// UpdatePlanExecutionLogStatus 更新计划执行日志的状态
UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error
// UpdatePlanExecutionLogsStatusByIDs 批量更新计划执行日志的状态
UpdatePlanExecutionLogsStatusByIDs(logIDs []uint, status models.ExecutionStatus) error
// FindIncompletePlanExecutionLogs 查找所有未完成的计划执行日志
FindIncompletePlanExecutionLogs() ([]models.PlanExecutionLog, error)
}
// gormExecutionLogRepository 是使用 GORM 的具体实现。
@@ -93,3 +99,18 @@ func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(id uint) (*models.
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error {
return r.db.Model(&models.PlanExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
}
// UpdatePlanExecutionLogsStatusByIDs 批量更新计划执行日志的状态
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogsStatusByIDs(logIDs []uint, status models.ExecutionStatus) error {
if len(logIDs) == 0 {
return nil
}
return r.db.Model(&models.PlanExecutionLog{}).Where("id IN ?", logIDs).Update("status", status).Error
}
// FindIncompletePlanExecutionLogs 查找所有未完成的计划执行日志
func (r *gormExecutionLogRepository) FindIncompletePlanExecutionLogs() ([]models.PlanExecutionLog, error) {
var logs []models.PlanExecutionLog
err := r.db.Where("status = ?", models.ExecutionStatusStarted).Find(&logs).Error
return logs, err
}

View File

@@ -21,6 +21,9 @@ type PendingTaskRepository interface {
// UpdatePendingTaskExecuteAt 更新指定待执行任务的执行时间
UpdatePendingTaskExecuteAt(id uint, executeAt time.Time) error
// ClearAllPendingTasks 清空所有待执行任务
ClearAllPendingTasks() error
// ClaimNextAvailableTask 原子地认领下一个可用的任务。
// 它会同时返回被认领任务对应的日志对象,以及被删除的待办任务对象的内存副本。
ClaimNextAvailableTask(excludePlanIDs []uint) (*models.TaskExecutionLog, *models.PendingTask, error)
@@ -41,7 +44,8 @@ func NewGormPendingTaskRepository(db *gorm.DB) PendingTaskRepository {
func (r *gormPendingTaskRepository) FindAllPendingTasks() ([]models.PendingTask, error) {
var tasks []models.PendingTask
// 预加载 Task 以便后续访问 Task.PlanID
err := r.db.Preload("Task").Find(&tasks).Error
// 预加载 TaskExecutionLog 以便后续访问 PlanExecutionLogID
err := r.db.Preload("Task").Preload("TaskExecutionLog").Find(&tasks).Error
return tasks, err
}
@@ -82,6 +86,11 @@ func (r *gormPendingTaskRepository) UpdatePendingTaskExecuteAt(id uint, executeA
return r.db.Model(&models.PendingTask{}).Where("id = ?", id).Update("execute_at", executeAt).Error
}
// ClearAllPendingTasks 清空所有待执行任务
func (r *gormPendingTaskRepository) ClearAllPendingTasks() error {
return r.db.Where("1 = 1").Delete(&models.PendingTask{}).Error
}
// ClaimNextAvailableTask 以原子方式认领下一个可用的任务。
func (r *gormPendingTaskRepository) ClaimNextAvailableTask(excludePlanIDs []uint) (*models.TaskExecutionLog, *models.PendingTask, error) {
var log models.TaskExecutionLog

View File

@@ -51,6 +51,9 @@ type PlanRepository interface {
// CreatePlanAnalysisTask 创建一个 plan_analysis 类型的任务并返回它
CreatePlanAnalysisTask(plan *models.Plan) (*models.Task, error)
// FindPlansWithPendingTasks 查找所有正在执行的计划
FindPlansWithPendingTasks() ([]*models.Plan, error)
}
// gormPlanRepository 是 PlanRepository 的 GORM 实现
@@ -625,3 +628,18 @@ func (r *gormPlanRepository) CreatePlanAnalysisTask(plan *models.Plan) (*models.
})
return createdTask, err
}
// FindPlansWithPendingTasks 查找所有正在执行的计划
func (r *gormPlanRepository) FindPlansWithPendingTasks() ([]*models.Plan, error) {
var plans []*models.Plan
// 关联 pending_tasks, task_execution_logs, tasks 表来查找符合条件的计划
err := r.db.Table("plans").
Joins("JOIN tasks ON plans.id = tasks.plan_id").
Joins("JOIN task_execution_logs ON tasks.id = task_execution_logs.task_id").
Joins("JOIN pending_tasks ON task_execution_logs.id = pending_tasks.task_execution_log_id").
Group("plans.id"). // 避免重复,因为一个计划可能有多个待执行任务
Find(&plans).Error
return plans, err
}