gonna swap from feeds.Feeds

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

1
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
golang.org/x/sys v0.31.0 // indirect golang.org/x/sys v0.31.0 // indirect
modernc.org/libc v1.62.1 // indirect modernc.org/libc v1.62.1 // indirect

2
go.sum
View File

@ -10,6 +10,8 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw= golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw=
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM= golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=

View File

@ -2,9 +2,11 @@ package cron
import ( import (
"context" "context"
"io" "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 {
@ -30,10 +32,33 @@ func One(ctx context.Context) error {
} }
return f.ForEach(ctx, func(feed feeds.Feed) 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 { 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 ( import (
"context" "context"
"fmt"
"net/http"
"net/http/httptest"
"show-rss/src/cmd/cron" "show-rss/src/cmd/cron"
"show-rss/src/db" "show-rss/src/db"
"show-rss/src/feeds" "show-rss/src/feeds"
"slices"
"strconv" "strconv"
"testing" "testing"
"time" "time"
@ -19,6 +23,19 @@ func TestOne(t *testing.T) {
return db.Test(t, ctx) return db.Test(t, ctx)
}, },
"feeds": func() context.Context { "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) ctx := db.Test(t, ctx)
f, err := feeds.New(ctx) f, err := feeds.New(ctx)
@ -26,7 +43,7 @@ func TestOne(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
for i := 0; i < 2; i++ { 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) t.Fatal(err)
} }
} }