move cron parse into feed.ShouldExecute

main
Bel LaPointe 2025-04-27 12:04:11 -06:00
parent a097814a62
commit ab396d1833
3 changed files with 55 additions and 13 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
})
}