v1.1
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/globalkey"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
@@ -115,6 +116,7 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
|
||||
// 创建清理日志记录(只创建一次)
|
||||
cleanupLog := &model.QueryCleanupLog{
|
||||
Id: uuid.New().String(),
|
||||
CleanupTime: now,
|
||||
CleanupBefore: cleanupBefore,
|
||||
Status: 1,
|
||||
@@ -122,13 +124,11 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
}
|
||||
|
||||
// 先创建清理日志记录
|
||||
var cleanupLogId int64
|
||||
err = l.svcCtx.QueryCleanupLogModel.Trans(taskCtx, func(logCtx context.Context, logSession sqlx.Session) error {
|
||||
cleanupLogInsertResult, insertErr := l.svcCtx.QueryCleanupLogModel.Insert(logCtx, logSession, cleanupLog)
|
||||
_, insertErr := l.svcCtx.QueryCleanupLogModel.Insert(logCtx, logSession, cleanupLog)
|
||||
if insertErr != nil {
|
||||
return insertErr
|
||||
}
|
||||
cleanupLogId, insertErr = cleanupLogInsertResult.LastInsertId()
|
||||
return insertErr
|
||||
})
|
||||
if err != nil {
|
||||
@@ -136,11 +136,11 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
return err
|
||||
}
|
||||
|
||||
logx.Infof("创建清理日志记录成功,日志ID: %d", cleanupLogId)
|
||||
logx.Infof("创建清理日志记录成功,日志ID: %s", cleanupLog.Id)
|
||||
|
||||
// 分批处理,每个批次使用独立事务
|
||||
batchCount := 0
|
||||
lastProcessedId := int64(0)
|
||||
lastProcessedId := ""
|
||||
|
||||
for {
|
||||
// 检查是否被取消(优雅关闭支持)
|
||||
@@ -148,7 +148,7 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
case <-taskCtx.Done():
|
||||
logx.Infof("清理任务被取消,已处理 %d 批次,共删除 %d 条记录", batchCount, cleanupLog.AffectedRows)
|
||||
// 更新清理日志状态
|
||||
l.updateCleanupLogStatus(taskCtx, cleanupLogId, cleanupLog, fmt.Errorf("任务被取消"))
|
||||
l.updateCleanupLogStatus(taskCtx, cleanupLog.Id, cleanupLog, fmt.Errorf("任务被取消"))
|
||||
return taskCtx.Err()
|
||||
default:
|
||||
// 继续处理
|
||||
@@ -165,7 +165,7 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
Limit(uint64(batchSize))
|
||||
|
||||
// 如果已处理过,从上次处理的ID之后继续
|
||||
if lastProcessedId > 0 {
|
||||
if lastProcessedId != "" {
|
||||
builder = builder.Where("id > ?", lastProcessedId)
|
||||
}
|
||||
|
||||
@@ -191,7 +191,8 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
// 3. 保存清理明细
|
||||
for _, query := range batchQueries {
|
||||
detail := &model.QueryCleanupDetail{
|
||||
CleanupLogId: cleanupLogId,
|
||||
Id: uuid.New().String(),
|
||||
CleanupLogId: cleanupLog.Id,
|
||||
QueryId: query.Id,
|
||||
OrderId: query.OrderId,
|
||||
UserId: query.UserId,
|
||||
@@ -214,7 +215,7 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
if batchErr != nil {
|
||||
// 批次失败,更新清理日志状态
|
||||
logx.Errorf("批次处理失败(批次 %d): %v", batchCount+1, batchErr)
|
||||
l.updateCleanupLogStatus(taskCtx, cleanupLogId, cleanupLog, batchErr)
|
||||
l.updateCleanupLogStatus(taskCtx, cleanupLog.Id, cleanupLog, batchErr)
|
||||
return batchErr
|
||||
}
|
||||
|
||||
@@ -238,7 +239,7 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
}
|
||||
|
||||
// 更新清理日志状态为成功
|
||||
l.updateCleanupLogStatus(taskCtx, cleanupLogId, cleanupLog, nil)
|
||||
l.updateCleanupLogStatus(taskCtx, cleanupLog.Id, cleanupLog, nil)
|
||||
|
||||
duration := time.Since(startTime)
|
||||
logx.Infof("%s - 查询数据清理完成,共处理 %d 批次,删除 %d 条记录,耗时 %v",
|
||||
@@ -247,10 +248,10 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
|
||||
}
|
||||
|
||||
// updateCleanupLogStatus 更新清理日志状态
|
||||
func (l *CleanQueryDataHandler) updateCleanupLogStatus(ctx context.Context, logId int64, cleanupLog *model.QueryCleanupLog, err error) {
|
||||
func (l *CleanQueryDataHandler) updateCleanupLogStatus(ctx context.Context, logId string, cleanupLog *model.QueryCleanupLog, err error) {
|
||||
err = l.svcCtx.QueryCleanupLogModel.Trans(ctx, func(updateCtx context.Context, updateSession sqlx.Session) error {
|
||||
// 查询当前日志记录
|
||||
currentLog, findErr := l.svcCtx.QueryCleanupLogModel.FindOne(updateCtx, logId)
|
||||
currentLog, findErr := l.svcCtx.QueryCleanupLogModel.FindOne(updateCtx, cleanupLog.Id)
|
||||
if findErr != nil {
|
||||
return findErr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user