fix: 初验针对修改
This commit is contained in:
71
internal/systemsettings/sync.go
Normal file
71
internal/systemsettings/sync.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package systemsettings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/ops/logs/internal/config"
|
||||
"git.apinb.com/ops/logs/internal/impl"
|
||||
"git.apinb.com/ops/logs/internal/models"
|
||||
settingsv1 "git.apinb.com/ops/pkgs/systemsettings/v1"
|
||||
)
|
||||
|
||||
var retentionDays atomic.Int64
|
||||
|
||||
func LoadInitial(ctx context.Context) (*settingsv1.Client, error) {
|
||||
client, err := settingsv1.NewClient(
|
||||
config.Spec.SystemSettings.BaseURL,
|
||||
time.Duration(config.Spec.SystemSettings.TimeoutSeconds)*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values, err := client.Fetch(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value, err := values.Require(settingsv1.OperationLogRetentionDays)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
retentionDays.Store(int64(value))
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func Start(ctx context.Context, client *settingsv1.Client) {
|
||||
refreshTicker := time.NewTicker(time.Duration(config.Spec.SystemSettings.RefreshSeconds) * time.Second)
|
||||
cleanupTicker := time.NewTicker(time.Hour)
|
||||
defer refreshTicker.Stop()
|
||||
defer cleanupTicker.Stop()
|
||||
cleanup(ctx)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-refreshTicker.C:
|
||||
values, err := client.Fetch(ctx)
|
||||
if err != nil {
|
||||
log.Printf("logs: 刷新系统参数失败: %v", err)
|
||||
continue
|
||||
}
|
||||
value, err := values.Require(settingsv1.OperationLogRetentionDays)
|
||||
if err != nil {
|
||||
log.Printf("logs: 应用系统参数失败: %v", err)
|
||||
continue
|
||||
}
|
||||
retentionDays.Store(int64(value))
|
||||
case <-cleanupTicker.C:
|
||||
cleanup(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cleanup(ctx context.Context) {
|
||||
cutoff := time.Now().UTC().AddDate(0, 0, -int(retentionDays.Load()))
|
||||
result := impl.DBService.WithContext(ctx).Where("created_at < ?", cutoff).Delete(&models.AuditLog{})
|
||||
if result.Error != nil {
|
||||
log.Printf("logs: 清理过期操作日志失败: %v", result.Error)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user