任务调度器关于任务执行部分实现(没测没检查, 但应该实现完了)
This commit is contained in:
@@ -12,6 +12,7 @@ type ExecutionLogRepository interface {
|
||||
UpdatePlanExecutionLog(log *models.PlanExecutionLog) error
|
||||
CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error
|
||||
UpdateTaskExecutionLog(log *models.TaskExecutionLog) error
|
||||
FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error)
|
||||
}
|
||||
|
||||
// executionLogRepository 是使用 GORM 的具体实现。
|
||||
@@ -30,11 +31,11 @@ func (r *executionLogRepository) CreatePlanExecutionLog(log *models.PlanExecutio
|
||||
return r.db.Create(log).Error
|
||||
}
|
||||
|
||||
// UpdatePlanExecutionLog 使用 Save 方法全量更新一个计划执行日志。
|
||||
// GORM 的 Save 会自动根据主键是否存在来决定是执行 UPDATE 还是 INSERT。
|
||||
// UpdatePlanExecutionLog 使用 Updates 方法更新一个计划执行日志。
|
||||
// GORM 的 Updates 传入 struct 时,只会更新非零值字段。
|
||||
// 在这里,我们期望传入的对象一定包含一个有效的 ID。
|
||||
func (r *executionLogRepository) UpdatePlanExecutionLog(log *models.PlanExecutionLog) error {
|
||||
return r.db.Save(log).Error
|
||||
return r.db.Updates(log).Error
|
||||
}
|
||||
|
||||
// CreateTaskExecutionLogsInBatch 在一次数据库调用中创建多个任务执行日志条目。
|
||||
@@ -44,8 +45,21 @@ func (r *executionLogRepository) CreateTaskExecutionLogsInBatch(logs []*models.T
|
||||
return r.db.Create(&logs).Error
|
||||
}
|
||||
|
||||
// UpdateTaskExecutionLog 使用 Save 方法全量更新一个任务执行日志。
|
||||
// UpdateTaskExecutionLog 使用 Updates 方法更新一个任务执行日志。
|
||||
// GORM 的 Updates 传入 struct 时,只会更新非零值字段。
|
||||
// 这种方式代码更直观,上层服务可以直接修改模型对象后进行保存。
|
||||
func (r *executionLogRepository) UpdateTaskExecutionLog(log *models.TaskExecutionLog) error {
|
||||
return r.db.Save(log).Error
|
||||
return r.db.Updates(log).Error
|
||||
}
|
||||
|
||||
// FindTaskExecutionLogByID 根据 ID 查找单个任务执行日志。
|
||||
// 它会预加载关联的 Task 信息。
|
||||
func (r *executionLogRepository) FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error) {
|
||||
var log models.TaskExecutionLog
|
||||
// 使用 Preload("Task") 来确保关联的任务信息被一并加载
|
||||
err := r.db.Preload("Task").First(&log, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &log, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user