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
This commit is contained in:
Kyle Sanderson 2022-08-01 14:24:14 -07:00 committed by GitHub
parent eefd1b576c
commit bbfa5627aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 15 deletions

View file

@ -2,7 +2,6 @@ package feed
import ( import (
"context" "context"
"fmt"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger" "github.com/autobrr/autobrr/internal/logger"
@ -13,6 +12,7 @@ import (
"github.com/dcarbone/zadapters/zstdlog" "github.com/dcarbone/zadapters/zstdlog"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"time"
) )
type Service interface { type Service interface {
@ -34,7 +34,7 @@ type feedInstance struct {
URL string URL string
ApiKey string ApiKey string
Implementation string Implementation string
CronSchedule string CronSchedule time.Duration
} }
type service struct { type service struct {
@ -274,15 +274,13 @@ func (s *service) startJob(f domain.Feed) error {
} }
// cron schedule to run every X minutes // cron schedule to run every X minutes
schedule := fmt.Sprintf("*/%d * * * *", f.Interval)
fi := feedInstance{ fi := feedInstance{
Name: f.Name, Name: f.Name,
IndexerIdentifier: f.Indexer, IndexerIdentifier: f.Indexer,
Implementation: f.Type, Implementation: f.Type,
URL: f.URL, URL: f.URL,
ApiKey: f.ApiKey, ApiKey: f.ApiKey,
CronSchedule: schedule, CronSchedule: time.Duration(f.Interval) * time.Minute,
} }
switch fi.Implementation { switch fi.Implementation {
@ -302,8 +300,8 @@ func (s *service) addTorznabJob(f feedInstance) error {
if f.URL == "" { if f.URL == "" {
return errors.New("torznab feed requires URL") return errors.New("torznab feed requires URL")
} }
if f.CronSchedule == "" { if f.CronSchedule < time.Duration(5 * time.Minute) {
f.CronSchedule = "*/15 * * * *" f.CronSchedule = time.Duration(15 * time.Minute)
} }
// setup logger // setup logger

View file

@ -5,7 +5,6 @@ import (
"github.com/autobrr/autobrr/internal/logger" "github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/internal/notification" "github.com/autobrr/autobrr/internal/notification"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -14,7 +13,7 @@ import (
type Service interface { type Service interface {
Start() Start()
Stop() 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 RemoveJobByID(id cron.EntryID) error
RemoveJobByIdentifier(id string) error RemoveJobByIdentifier(id string) error
} }
@ -63,7 +62,7 @@ func (s *service) addAppJobs() {
lastCheckVersion: "", lastCheckVersion: "",
} }
s.AddJob(checkUpdates, "2 */6 * * *", "app-check-updates") s.AddJob(checkUpdates, time.Duration(36 * time.Hour), "app-check-updates")
} }
func (s *service) Stop() { func (s *service) Stop() {
@ -72,14 +71,11 @@ func (s *service) Stop() {
return 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), 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) s.log.Debug().Msgf("scheduler.AddJob: job successfully added: %v", id)