41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package queue
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
"tyc-server/app/main/api/internal/config"
|
|
"tyc-server/app/main/api/internal/svc"
|
|
|
|
"github.com/hibiken/asynq"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CleanQueryDataHandler struct {
|
|
config *config.Config
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewCleanQueryDataHandler(config *config.Config, svcCtx *svc.ServiceContext) *CleanQueryDataHandler {
|
|
return &CleanQueryDataHandler{
|
|
config: config,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task) error {
|
|
now := time.Now().Format("2006-01-02 15:04:05")
|
|
logx.Infof("%s - 开始执行查询数据清理任务", now)
|
|
|
|
// 计算15天前的时间
|
|
sevenDaysAgo := time.Now().AddDate(0, 0, -l.config.CleanTask.Days)
|
|
|
|
result, err := l.svcCtx.QueryModel.DeleteBefore(ctx, sevenDaysAgo)
|
|
if err != nil {
|
|
logx.Errorf("%s - 清理查询数据失败: %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
|
return err
|
|
}
|
|
|
|
logx.Infof("%s - 查询数据清理完成,共删除 %d 条记录", time.Now().Format("2006-01-02 15:04:05"), result)
|
|
return nil
|
|
}
|