gonna swap from feeds.Feeds

This commit is contained in:
Bel LaPointe
2025-04-27 11:46:20 -06:00
parent 537eaf9801
commit e54c7a76f9
4 changed files with 49 additions and 4 deletions

View File

@@ -2,9 +2,11 @@ package cron
import (
"context"
"io"
"fmt"
"show-rss/src/feeds"
"time"
"github.com/robfig/cron/v3"
)
func Main(ctx context.Context) error {
@@ -30,10 +32,33 @@ func One(ctx context.Context) error {
}
return f.ForEach(ctx, func(feed feeds.Feed) error {
return one(ctx, feed)
if err := one(ctx, feed); err != nil {
return fmt.Errorf("failed to cron %s (%+v): %w", feed.Entry.ID, feed.Version, err)
}
return nil
})
}
func one(ctx context.Context, feed feeds.Feed) error {
return io.EOF
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) {
return nil
}
return fmt.Errorf("should GET %s", feed.Version.URL)
f, err := feeds.New(ctx)
if err != nil {
return fmt.Errorf("failed new feeds to executed %s: %w", feed.Entry.ID, err)
}
return f.Executed(ctx, feed.Entry.ID, feed.Version.Created)
}

View File

@@ -2,9 +2,13 @@ package cron_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"show-rss/src/cmd/cron"
"show-rss/src/db"
"show-rss/src/feeds"
"slices"
"strconv"
"testing"
"time"
@@ -19,6 +23,19 @@ func TestOne(t *testing.T) {
return db.Test(t, ctx)
},
"feeds": func() context.Context {
gets := []string{}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gets = append(gets, r.URL.String())
t.Logf("%s", gets[len(gets)-1])
}))
t.Cleanup(s.Close)
t.Cleanup(func() {
slices.Sort(gets)
if len(gets) != 2+2+2 { // id=1+id=2 for each of 2 unrecycled ctx, id=1+id=2 for one across shared ctx
t.Errorf("didn't call urls exactly twice: %+v", gets)
}
})
ctx := db.Test(t, ctx)
f, err := feeds.New(ctx)
@@ -26,7 +43,7 @@ func TestOne(t *testing.T) {
t.Fatal(err)
}
for i := 0; i < 2; i++ {
if _, err := f.Insert(ctx, strconv.Itoa(i), "* * * * *"); err != nil {
if _, err := f.Insert(ctx, fmt.Sprintf("%s?idx=%d", s.URL, i), "* * * * *"); err != nil {
t.Fatal(err)
}
}