每日清理
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Command audit-retention-simulate 在测试环境演练完整自然月归档与清理边界。
|
||||
// Command audit-retention-simulate 在测试环境演练逐日归档与清理边界。
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -6,22 +6,21 @@ import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/hibiken/asynq"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
|
||||
auditarchive "github.com/break/junhong_cmp_fiber/internal/application/auditarchive"
|
||||
auditinfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
taskapp "github.com/break/junhong_cmp_fiber/internal/task"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/config"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/database"
|
||||
logpkg "github.com/break/junhong_cmp_fiber/pkg/logger"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/storage"
|
||||
)
|
||||
|
||||
@@ -67,6 +66,11 @@ func run(ctx context.Context) error {
|
||||
return fmt.Errorf("仅允许显式确认的测试数据库,当前数据库为 %q", cfg.Database.DBName)
|
||||
}
|
||||
logger := zap.NewNop()
|
||||
if err := os.MkdirAll(filepath.Dir(cfg.Logging.RetentionLog.Filename), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
retentionLogger, syncRetentionLogger := logpkg.NewRetentionLogger(cfg.Logging.Level, logpkg.LogRotationConfig{Filename: cfg.Logging.RetentionLog.Filename, MaxSize: cfg.Logging.RetentionLog.MaxSize, MaxBackups: cfg.Logging.RetentionLog.MaxBackups, MaxAge: cfg.Logging.RetentionLog.MaxAge, Compress: cfg.Logging.RetentionLog.Compress})
|
||||
defer func() { _ = syncRetentionLogger() }()
|
||||
db, err := database.InitPostgreSQL(&cfg.Database, logger)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -81,8 +85,7 @@ func run(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
auditWriter := auditinfra.NewWriter(auditinfra.NewRegistry(), nil)
|
||||
service, err := auditarchive.NewService(db, provider, simulationInstance, auditWriter)
|
||||
service, err := auditarchive.NewService(db, provider, simulationInstance)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,14 +111,17 @@ func run(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
payload, _ := sonic.Marshal(taskapp.AuditMonthlyRetentionPayload{ArchiveMonth: simulationMonth})
|
||||
dryRunTask := asynq.NewTask(constants.TaskTypeAuditMonthlyRetention, payload)
|
||||
if err := taskapp.NewAuditMonthlyRetentionHandler(service, logger, false).Handle(ctx, dryRunTask); err != nil {
|
||||
return fmt.Errorf("月度只读演练失败: %w", err)
|
||||
}
|
||||
dryRun, err := service.ValidateMonth(ctx, monthStart)
|
||||
if err != nil {
|
||||
return err
|
||||
var dryRun auditarchive.RetentionResult
|
||||
for date := monthStart; date.Before(monthEnd); date = date.AddDate(0, 0, 1) {
|
||||
result, retainErr := service.RetainDate(ctx, date, false)
|
||||
if retainErr != nil {
|
||||
return fmt.Errorf("逐日只读演练失败: %w", retainErr)
|
||||
}
|
||||
retentionLogger.Info("日留存仿真只读校验通过", zap.String("archive_date", result.ArchiveDate))
|
||||
dryRun.EventCount += result.EventCount
|
||||
dryRun.ResourceCount += result.ResourceCount
|
||||
dryRun.IntegrationCount += result.IntegrationCount
|
||||
dryRun.EstimatedBatches += result.EstimatedBatches
|
||||
}
|
||||
summary, err := collectBeforeCleanup(ctx, db, monthStart, monthEnd, dryRun)
|
||||
if err != nil {
|
||||
@@ -125,16 +131,24 @@ func run(ctx context.Context) error {
|
||||
return fmt.Errorf("只归档模式写入了 %d 个清理断点", summary.CleanupMarkersBefore)
|
||||
}
|
||||
|
||||
cleanupTask := asynq.NewTask(constants.TaskTypeAuditMonthlyRetention, payload)
|
||||
if err := taskapp.NewAuditMonthlyRetentionHandler(service, logger, true).Handle(ctx, cleanupTask); err != nil {
|
||||
return fmt.Errorf("隔离测试库物理清理演练失败: %w", err)
|
||||
for date := monthStart; date.Before(monthEnd); date = date.AddDate(0, 0, 1) {
|
||||
result, retainErr := service.RetainDate(ctx, date, true)
|
||||
if retainErr != nil {
|
||||
return fmt.Errorf("隔离测试库逐日物理清理演练失败: %w", retainErr)
|
||||
}
|
||||
retentionLogger.Info("日留存仿真物理清理完成", zap.String("archive_date", result.ArchiveDate))
|
||||
}
|
||||
if err := collectAfterCleanup(ctx, db, monthStart, monthEnd, &summary); err != nil {
|
||||
return err
|
||||
}
|
||||
if summary.TargetRowsAfterCleanup != 0 || summary.BoundaryRowsAfterCleanup != 6 || summary.CleanupMarkersAfter != int64(summary.Days*2) || !summary.RetentionAuditRecorded {
|
||||
return fmt.Errorf("清理范围复核失败: target=%d boundary=%d markers=%d audit=%t",
|
||||
summary.TargetRowsAfterCleanup, summary.BoundaryRowsAfterCleanup, summary.CleanupMarkersAfter, summary.RetentionAuditRecorded)
|
||||
if summary.TargetRowsAfterCleanup != 0 || summary.BoundaryRowsAfterCleanup != 6 || summary.CleanupMarkersAfter != int64(summary.Days*2) {
|
||||
return fmt.Errorf("清理范围复核失败: target=%d boundary=%d markers=%d", summary.TargetRowsAfterCleanup, summary.BoundaryRowsAfterCleanup, summary.CleanupMarkersAfter)
|
||||
}
|
||||
if err := syncRetentionLogger(); err != nil {
|
||||
return fmt.Errorf("刷新日留存仿真日志失败: %w", err)
|
||||
}
|
||||
if _, err := os.Stat(cfg.Logging.RetentionLog.Filename); err != nil {
|
||||
return fmt.Errorf("独立日留存日志未生成: %w", err)
|
||||
}
|
||||
encoded, _ := sonic.MarshalIndent(summary, "", " ")
|
||||
fmt.Println(string(encoded))
|
||||
@@ -261,7 +275,12 @@ func archiveMonth(ctx context.Context, db *gorm.DB, service *auditarchive.Servic
|
||||
Updates(map[string]any{"result": constants.IntegrationResultSuccess, "updated_at": time.Now()}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return service.FinalizeIntegrationMonth(ctx, start)
|
||||
for date := start; date.Before(end); date = date.AddDate(0, 0, 1) {
|
||||
if err := service.FinalizeIntegrationDate(ctx, date); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func collectBeforeCleanup(ctx context.Context, db *gorm.DB, start, end time.Time, dryRun auditarchive.RetentionResult) (simulationSummary, error) {
|
||||
@@ -311,10 +330,5 @@ func collectAfterCleanup(ctx context.Context, db *gorm.DB, start, end time.Time,
|
||||
Count(&summary.CleanupMarkersAfter).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var auditCount int64
|
||||
if err := db.WithContext(ctx).Model(&model.AuditEvent{}).Where("event_id = ?", "evt_retention_"+strings.ReplaceAll(simulationMonth, "-", "_")).Count(&auditCount).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
summary.RetentionAuditRecorded = auditCount == 1
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user