From bbfa5627aa1af188f7c3f44312eaf986d3b099c5 Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Mon, 1 Aug 2022 14:24:14 -0700 Subject: [PATCH] fix(scheduler): use time as opposed to cron fields (#390) * fix(torznab): use time for interval. * flip Schedule to be duration based * typefix * Update service.go * clear err * pull err package * casting to the moon * enforce a minimum of 15m * lower minimum based on feedback from Unit3d --- internal/feed/service.go | 12 +++++------- internal/scheduler/service.go | 12 ++++-------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/internal/feed/service.go b/internal/feed/service.go index 190e6ca..dfa39a9 100644 --- a/internal/feed/service.go +++ b/internal/feed/service.go @@ -2,7 +2,6 @@ package feed import ( "context" - "fmt" "github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/logger" @@ -13,6 +12,7 @@ import ( "github.com/dcarbone/zadapters/zstdlog" "github.com/rs/zerolog" + "time" ) type Service interface { @@ -34,7 +34,7 @@ type feedInstance struct { URL string ApiKey string Implementation string - CronSchedule string + CronSchedule time.Duration } type service struct { @@ -274,15 +274,13 @@ func (s *service) startJob(f domain.Feed) error { } // cron schedule to run every X minutes - schedule := fmt.Sprintf("*/%d * * * *", f.Interval) - fi := feedInstance{ Name: f.Name, IndexerIdentifier: f.Indexer, Implementation: f.Type, URL: f.URL, ApiKey: f.ApiKey, - CronSchedule: schedule, + CronSchedule: time.Duration(f.Interval) * time.Minute, } switch fi.Implementation { @@ -302,8 +300,8 @@ func (s *service) addTorznabJob(f feedInstance) error { if f.URL == "" { return errors.New("torznab feed requires URL") } - if f.CronSchedule == "" { - f.CronSchedule = "*/15 * * * *" + if f.CronSchedule < time.Duration(5 * time.Minute) { + f.CronSchedule = time.Duration(15 * time.Minute) } // setup logger diff --git a/internal/scheduler/service.go b/internal/scheduler/service.go index c933b48..9df58a1 100644 --- a/internal/scheduler/service.go +++ b/internal/scheduler/service.go @@ -5,7 +5,6 @@ import ( "github.com/autobrr/autobrr/internal/logger" "github.com/autobrr/autobrr/internal/notification" - "github.com/autobrr/autobrr/pkg/errors" "github.com/robfig/cron/v3" "github.com/rs/zerolog" @@ -14,7 +13,7 @@ import ( type Service interface { Start() Stop() - AddJob(job cron.Job, interval string, identifier string) (int, error) + AddJob(job cron.Job, interval time.Duration, identifier string) (int, error) RemoveJobByID(id cron.EntryID) error RemoveJobByIdentifier(id string) error } @@ -63,7 +62,7 @@ func (s *service) addAppJobs() { lastCheckVersion: "", } - s.AddJob(checkUpdates, "2 */6 * * *", "app-check-updates") + s.AddJob(checkUpdates, time.Duration(36 * time.Hour), "app-check-updates") } func (s *service) Stop() { @@ -72,14 +71,11 @@ func (s *service) Stop() { return } -func (s *service) AddJob(job cron.Job, interval string, identifier string) (int, error) { +func (s *service) AddJob(job cron.Job, interval time.Duration, identifier string) (int, error) { - id, err := s.cron.AddJob(interval, cron.NewChain( + id := s.cron.Schedule(cron.Every(interval), cron.NewChain( cron.SkipIfStillRunning(cron.DiscardLogger)).Then(job), ) - if err != nil { - return 0, errors.Wrap(err, "scheduler: add job failed") - } s.log.Debug().Msgf("scheduler.AddJob: job successfully added: %v", id)