定义执行历史和执行队列model, 以及基础的增删改查功能
This commit is contained in:
51
internal/infra/repository/execution_log_repository.go
Normal file
51
internal/infra/repository/execution_log_repository.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ExecutionLogRepository 定义了与执行日志交互的接口。
|
||||
// 这为服务层提供了一个清晰的契约,并允许在测试中轻松地进行模拟。
|
||||
type ExecutionLogRepository interface {
|
||||
CreatePlanExecutionLog(log *models.PlanExecutionLog) error
|
||||
UpdatePlanExecutionLog(log *models.PlanExecutionLog) error
|
||||
CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error
|
||||
UpdateTaskExecutionLog(log *models.TaskExecutionLog) error
|
||||
}
|
||||
|
||||
// executionLogRepository 是使用 GORM 的具体实现。
|
||||
type executionLogRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewExecutionLogRepository 创建一个新的执行日志仓库。
|
||||
// 它接收一个 GORM DB 实例作为依赖。
|
||||
func NewExecutionLogRepository(db *gorm.DB) ExecutionLogRepository {
|
||||
return &executionLogRepository{db: db}
|
||||
}
|
||||
|
||||
// CreatePlanExecutionLog 为一次计划执行创建一条新的日志条目。
|
||||
func (r *executionLogRepository) CreatePlanExecutionLog(log *models.PlanExecutionLog) error {
|
||||
return r.db.Create(log).Error
|
||||
}
|
||||
|
||||
// UpdatePlanExecutionLog 使用 Save 方法全量更新一个计划执行日志。
|
||||
// GORM 的 Save 会自动根据主键是否存在来决定是执行 UPDATE 还是 INSERT。
|
||||
// 在这里,我们期望传入的对象一定包含一个有效的 ID。
|
||||
func (r *executionLogRepository) UpdatePlanExecutionLog(log *models.PlanExecutionLog) error {
|
||||
return r.db.Save(log).Error
|
||||
}
|
||||
|
||||
// CreateTaskExecutionLogsInBatch 在一次数据库调用中创建多个任务执行日志条目。
|
||||
// 这是“预写日志”步骤的关键。
|
||||
func (r *executionLogRepository) CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error {
|
||||
// GORM 的 Create 传入一个切片指针会执行批量插入。
|
||||
return r.db.Create(&logs).Error
|
||||
}
|
||||
|
||||
// UpdateTaskExecutionLog 使用 Save 方法全量更新一个任务执行日志。
|
||||
// 这种方式代码更直观,上层服务可以直接修改模型对象后进行保存。
|
||||
func (r *executionLogRepository) UpdateTaskExecutionLog(log *models.TaskExecutionLog) error {
|
||||
return r.db.Save(log).Error
|
||||
}
|
||||
82
internal/infra/repository/pending_task_repository.go
Normal file
82
internal/infra/repository/pending_task_repository.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// PendingTaskRepository 定义了与待执行任务队列交互的接口。
|
||||
type PendingTaskRepository interface {
|
||||
CreatePendingTasksInBatch(tasks []*models.PendingTask) error
|
||||
ClaimNextDueTask() (*models.TaskExecutionLog, error)
|
||||
}
|
||||
|
||||
// pendingTaskRepository 是使用 GORM 的具体实现。
|
||||
type pendingTaskRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewPendingTaskRepository 创建一个新的待执行任务队列仓库。
|
||||
// 它接收一个 GORM DB 实例作为依赖。
|
||||
func NewPendingTaskRepository(db *gorm.DB) PendingTaskRepository {
|
||||
return &pendingTaskRepository{db: db}
|
||||
}
|
||||
|
||||
// CreatePendingTasksInBatch 在一次数据库调用中创建多个待执行任务条目。
|
||||
func (r *pendingTaskRepository) CreatePendingTasksInBatch(tasks []*models.PendingTask) error {
|
||||
return r.db.Create(&tasks).Error
|
||||
}
|
||||
|
||||
// ClaimNextDueTask 以原子方式认领下一个到期任务。
|
||||
// 它在一个事务中完成三件事:
|
||||
// 1. 查找并锁定一个到期的待办任务。
|
||||
// 2. 从待办队列中将其删除。
|
||||
// 3. 将其在执行日志表中的状态更新为 'running'。
|
||||
// 最后返回更新后的执行日志对象,作为执行的唯一凭证。
|
||||
func (r *pendingTaskRepository) ClaimNextDueTask() (*models.TaskExecutionLog, error) {
|
||||
var log models.TaskExecutionLog
|
||||
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
var pendingTask models.PendingTask
|
||||
|
||||
// 1. 查找并用 FOR UPDATE 锁定一个到期的待办任务
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("execute_at <= ?", time.Now()).
|
||||
Order("execute_at ASC").
|
||||
First(&pendingTask).Error; err != nil {
|
||||
return err // 没找到是正常情况,事务将回滚
|
||||
}
|
||||
|
||||
// 2. 在同一个事务中,立刻硬删除这个待办任务
|
||||
if err := tx.Unscoped().Delete(&pendingTask).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. 在同一个事务中,更新其在日志表中的孪生兄弟的状态
|
||||
updates := map[string]interface{}{
|
||||
"status": models.ExecutionStatusStarted,
|
||||
"started_at": time.Now(),
|
||||
}
|
||||
if err := tx.Model(&models.TaskExecutionLog{}).Where("id = ?", pendingTask.TaskExecutionLogID).Updates(updates).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 4. 获取更新后的完整日志对象,以返回给调用者
|
||||
if err := tx.First(&log, pendingTask.TaskExecutionLogID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
// 如果错误是 `gorm.ErrRecordNotFound`,它仅表示当前没有到期的任务。
|
||||
// 服务层应该优雅地处理这种情况,而不是将其视为需要立即处理的严重错误。
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &log, nil
|
||||
}
|
||||
Reference in New Issue
Block a user