move cron parse into feed.ShouldExecute
parent
a097814a62
commit
ab396d1833
|
|
@ -5,8 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"show-rss/src/feeds"
|
"show-rss/src/feeds"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/robfig/cron/v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Main(ctx context.Context) error {
|
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 {
|
func one(ctx context.Context, feed feeds.Feed) error {
|
||||||
if schedule, err := cron.NewParser(
|
if should, err := feed.ShouldExecute(); err != nil {
|
||||||
cron.SecondOptional |
|
return err
|
||||||
cron.Minute |
|
} else if !should {
|
||||||
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) {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,30 @@ package feeds
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io"
|
"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) {
|
func (feed Feed) Fetch(ctx context.Context) (Items, error) {
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,32 @@
|
||||||
package feeds_test
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue