2025-01-10 00:09:25 +08:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-04-11 13:10:17 +08:00
|
|
|
"qnc-server/app/user/cmd/api/internal/svc"
|
2025-04-08 21:53:45 +08:00
|
|
|
|
|
|
|
"github.com/hibiken/asynq"
|
2025-01-10 00:09:25 +08:00
|
|
|
)
|
|
|
|
|
2025-04-08 21:53:45 +08:00
|
|
|
// TASKTIME 定义为每天凌晨3点执行
|
|
|
|
const TASKTIME = "0 3 * * *"
|
2025-01-10 00:09:25 +08:00
|
|
|
|
|
|
|
type CleanQueryDataHandler struct {
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCleanQueryDataHandler(svcCtx *svc.ServiceContext) *CleanQueryDataHandler {
|
|
|
|
return &CleanQueryDataHandler{
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task) error {
|
2025-04-11 13:10:17 +08:00
|
|
|
// now := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
// logx.Infof("%s - 开始执行查询数据清理任务", now)
|
2025-04-08 21:53:45 +08:00
|
|
|
|
2025-04-11 13:10:17 +08:00
|
|
|
// // 计算3天前的时间
|
|
|
|
// threeDaysAgo := time.Now().AddDate(0, 0, -3)
|
2025-04-08 21:53:45 +08:00
|
|
|
|
2025-04-11 13:10:17 +08:00
|
|
|
// // 调用QueryModel删除3天前的数据
|
|
|
|
// result, err := l.svcCtx.QueryModel.DeleteBefore(ctx, threeDaysAgo)
|
|
|
|
// if err != nil {
|
|
|
|
// logx.Errorf("%s - 清理查询数据失败: %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
|
|
|
// return err
|
|
|
|
// }
|
2025-04-08 21:53:45 +08:00
|
|
|
|
2025-04-11 13:10:17 +08:00
|
|
|
// logx.Infof("%s - 查询数据清理完成,共删除 %d 条记录", time.Now().Format("2006-01-02 15:04:05"), result)
|
2025-01-10 00:09:25 +08:00
|
|
|
return nil
|
|
|
|
}
|