// Package task hosts the scheduled jobs ported from sdf-serve. The daily // log auto-archive job moves oldest audit logs into MinIO when they exceed // the archive_threshold configured in t_config. package task import ( "context" "udeServer/internal/audit" "udeServer/internal/log" "udeServer/internal/svc" "github.com/robfig/cron/v3" ) // StartScheduler registers and starts the cron scheduler. func StartScheduler(ctx *svc.ServiceContext) { c := cron.New() // 每天 00:30 执行自动归档 _, err := c.AddFunc("30 0 * * *", func() { log.RecordInfo(context.Background(), "[Ude-Task]开始自动归档日志!") LogAutoArchive(ctx) }) if err != nil { log.RecordError(context.Background(), err) } log.RecordInfo(context.Background(), "定时任务启动中...") c.Start() } // LogAutoArchive archives oldest logs while they exceed the threshold. func LogAutoArchive(svcCtx *svc.ServiceContext) { ctx := context.Background() threshold := svcCtx.LogConfigManager.ArchiveThreshold if threshold <= 0 { log.RecordInfo(ctx, "[Task-LogArchive]归档阈值未配置(t_config.archive_threshold),跳过自动归档!") return } if svcCtx.MinioClient == nil { log.RecordWarn(ctx, "[Task-LogArchive]MinIO客户端未加载(检查t_config的file_*配置),跳过自动归档!") return } archiveName := "system_auto_archive" for { list, err := svcCtx.SystemLogModel.FindAllWithLimit(ctx, int(threshold)) if err != nil { log.RecordError(ctx, err) return } if int64(len(list)) < threshold { log.RecordInfo(ctx, "[Task-LogArchive]当前日志%d条,未到达归档阈值%d,跳过自动归档!", len(list), threshold) break } _, err = audit.ArchiveLogsToStorageAndRecord(ctx, list, archiveName, threshold, audit.AutoArchive, svcCtx.MinioClient, svcCtx.DB) if err != nil { log.RecordError(ctx, err) return } log.RecordInfo(ctx, "[Task-LogArchive]自动归档:%d条日志成功!", len(list)) } }