From ab396d1833acbb5bceef25a970a36509ec3fbc4c Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 27 Apr 2025 12:04:11 -0600 Subject: [PATCH] move cron parse into feed.ShouldExecute --- src/cmd/cron/main.go | 16 +++------------- src/feeds/http.go | 21 +++++++++++++++++++++ src/feeds/http_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/cmd/cron/main.go b/src/cmd/cron/main.go index b3c4920..d8ca119 100644 --- a/src/cmd/cron/main.go +++ b/src/cmd/cron/main.go @@ -5,8 +5,6 @@ import ( "fmt" "show-rss/src/feeds" "time" - - "github.com/robfig/cron/v3" ) func Main(ctx context.Context) error { @@ -35,17 +33,9 @@ func One(ctx context.Context) error { } func one(ctx context.Context, feed feeds.Feed) error { - if schedule, err := cron.NewParser( - cron.SecondOptional | - cron.Minute | - cron.Hour | - cron.Dom | - cron.Month | - cron.Dow | - cron.Descriptor, - ).Parse(feed.Version.Cron); err != nil { - return fmt.Errorf("illegal cron %q", feed.Version.Cron) - } else if next := schedule.Next(feed.Execution.Executed); time.Now().Before(next) { + if should, err := feed.ShouldExecute(); err != nil { + return err + } else if !should { return nil } diff --git a/src/feeds/http.go b/src/feeds/http.go index bd564a3..e1817c1 100644 --- a/src/feeds/http.go +++ b/src/feeds/http.go @@ -2,9 +2,30 @@ package feeds import ( "context" + "fmt" "io" + "time" + + "github.com/robfig/cron/v3" ) +func (feed Feed) ShouldExecute() (bool, error) { + schedule, err := cron.NewParser( + cron.SecondOptional | + cron.Minute | + cron.Hour | + cron.Dom | + cron.Month | + cron.Dow | + cron.Descriptor, + ).Parse(feed.Version.Cron) + if err != nil { + return false, fmt.Errorf("illegal cron %q", feed.Version.Cron) + } + next := schedule.Next(feed.Execution.Executed) + return time.Now().Before(next), nil +} + func (feed Feed) Fetch(ctx context.Context) (Items, error) { return nil, io.EOF } diff --git a/src/feeds/http_test.go b/src/feeds/http_test.go index 3497b2e..214fc5a 100644 --- a/src/feeds/http_test.go +++ b/src/feeds/http_test.go @@ -1 +1,32 @@ package feeds_test + +import ( + "show-rss/src/feeds" + "testing" + "time" +) + +func TestFeed(t *testing.T) { + t.Run("fetch", func(t *testing.T) { + created := time.Now().Add(-4 * time.Second) + feed := feeds.Feed{ + Entry: feeds.Entry{ + ID: "id", + Created: created, + Updated: created, + Deleted: time.Time{}, + }, + Version: feeds.Version{ + Created: created, + URL: "url", + Cron: "* * * * *", + Pattern: "matches", + }, + Execution: feeds.Execution{ + Executed: created.Add(-2 * time.Second), + Version: created, + }, + } + _ = feed + }) +}