feat: setup logs retention by max file size

This commit is contained in:
fsylvain committed 2026-10-07 22:02:13 +02:00
1 parent 70c8492f4f
commit f914eae3c5
44 files changed
+2316 -161

No files matched your search

+2
View File
@@ -105,6 +105,8 @@ const (
ParamRemoved = "removed"
ParamFrom = "from"
ParamTo = "to"
ParamMaxGBFrom = "max_gb_from"
ParamMaxGBTo = "max_gb_to"
ParamMerged = "merged"
ParamArticlesUpdated = "articles_updated"
ParamFailedGroups = "failed_groups"
@@ -7,10 +7,12 @@ type Repository interface {
List(filter Filter) (Page, error)
Summary(query SummaryQuery) (Summary, error)
DeleteOlderThan(cutoff time.Time) (int, error)
DeleteOldest(limit int, before time.Time) (int, error)
SizeBytes() (int64, error)
}
type SettingsRepository interface {
Get() (Settings, error)
SaveRetention(days int) error
SaveRetention(days int, maxGB int) error
SaveLastCleanup(at time.Time, removed int) error
}
+31 -1
View File
@@ -7,6 +7,9 @@ const (
ForeverRetentionDays = 0
CleanupHour = 3
KeepRecentHours = 24
NoSizeLimitGB = 0
BytesPerGB = int64(1) << 30
RowOverheadBytes = int64(64)
)
func RetentionOptions() []int {
@@ -22,8 +25,23 @@ func ValidateRetentionDays(days int) error {
return ErrInvalidRetention
}
func SizeOptions() []int {
return []int{NoSizeLimitGB, 1, 5, 10, 25, 50}
}
func ValidateRetentionMaxGB(maxGB int) error {
for _, option := range SizeOptions() {
if option == maxGB {
return nil
}
}
return ErrInvalidRetention
}
type Settings struct {
RetentionDays int `json:"activity_retention_days"`
RetentionMaxGB int `json:"activity_retention_max_gb"`
LogSizeBytes int64 `json:"activity_log_size_bytes"`
LastCleanupAt *time.Time `json:"activity_last_cleanup_at"`
LastCleanupRemoved int `json:"activity_last_cleanup_removed"`
NextCleanupAt *time.Time `json:"activity_next_cleanup_at"`
@@ -33,6 +51,14 @@ func DefaultSettings() Settings {
return Settings{RetentionDays: DefaultRetentionDays}
}
func (s Settings) HasRetentionLimit() bool {
return s.RetentionDays != ForeverRetentionDays || s.RetentionMaxGB != NoSizeLimitGB
}
func (s Settings) MaxLogSizeBytes() int64 {
return int64(s.RetentionMaxGB) * BytesPerGB
}
func (s Settings) CleanupDue(now time.Time) bool {
todayRun := time.Date(now.Year(), now.Month(), now.Day(), CleanupHour, 0, 0, 0, now.Location())
if now.Before(todayRun) {
@@ -43,7 +69,7 @@ func (s Settings) CleanupDue(now time.Time) bool {
func (s Settings) WithNextCleanup(now time.Time) Settings {
s.NextCleanupAt = nil
if s.RetentionDays == ForeverRetentionDays {
if !s.HasRetentionLimit() {
return s
}
next := time.Date(now.Year(), now.Month(), now.Day(), CleanupHour, 0, 0, 0, now.Location())
@@ -62,6 +88,10 @@ func ClampCutoff(now time.Time, cutoff time.Time) time.Time {
return cutoff
}
func GuardCutoff(now time.Time) time.Time {
return ClampCutoff(now, now)
}
func RetentionCutoff(now time.Time, retentionDays int) time.Time {
return ClampCutoff(now, now.Add(-time.Duration(retentionDays)*24*time.Hour))
}