feat(feeds): add scheduled cleanup (#1073)

* refactor(feeds): make feed scheduling more robust

* feat(feeds): add daily cleanup job

* removes feed cache older than 30 days

* fix(feeds): fmt wrong type
This commit is contained in:
ze0s 2023-09-02 22:44:28 +02:00 committed by GitHub
parent cfc2436d50
commit 6fd8626507
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 301 additions and 215 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/internal/notification"
"github.com/autobrr/autobrr/internal/update"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
@ -19,7 +20,8 @@ import (
type Service interface {
Start()
Stop()
AddJob(job cron.Job, interval time.Duration, identifier string) (int, error)
ScheduleJob(job cron.Job, interval time.Duration, identifier string) (int, error)
AddJob(job cron.Job, spec string, identifier string) (int, error)
RemoveJobByIdentifier(id string) error
GetNextRun(id string) (time.Time, error)
}
@ -74,7 +76,7 @@ func (s *service) addAppJobs() {
lastCheckVersion: s.version,
}
if id, err := s.AddJob(checkUpdates, 2*time.Hour, "app-check-updates"); err != nil {
if id, err := s.ScheduleJob(checkUpdates, 2*time.Hour, "app-check-updates"); err != nil {
s.log.Error().Err(err).Msgf("scheduler.addAppJobs: error adding job: %v", id)
}
}
@ -86,11 +88,27 @@ func (s *service) Stop() {
return
}
func (s *service) AddJob(job cron.Job, interval time.Duration, identifier string) (int, error) {
// ScheduleJob takes a time duration and adds a job
func (s *service) ScheduleJob(job cron.Job, interval time.Duration, identifier string) (int, error) {
id := s.cron.Schedule(cron.Every(interval), cron.NewChain(cron.SkipIfStillRunning(cron.DiscardLogger)).Then(job))
id := s.cron.Schedule(cron.Every(interval), cron.NewChain(
cron.SkipIfStillRunning(cron.DiscardLogger)).Then(job),
)
s.log.Debug().Msgf("scheduler.ScheduleJob: job successfully added: %s id %d", identifier, id)
s.m.Lock()
// add to job map
s.jobs[identifier] = id
s.m.Unlock()
return int(id), nil
}
// AddJob takes a cron schedule and adds a job
func (s *service) AddJob(job cron.Job, spec string, identifier string) (int, error) {
id, err := s.cron.AddJob(spec, cron.NewChain(cron.SkipIfStillRunning(cron.DiscardLogger)).Then(job))
if err != nil {
return 0, errors.Wrap(err, "could not add job to cron")
}
s.log.Debug().Msgf("scheduler.AddJob: job successfully added: %s id %d", identifier, id)