no message
This commit is contained in:
parent
ab97c798ad
commit
1a3e8f5d8d
32
main.go
32
main.go
@ -19,7 +19,6 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
@ -111,7 +110,6 @@ func deleteOldData(model interface{}, daysAgo int) error {
|
||||
}
|
||||
threshold := time.Now().AddDate(0, 0, -daysAgo)
|
||||
|
||||
for {
|
||||
tx := db.Begin()
|
||||
applogger.Info("开始事务")
|
||||
defer func() {
|
||||
@ -124,6 +122,7 @@ func deleteOldData(model interface{}, daysAgo int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
var ids []int
|
||||
result := tx.Model(model).Where("created_at < ?", threshold).Limit(delDataSize).Pluck("id", &ids)
|
||||
if result.Error != nil {
|
||||
@ -143,24 +142,17 @@ func deleteOldData(model interface{}, daysAgo int) error {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
applogger.Error("事务提交失败: ", err)
|
||||
return err
|
||||
}
|
||||
applogger.Info("事务提交成功")
|
||||
|
||||
totalRowsAffected += result.RowsAffected
|
||||
if len(ids) < batchSize {
|
||||
applogger.Info("已处理所有符合条件的数据,退出循环")
|
||||
break
|
||||
applogger.Info(fmt.Sprintf("删除 %d 条数据", result.RowsAffected))
|
||||
}
|
||||
}
|
||||
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))
|
||||
closeDb(db)
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
applogger.Error("提交事务失败: ", err)
|
||||
return fmt.Errorf("提交事务失败: %w", err)
|
||||
}
|
||||
|
||||
elapsed := time.Since(start)
|
||||
applogger.Info(fmt.Sprintf("删除 %d 条数据,共耗时 %s", totalRowsAffected, elapsed))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -1366,9 +1358,9 @@ var ( //初始化变量
|
||||
token string
|
||||
lastCallPath string
|
||||
verifySignatureKey string
|
||||
dataExpirationDays int
|
||||
delDataSize int
|
||||
rateLimiter uint
|
||||
dataExpirationDays int //数据过期天数
|
||||
delDataSize int //删除数据批次大小
|
||||
rateLimiter uint //下载限速单位 M
|
||||
dbMaxOpenConns int
|
||||
dbMaxIdleConns int
|
||||
dbConnMaxLifetime int
|
||||
|
||||
Loading…
Reference in New Issue
Block a user