58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package purge
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var ErrInvalidPurgeInterval = errors.New("invalid purge interval")
|
|
var ErrPurgeConflict = errors.New("the library changed since the fingerprint")
|
|
var ErrPurgeBusy = errors.New("a purge is already running")
|
|
|
|
const (
|
|
DefaultIntervalDays = 30
|
|
MinIntervalDays = 1
|
|
MaxIntervalDays = 365
|
|
)
|
|
|
|
type Settings struct {
|
|
AutoPurgeEnabled bool `json:"auto_purge_enabled"`
|
|
AutoPurgeIntervalDays int `json:"auto_purge_interval_days"`
|
|
LastPurgeAt *time.Time `json:"last_purge_at"`
|
|
LastPurgeMerged int `json:"last_purge_merged"`
|
|
LastPurgeArticlesUpdated int `json:"last_purge_articles_updated"`
|
|
EnabledAt *time.Time `json:"enabled_at"`
|
|
}
|
|
|
|
func DefaultSettings() Settings {
|
|
return Settings{AutoPurgeIntervalDays: DefaultIntervalDays}
|
|
}
|
|
|
|
func ValidateIntervalDays(days int) error {
|
|
if days < MinIntervalDays || days > MaxIntervalDays {
|
|
return ErrInvalidPurgeInterval
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s Settings) NextPurgeAt() *time.Time {
|
|
if !s.AutoPurgeEnabled || s.EnabledAt == nil {
|
|
return nil
|
|
}
|
|
base := *s.EnabledAt
|
|
if s.LastPurgeAt != nil && s.LastPurgeAt.After(base) {
|
|
base = *s.LastPurgeAt
|
|
}
|
|
next := base.AddDate(0, 0, s.AutoPurgeIntervalDays)
|
|
return &next
|
|
}
|
|
|
|
type RunResult struct {
|
|
GroupsTotal int
|
|
GroupsPurged int
|
|
Merged int
|
|
ArticlesUpdated int
|
|
SitesUpdated int
|
|
FailedGroups int
|
|
}
|