|
| 1 | +/* |
| 2 | + * Copyright 2025 The Dragonfly Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package gc |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "time" |
| 22 | + |
| 23 | + "gorm.io/gorm" |
| 24 | + |
| 25 | + logger "d7y.io/dragonfly/v2/internal/dflog" |
| 26 | + "d7y.io/dragonfly/v2/manager/models" |
| 27 | + pkggc "d7y.io/dragonfly/v2/pkg/gc" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // DefaultAuditGCBatchSize is the default batch size for deleting jobs. |
| 32 | + DefaultAuditGCBatchSize = 5000 |
| 33 | + |
| 34 | + // DefaultAuditGCInterval is the default interval for running audit GC. |
| 35 | + DefaultAuditGCInterval = time.Hour * 6 |
| 36 | + |
| 37 | + // DefaultAuditGCTimeout is the default timeout for running audit GC. |
| 38 | + DefaultAuditGCTimeout = time.Hour * 2 |
| 39 | + |
| 40 | + // AuditGCTaskID is the ID of the audit GC task. |
| 41 | + AuditGCTaskID = "audit" |
| 42 | +) |
| 43 | + |
| 44 | +func NewAuditGCTask(db *gorm.DB) pkggc.Task { |
| 45 | + return pkggc.Task{ |
| 46 | + ID: AuditGCTaskID, |
| 47 | + Interval: DefaultAuditGCInterval, |
| 48 | + Timeout: DefaultAuditGCTimeout, |
| 49 | + Runner: &audit{db: db, recorder: newJobRecorder(db)}, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// audit is the struct for cleaning up audits which implements the gc Runner interface. |
| 54 | +type audit struct { |
| 55 | + db *gorm.DB |
| 56 | + recorder *jobRecorder |
| 57 | +} |
| 58 | + |
| 59 | +// RunGC implements the gc Runner interface. |
| 60 | +func (a *audit) RunGC() error { |
| 61 | + ttl, err := a.getTTL() |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + if err := a.recorder.Init(AuditGCTaskID, models.JSONMap{ |
| 67 | + "ttl": ttl, |
| 68 | + "batch_size": DefaultAuditGCBatchSize, |
| 69 | + }); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + var gcResult Result |
| 74 | + defer func() { |
| 75 | + if err := a.recorder.Record(gcResult); err != nil { |
| 76 | + logger.Errorf("failed to record audit GC result: %v", err) |
| 77 | + } |
| 78 | + }() |
| 79 | + |
| 80 | + for { |
| 81 | + result := a.db.Where("created_at < ?", time.Now().Add(-ttl)).Limit(DefaultAuditGCBatchSize).Unscoped().Delete(&models.Audit{}) |
| 82 | + if result.Error != nil { |
| 83 | + gcResult.Error = result.Error |
| 84 | + return result.Error |
| 85 | + } |
| 86 | + |
| 87 | + if result.RowsAffected == 0 { |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + gcResult.Purged += result.RowsAffected |
| 92 | + logger.Infof("gc audit deleted %d audits", result.RowsAffected) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func (a *audit) getTTL() (time.Duration, error) { |
| 99 | + var config models.Config |
| 100 | + if err := a.db.Model(models.Config{}).First(&config, &models.Config{Name: models.ConfigGC}).Error; err != nil { |
| 101 | + return 0, err |
| 102 | + } |
| 103 | + |
| 104 | + var gcConfig models.GCConfig |
| 105 | + if err := json.Unmarshal([]byte(config.Value), &gcConfig); err != nil { |
| 106 | + return 0, err |
| 107 | + } |
| 108 | + |
| 109 | + return gcConfig.Audit.TTL, nil |
| 110 | +} |
0 commit comments