This commit is contained in:
2025-09-03 13:51:52 +08:00
parent c579e53ad1
commit c1f127e9b1
12 changed files with 645 additions and 23 deletions

View File

@@ -0,0 +1,168 @@
package repositories
import (
"context"
"fmt"
"time"
"tyapi-server/internal/domains/article/entities"
"tyapi-server/internal/domains/article/repositories"
"go.uber.org/zap"
"gorm.io/gorm"
)
// GormScheduledTaskRepository GORM定时任务仓储实现
type GormScheduledTaskRepository struct {
db *gorm.DB
logger *zap.Logger
}
// 编译时检查接口实现
var _ repositories.ScheduledTaskRepository = (*GormScheduledTaskRepository)(nil)
// NewGormScheduledTaskRepository 创建GORM定时任务仓储
func NewGormScheduledTaskRepository(db *gorm.DB, logger *zap.Logger) *GormScheduledTaskRepository {
return &GormScheduledTaskRepository{
db: db,
logger: logger,
}
}
// Create 创建定时任务记录
func (r *GormScheduledTaskRepository) Create(ctx context.Context, task entities.ScheduledTask) (entities.ScheduledTask, error) {
r.logger.Info("创建定时任务记录", zap.String("task_id", task.TaskID), zap.String("article_id", task.ArticleID))
err := r.db.WithContext(ctx).Create(&task).Error
if err != nil {
r.logger.Error("创建定时任务记录失败", zap.Error(err))
return task, err
}
return task, nil
}
// GetByTaskID 根据Asynq任务ID获取任务记录
func (r *GormScheduledTaskRepository) GetByTaskID(ctx context.Context, taskID string) (entities.ScheduledTask, error) {
var task entities.ScheduledTask
err := r.db.WithContext(ctx).
Preload("Article").
Where("task_id = ?", taskID).
First(&task).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return task, fmt.Errorf("定时任务不存在")
}
r.logger.Error("获取定时任务失败", zap.String("task_id", taskID), zap.Error(err))
return task, err
}
return task, nil
}
// GetByArticleID 根据文章ID获取任务记录
func (r *GormScheduledTaskRepository) GetByArticleID(ctx context.Context, articleID string) (entities.ScheduledTask, error) {
var task entities.ScheduledTask
err := r.db.WithContext(ctx).
Preload("Article").
Where("article_id = ? AND status IN (?)", articleID, []string{"pending", "running"}).
First(&task).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return task, fmt.Errorf("文章没有活动的定时任务")
}
r.logger.Error("获取文章定时任务失败", zap.String("article_id", articleID), zap.Error(err))
return task, err
}
return task, nil
}
// Update 更新任务记录
func (r *GormScheduledTaskRepository) Update(ctx context.Context, task entities.ScheduledTask) error {
r.logger.Info("更新定时任务记录", zap.String("task_id", task.TaskID), zap.String("status", string(task.Status)))
err := r.db.WithContext(ctx).Save(&task).Error
if err != nil {
r.logger.Error("更新定时任务记录失败", zap.String("task_id", task.TaskID), zap.Error(err))
return err
}
return nil
}
// Delete 删除任务记录
func (r *GormScheduledTaskRepository) Delete(ctx context.Context, taskID string) error {
r.logger.Info("删除定时任务记录", zap.String("task_id", taskID))
err := r.db.WithContext(ctx).Where("task_id = ?", taskID).Delete(&entities.ScheduledTask{}).Error
if err != nil {
r.logger.Error("删除定时任务记录失败", zap.String("task_id", taskID), zap.Error(err))
return err
}
return nil
}
// MarkAsCancelled 标记任务为已取消
func (r *GormScheduledTaskRepository) MarkAsCancelled(ctx context.Context, taskID string) error {
r.logger.Info("标记定时任务为已取消", zap.String("task_id", taskID))
result := r.db.WithContext(ctx).
Model(&entities.ScheduledTask{}).
Where("task_id = ? AND status IN (?)", taskID, []string{"pending", "running"}).
Updates(map[string]interface{}{
"status": entities.TaskStatusCancelled,
"completed_at": time.Now(),
})
if result.Error != nil {
r.logger.Error("标记定时任务为已取消失败", zap.String("task_id", taskID), zap.Error(result.Error))
return result.Error
}
if result.RowsAffected == 0 {
r.logger.Warn("没有找到需要取消的定时任务", zap.String("task_id", taskID))
}
return nil
}
// GetActiveTasks 获取活动状态的任务列表
func (r *GormScheduledTaskRepository) GetActiveTasks(ctx context.Context) ([]entities.ScheduledTask, error) {
var tasks []entities.ScheduledTask
err := r.db.WithContext(ctx).
Preload("Article").
Where("status IN (?)", []string{"pending", "running"}).
Order("scheduled_at ASC").
Find(&tasks).Error
if err != nil {
r.logger.Error("获取活动定时任务列表失败", zap.Error(err))
return nil, err
}
return tasks, nil
}
// GetExpiredTasks 获取过期的任务列表
func (r *GormScheduledTaskRepository) GetExpiredTasks(ctx context.Context) ([]entities.ScheduledTask, error) {
var tasks []entities.ScheduledTask
err := r.db.WithContext(ctx).
Preload("Article").
Where("status = ? AND scheduled_at < ?", entities.TaskStatusPending, time.Now()).
Order("scheduled_at ASC").
Find(&tasks).Error
if err != nil {
r.logger.Error("获取过期定时任务列表失败", zap.Error(err))
return nil, err
}
return tasks, nil
}