修改infra.repository包
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -30,61 +33,63 @@ type TaskExecutionLogListOptions struct {
|
||||
// ExecutionLogRepository 定义了与执行日志交互的接口。
|
||||
type ExecutionLogRepository interface {
|
||||
// --- Existing methods ---
|
||||
UpdateTaskExecutionLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error
|
||||
UpdateTaskExecutionLogStatus(logID uint, status models.ExecutionStatus) error
|
||||
CreateTaskExecutionLog(log *models.TaskExecutionLog) error
|
||||
CreatePlanExecutionLog(log *models.PlanExecutionLog) error
|
||||
UpdatePlanExecutionLog(log *models.PlanExecutionLog) error
|
||||
CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error
|
||||
UpdateTaskExecutionLog(log *models.TaskExecutionLog) error
|
||||
FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error)
|
||||
UpdateTaskExecutionLogStatusByIDs(ctx context.Context, logIDs []uint, status models.ExecutionStatus) error
|
||||
UpdateTaskExecutionLogStatus(ctx context.Context, logID uint, status models.ExecutionStatus) error
|
||||
CreateTaskExecutionLog(ctx context.Context, log *models.TaskExecutionLog) error
|
||||
CreatePlanExecutionLog(ctx context.Context, log *models.PlanExecutionLog) error
|
||||
UpdatePlanExecutionLog(ctx context.Context, log *models.PlanExecutionLog) error
|
||||
CreateTaskExecutionLogsInBatch(ctx context.Context, logs []*models.TaskExecutionLog) error
|
||||
UpdateTaskExecutionLog(ctx context.Context, log *models.TaskExecutionLog) error
|
||||
FindTaskExecutionLogByID(ctx context.Context, id uint) (*models.TaskExecutionLog, error)
|
||||
// UpdatePlanExecutionLogStatus 更新计划执行日志的状态
|
||||
UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error
|
||||
UpdatePlanExecutionLogStatus(ctx context.Context, logID uint, status models.ExecutionStatus) error
|
||||
|
||||
// UpdatePlanExecutionLogsStatusByIDs 批量更新计划执行日志的状态
|
||||
UpdatePlanExecutionLogsStatusByIDs(logIDs []uint, status models.ExecutionStatus) error
|
||||
UpdatePlanExecutionLogsStatusByIDs(ctx context.Context, logIDs []uint, status models.ExecutionStatus) error
|
||||
|
||||
// FindIncompletePlanExecutionLogs 查找所有未完成的计划执行日志
|
||||
FindIncompletePlanExecutionLogs() ([]models.PlanExecutionLog, error)
|
||||
FindIncompletePlanExecutionLogs(ctx context.Context) ([]models.PlanExecutionLog, error)
|
||||
// FindInProgressPlanExecutionLogByPlanID 根据 PlanID 查找正在进行的计划执行日志
|
||||
FindInProgressPlanExecutionLogByPlanID(planID uint) (*models.PlanExecutionLog, error)
|
||||
FindInProgressPlanExecutionLogByPlanID(ctx context.Context, planID uint) (*models.PlanExecutionLog, error)
|
||||
// FindIncompleteTaskExecutionLogsByPlanLogID 根据计划日志ID查找所有未完成的任务日志
|
||||
FindIncompleteTaskExecutionLogsByPlanLogID(planLogID uint) ([]models.TaskExecutionLog, error)
|
||||
FindIncompleteTaskExecutionLogsByPlanLogID(ctx context.Context, planLogID uint) ([]models.TaskExecutionLog, error)
|
||||
|
||||
// FailAllIncompletePlanExecutionLogs 将所有状态为 ExecutionStatusStarted 和 ExecutionStatusWaiting 的计划状态都修改为 ExecutionStatusFailed
|
||||
FailAllIncompletePlanExecutionLogs() error
|
||||
FailAllIncompletePlanExecutionLogs(ctx context.Context) error
|
||||
// CancelAllIncompleteTaskExecutionLogs 将所有状态为 ExecutionStatusStarted 和 ExecutionStatusWaiting 的任务状态修改为 ExecutionStatusCancelled
|
||||
CancelAllIncompleteTaskExecutionLogs() error
|
||||
CancelAllIncompleteTaskExecutionLogs(ctx context.Context) error
|
||||
|
||||
// FindPlanExecutionLogByID 根据ID查找计划执行日志
|
||||
FindPlanExecutionLogByID(id uint) (*models.PlanExecutionLog, error)
|
||||
FindPlanExecutionLogByID(ctx context.Context, id uint) (*models.PlanExecutionLog, error)
|
||||
|
||||
// CountIncompleteTasksByPlanLogID 计算一个计划执行中未完成的任务数量
|
||||
CountIncompleteTasksByPlanLogID(planLogID uint) (int64, error)
|
||||
CountIncompleteTasksByPlanLogID(ctx context.Context, planLogID uint) (int64, error)
|
||||
|
||||
// FailPlanExecution 将指定的计划执行标记为失败
|
||||
FailPlanExecution(planLogID uint, errorMessage string) error
|
||||
FailPlanExecution(ctx context.Context, planLogID uint, errorMessage string) error
|
||||
|
||||
// CancelIncompleteTasksByPlanLogID 取消一个计划执行中的所有未完成任务
|
||||
CancelIncompleteTasksByPlanLogID(planLogID uint, reason string) error
|
||||
CancelIncompleteTasksByPlanLogID(ctx context.Context, planLogID uint, reason string) error
|
||||
|
||||
// --- New methods ---
|
||||
ListPlanExecutionLogs(opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error)
|
||||
ListTaskExecutionLogs(opts TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error)
|
||||
ListPlanExecutionLogs(ctx context.Context, opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error)
|
||||
ListTaskExecutionLogs(ctx context.Context, opts TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error)
|
||||
}
|
||||
|
||||
// gormExecutionLogRepository 是使用 GORM 的具体实现。
|
||||
type gormExecutionLogRepository struct {
|
||||
db *gorm.DB
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormExecutionLogRepository 创建一个新的执行日志仓库。
|
||||
func NewGormExecutionLogRepository(db *gorm.DB) ExecutionLogRepository {
|
||||
return &gormExecutionLogRepository{db: db}
|
||||
func NewGormExecutionLogRepository(ctx context.Context, db *gorm.DB) ExecutionLogRepository {
|
||||
return &gormExecutionLogRepository{ctx: ctx, db: db}
|
||||
}
|
||||
|
||||
// ListPlanExecutionLogs 实现了分页和过滤查询计划执行日志的功能
|
||||
func (r *gormExecutionLogRepository) ListPlanExecutionLogs(opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) {
|
||||
func (r *gormExecutionLogRepository) ListPlanExecutionLogs(ctx context.Context, opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListPlanExecutionLogs")
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
@@ -92,7 +97,7 @@ func (r *gormExecutionLogRepository) ListPlanExecutionLogs(opts PlanExecutionLog
|
||||
var results []models.PlanExecutionLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PlanExecutionLog{})
|
||||
query := r.db.WithContext(repoCtx).Model(&models.PlanExecutionLog{})
|
||||
|
||||
if opts.PlanID != nil {
|
||||
query = query.Where("plan_id = ?", *opts.PlanID)
|
||||
@@ -124,7 +129,8 @@ func (r *gormExecutionLogRepository) ListPlanExecutionLogs(opts PlanExecutionLog
|
||||
}
|
||||
|
||||
// ListTaskExecutionLogs 实现了分页和过滤查询任务执行日志的功能
|
||||
func (r *gormExecutionLogRepository) ListTaskExecutionLogs(opts TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error) {
|
||||
func (r *gormExecutionLogRepository) ListTaskExecutionLogs(ctx context.Context, opts TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListTaskExecutionLogs")
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
@@ -132,7 +138,7 @@ func (r *gormExecutionLogRepository) ListTaskExecutionLogs(opts TaskExecutionLog
|
||||
var results []models.TaskExecutionLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.TaskExecutionLog{})
|
||||
query := r.db.WithContext(repoCtx).Model(&models.TaskExecutionLog{})
|
||||
|
||||
if opts.PlanExecutionLogID != nil {
|
||||
query = query.Where("plan_execution_log_id = ?", *opts.PlanExecutionLogID)
|
||||
@@ -169,56 +175,64 @@ func (r *gormExecutionLogRepository) ListTaskExecutionLogs(opts TaskExecutionLog
|
||||
|
||||
// --- Existing method implementations ---
|
||||
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error {
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatusByIDs(ctx context.Context, logIDs []uint, status models.ExecutionStatus) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateTaskExecutionLogStatusByIDs")
|
||||
if len(logIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.Model(&models.TaskExecutionLog{}).Where("id IN ?", logIDs).Update("status", status).Error
|
||||
return r.db.WithContext(repoCtx).Model(&models.TaskExecutionLog{}).Where("id IN ?", logIDs).Update("status", status).Error
|
||||
}
|
||||
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatus(logID uint, status models.ExecutionStatus) error {
|
||||
return r.db.Model(&models.TaskExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatus(ctx context.Context, logID uint, status models.ExecutionStatus) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateTaskExecutionLogStatus")
|
||||
return r.db.WithContext(repoCtx).Model(&models.TaskExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
|
||||
}
|
||||
|
||||
func (r *gormExecutionLogRepository) CreateTaskExecutionLog(log *models.TaskExecutionLog) error {
|
||||
return r.db.Create(log).Error
|
||||
func (r *gormExecutionLogRepository) CreateTaskExecutionLog(ctx context.Context, log *models.TaskExecutionLog) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateTaskExecutionLog")
|
||||
return r.db.WithContext(repoCtx).Create(log).Error
|
||||
}
|
||||
|
||||
// CreatePlanExecutionLog 为一次计划执行创建一条新的日志条目。
|
||||
func (r *gormExecutionLogRepository) CreatePlanExecutionLog(log *models.PlanExecutionLog) error {
|
||||
return r.db.Create(log).Error
|
||||
func (r *gormExecutionLogRepository) CreatePlanExecutionLog(ctx context.Context, log *models.PlanExecutionLog) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreatePlanExecutionLog")
|
||||
return r.db.WithContext(repoCtx).Create(log).Error
|
||||
}
|
||||
|
||||
// UpdatePlanExecutionLog 使用 Updates 方法更新一个计划执行日志。
|
||||
// GORM 的 Updates 传入 struct 时,只会更新非零值字段。
|
||||
// 在这里,我们期望传入的对象一定包含一个有效的 ID。
|
||||
func (r *gormExecutionLogRepository) UpdatePlanExecutionLog(log *models.PlanExecutionLog) error {
|
||||
return r.db.Updates(log).Error
|
||||
func (r *gormExecutionLogRepository) UpdatePlanExecutionLog(ctx context.Context, log *models.PlanExecutionLog) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePlanExecutionLog")
|
||||
return r.db.WithContext(repoCtx).Updates(log).Error
|
||||
}
|
||||
|
||||
// CreateTaskExecutionLogsInBatch 在一次数据库调用中创建多个任务执行日志条目。
|
||||
// 这是“预写日志”步骤的关键。
|
||||
func (r *gormExecutionLogRepository) CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error {
|
||||
if len(logs) == 0 {
|
||||
func (r *gormExecutionLogRepository) CreateTaskExecutionLogsInBatch(ctx context.Context, executionLogs []*models.TaskExecutionLog) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateTaskExecutionLogsInBatch")
|
||||
if len(executionLogs) == 0 {
|
||||
return nil
|
||||
}
|
||||
// GORM 的 CreateTx 传入一个切片指针会执行批量插入。
|
||||
return r.db.Create(&logs).Error
|
||||
return r.db.WithContext(repoCtx).Create(&executionLogs).Error
|
||||
}
|
||||
|
||||
// UpdateTaskExecutionLog 使用 Updates 方法更新一个任务执行日志。
|
||||
// GORM 的 Updates 传入 struct 时,只会更新非零值字段。
|
||||
// 这种方式代码更直观,上层服务可以直接修改模型对象后进行保存。
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLog(log *models.TaskExecutionLog) error {
|
||||
return r.db.Updates(log).Error
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLog(ctx context.Context, log *models.TaskExecutionLog) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateTaskExecutionLog")
|
||||
return r.db.WithContext(repoCtx).Updates(log).Error
|
||||
}
|
||||
|
||||
// FindTaskExecutionLogByID 根据 ID 查找单个任务执行日志。
|
||||
// 它会预加载关联的 Task 信息。
|
||||
func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error) {
|
||||
func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(ctx context.Context, id uint) (*models.TaskExecutionLog, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindTaskExecutionLogByID")
|
||||
var log models.TaskExecutionLog
|
||||
// 使用 Preload("Task") 来确保关联的任务信息被一并加载
|
||||
err := r.db.Preload("Task").First(&log, id).Error
|
||||
err := r.db.WithContext(repoCtx).Preload("Task").First(&log, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -226,29 +240,33 @@ func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(id uint) (*models.
|
||||
}
|
||||
|
||||
// UpdatePlanExecutionLogStatus 更新计划执行日志的状态
|
||||
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error {
|
||||
return r.db.Model(&models.PlanExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
|
||||
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogStatus(ctx context.Context, logID uint, status models.ExecutionStatus) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePlanExecutionLogStatus")
|
||||
return r.db.WithContext(repoCtx).Model(&models.PlanExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
|
||||
}
|
||||
|
||||
// UpdatePlanExecutionLogsStatusByIDs 批量更新计划执行日志的状态
|
||||
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogsStatusByIDs(logIDs []uint, status models.ExecutionStatus) error {
|
||||
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogsStatusByIDs(ctx context.Context, logIDs []uint, status models.ExecutionStatus) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePlanExecutionLogsStatusByIDs")
|
||||
if len(logIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.Model(&models.PlanExecutionLog{}).Where("id IN ?", logIDs).Update("status", status).Error
|
||||
return r.db.WithContext(repoCtx).Model(&models.PlanExecutionLog{}).Where("id IN ?", logIDs).Update("status", status).Error
|
||||
}
|
||||
|
||||
// FindIncompletePlanExecutionLogs 查找所有未完成的计划执行日志
|
||||
func (r *gormExecutionLogRepository) FindIncompletePlanExecutionLogs() ([]models.PlanExecutionLog, error) {
|
||||
func (r *gormExecutionLogRepository) FindIncompletePlanExecutionLogs(ctx context.Context) ([]models.PlanExecutionLog, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindIncompletePlanExecutionLogs")
|
||||
var logs []models.PlanExecutionLog
|
||||
err := r.db.Where("status = ? OR status = ?", models.ExecutionStatusStarted, models.ExecutionStatusWaiting).Find(&logs).Error
|
||||
err := r.db.WithContext(repoCtx).Where("status = ? OR status = ?", models.ExecutionStatusStarted, models.ExecutionStatusWaiting).Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
|
||||
// FindInProgressPlanExecutionLogByPlanID 根据 PlanID 查找正在进行的计划执行日志
|
||||
func (r *gormExecutionLogRepository) FindInProgressPlanExecutionLogByPlanID(planID uint) (*models.PlanExecutionLog, error) {
|
||||
func (r *gormExecutionLogRepository) FindInProgressPlanExecutionLogByPlanID(ctx context.Context, planID uint) (*models.PlanExecutionLog, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindInProgressPlanExecutionLogByPlanID")
|
||||
var log models.PlanExecutionLog
|
||||
err := r.db.Where("plan_id = ? AND status = ?", planID, models.ExecutionStatusStarted).First(&log).Error
|
||||
err := r.db.WithContext(repoCtx).Where("plan_id = ? AND status = ?", planID, models.ExecutionStatusStarted).First(&log).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 未找到不是一个需要上报的错误,代表计划当前没有在运行
|
||||
@@ -261,31 +279,35 @@ func (r *gormExecutionLogRepository) FindInProgressPlanExecutionLogByPlanID(plan
|
||||
}
|
||||
|
||||
// FindIncompleteTaskExecutionLogsByPlanLogID 根据计划日志ID查找所有未完成的任务日志
|
||||
func (r *gormExecutionLogRepository) FindIncompleteTaskExecutionLogsByPlanLogID(planLogID uint) ([]models.TaskExecutionLog, error) {
|
||||
func (r *gormExecutionLogRepository) FindIncompleteTaskExecutionLogsByPlanLogID(ctx context.Context, planLogID uint) ([]models.TaskExecutionLog, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindIncompleteTaskExecutionLogsByPlanLogID")
|
||||
var logs []models.TaskExecutionLog
|
||||
err := r.db.Where("plan_execution_log_id = ? AND (status = ? OR status = ?)",
|
||||
err := r.db.WithContext(repoCtx).Where("plan_execution_log_id = ? AND (status = ? OR status = ?)",
|
||||
planLogID, models.ExecutionStatusWaiting, models.ExecutionStatusStarted).Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
|
||||
// FailAllIncompletePlanExecutionLogs 将所有状态为 ExecutionStatusStarted 和 ExecutionStatusWaiting 的计划状态都修改为 ExecutionStatusFailed
|
||||
func (r *gormExecutionLogRepository) FailAllIncompletePlanExecutionLogs() error {
|
||||
return r.db.Model(&models.PlanExecutionLog{}).
|
||||
func (r *gormExecutionLogRepository) FailAllIncompletePlanExecutionLogs(ctx context.Context) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FailAllIncompletePlanExecutionLogs")
|
||||
return r.db.WithContext(repoCtx).Model(&models.PlanExecutionLog{}).
|
||||
Where("status IN (?, ?)", models.ExecutionStatusStarted, models.ExecutionStatusWaiting).
|
||||
Updates(map[string]interface{}{"status": models.ExecutionStatusFailed, "ended_at": time.Now(), "error": "系统中断"}).Error
|
||||
}
|
||||
|
||||
// CancelAllIncompleteTaskExecutionLogs 将所有状态为 ExecutionStatusStarted 和 ExecutionStatusWaiting 的任务状态修改为 ExecutionStatusCancelled
|
||||
func (r *gormExecutionLogRepository) CancelAllIncompleteTaskExecutionLogs() error {
|
||||
return r.db.Model(&models.TaskExecutionLog{}).
|
||||
func (r *gormExecutionLogRepository) CancelAllIncompleteTaskExecutionLogs(ctx context.Context) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CancelAllIncompleteTaskExecutionLogs")
|
||||
return r.db.WithContext(repoCtx).Model(&models.TaskExecutionLog{}).
|
||||
Where("status IN (?, ?)", models.ExecutionStatusStarted, models.ExecutionStatusWaiting).
|
||||
Updates(map[string]interface{}{"status": models.ExecutionStatusCancelled, "ended_at": time.Now(), "output": "系统中断"}).Error
|
||||
}
|
||||
|
||||
// FindPlanExecutionLogByID 根据ID查找计划执行日志
|
||||
func (r *gormExecutionLogRepository) FindPlanExecutionLogByID(id uint) (*models.PlanExecutionLog, error) {
|
||||
func (r *gormExecutionLogRepository) FindPlanExecutionLogByID(ctx context.Context, id uint) (*models.PlanExecutionLog, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindPlanExecutionLogByID")
|
||||
var log models.PlanExecutionLog
|
||||
err := r.db.First(&log, id).Error
|
||||
err := r.db.WithContext(repoCtx).First(&log, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -293,9 +315,10 @@ func (r *gormExecutionLogRepository) FindPlanExecutionLogByID(id uint) (*models.
|
||||
}
|
||||
|
||||
// CountIncompleteTasksByPlanLogID 计算一个计划执行中未完成的任务数量
|
||||
func (r *gormExecutionLogRepository) CountIncompleteTasksByPlanLogID(planLogID uint) (int64, error) {
|
||||
func (r *gormExecutionLogRepository) CountIncompleteTasksByPlanLogID(ctx context.Context, planLogID uint) (int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CountIncompleteTasksByPlanLogID")
|
||||
var count int64
|
||||
err := r.db.Model(&models.TaskExecutionLog{}).
|
||||
err := r.db.WithContext(repoCtx).Model(&models.TaskExecutionLog{}).
|
||||
Where("plan_execution_log_id = ? AND status IN (?, ?)",
|
||||
planLogID, models.ExecutionStatusWaiting, models.ExecutionStatusStarted).
|
||||
Count(&count).Error
|
||||
@@ -303,8 +326,9 @@ func (r *gormExecutionLogRepository) CountIncompleteTasksByPlanLogID(planLogID u
|
||||
}
|
||||
|
||||
// FailPlanExecution 将指定的计划执行标记为失败
|
||||
func (r *gormExecutionLogRepository) FailPlanExecution(planLogID uint, errorMessage string) error {
|
||||
return r.db.Model(&models.PlanExecutionLog{}).
|
||||
func (r *gormExecutionLogRepository) FailPlanExecution(ctx context.Context, planLogID uint, errorMessage string) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FailPlanExecution")
|
||||
return r.db.WithContext(repoCtx).Model(&models.PlanExecutionLog{}).
|
||||
Where("id = ?", planLogID).
|
||||
Updates(map[string]interface{}{
|
||||
"status": models.ExecutionStatusFailed,
|
||||
@@ -314,8 +338,9 @@ func (r *gormExecutionLogRepository) FailPlanExecution(planLogID uint, errorMess
|
||||
}
|
||||
|
||||
// CancelIncompleteTasksByPlanLogID 取消一个计划执行中的所有未完成任务
|
||||
func (r *gormExecutionLogRepository) CancelIncompleteTasksByPlanLogID(planLogID uint, reason string) error {
|
||||
return r.db.Model(&models.TaskExecutionLog{}).
|
||||
func (r *gormExecutionLogRepository) CancelIncompleteTasksByPlanLogID(ctx context.Context, planLogID uint, reason string) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CancelIncompleteTasksByPlanLogID")
|
||||
return r.db.WithContext(repoCtx).Model(&models.TaskExecutionLog{}).
|
||||
Where("plan_execution_log_id = ? AND status IN (?, ?)",
|
||||
planLogID, models.ExecutionStatusWaiting, models.ExecutionStatusStarted).
|
||||
Updates(map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user