no message

This commit is contained in:
chejiulong 2023-08-07 09:51:31 +08:00
parent ab97c798ad
commit 1a3e8f5d8d

52
main.go
View File

@ -19,7 +19,6 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"reflect"
"regexp" "regexp"
"sort" "sort"
"strconv" "strconv"
@ -111,19 +110,19 @@ func deleteOldData(model interface{}, daysAgo int) error {
} }
threshold := time.Now().AddDate(0, 0, -daysAgo) threshold := time.Now().AddDate(0, 0, -daysAgo)
for { tx := db.Begin()
tx := db.Begin() applogger.Info("开始事务")
applogger.Info("开始事务") defer func() {
defer func() { if r := recover(); r != nil {
if r := recover(); r != nil { applogger.Error("异常,回滚事务: ", r)
applogger.Error("异常,回滚事务: ", r) tx.Rollback()
tx.Rollback()
}
}()
if err := tx.Error; err != nil {
return err
} }
}()
if err := tx.Error; err != nil {
return err
}
for {
var ids []int var ids []int
result := tx.Model(model).Where("created_at < ?", threshold).Limit(delDataSize).Pluck("id", &ids) result := tx.Model(model).Where("created_at < ?", threshold).Limit(delDataSize).Pluck("id", &ids)
if result.Error != nil { if result.Error != nil {
@ -143,24 +142,17 @@ func deleteOldData(model interface{}, daysAgo int) error {
return result.Error return result.Error
} }
if err := tx.Commit().Error; err != nil {
applogger.Error("事务提交失败: ", err)
return err
}
applogger.Info("事务提交成功")
totalRowsAffected += result.RowsAffected totalRowsAffected += result.RowsAffected
if len(ids) < batchSize { applogger.Info(fmt.Sprintf("删除 %d 条数据", result.RowsAffected))
applogger.Info("已处理所有符合条件的数据,退出循环")
break
}
} }
time.Sleep(time.Second)
elapsed := time.Since(start)
modelName := reflect.TypeOf(model).Elem().Name()
applogger.Info(fmt.Sprintf("执行删除%v用时%s成功删除数据%d", modelName, elapsed.String(), totalRowsAffected)) if err := tx.Commit().Error; err != nil {
closeDb(db) applogger.Error("提交事务失败: ", err)
return fmt.Errorf("提交事务失败: %w", err)
}
elapsed := time.Since(start)
applogger.Info(fmt.Sprintf("删除 %d 条数据,共耗时 %s", totalRowsAffected, elapsed))
return nil return nil
} }
@ -1366,9 +1358,9 @@ var ( //初始化变量
token string token string
lastCallPath string lastCallPath string
verifySignatureKey string verifySignatureKey string
dataExpirationDays int dataExpirationDays int //数据过期天数
delDataSize int delDataSize int //删除数据批次大小
rateLimiter uint rateLimiter uint //下载限速单位 M
dbMaxOpenConns int dbMaxOpenConns int
dbMaxIdleConns int dbMaxIdleConns int
dbConnMaxLifetime int dbConnMaxLifetime int