feat: setup logs retention by max file size
This commit is contained in:
1 parent
70c8492f4f
commit
f914eae3c5
44 files changed
+2316
-161
No files matched your search
@@ -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))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user