From cc44d14722612d6a3db4360a971ac7cd0844b7ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 16 Oct 2023 21:20:58 -0700 Subject: [PATCH] Avoid excessive manual polling with default scheduler --- internal/api/category.go | 12 +++++++++--- internal/api/feed.go | 11 ++++++++--- internal/model/feed.go | 2 +- internal/model/feed_test.go | 4 ++++ internal/storage/job.go | 4 ++-- internal/ui/category_refresh.go | 12 +++++++++--- internal/ui/feed_refresh.go | 10 +++++++--- 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/internal/api/category.go b/internal/api/category.go index a378b32f..9e13a28f 100644 --- a/internal/api/category.go +++ b/internal/api/category.go @@ -5,6 +5,7 @@ package api // import "miniflux.app/v2/internal/api" import ( json_parser "encoding/json" + "log/slog" "net/http" "time" @@ -141,9 +142,14 @@ func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) { return } - go func() { - h.pool.Push(jobs) - }() + slog.Info( + "Triggered a manual refresh of all feeds for a given category from the API", + slog.Int64("user_id", userID), + slog.Int64("category_id", categoryID), + slog.Int("nb_jobs", len(jobs)), + ) + + go h.pool.Push(jobs) json.NoContent(w, r) } diff --git a/internal/api/feed.go b/internal/api/feed.go index 982a6bf8..0f486f70 100644 --- a/internal/api/feed.go +++ b/internal/api/feed.go @@ -5,6 +5,7 @@ package api // import "miniflux.app/v2/internal/api" import ( json_parser "encoding/json" + "log/slog" "net/http" "time" @@ -74,9 +75,13 @@ func (h *handler) refreshAllFeeds(w http.ResponseWriter, r *http.Request) { return } - go func() { - h.pool.Push(jobs) - }() + slog.Info( + "Triggered a manual refresh of all feeds from the API", + slog.Int64("user_id", userID), + slog.Int("nb_jobs", len(jobs)), + ) + + go h.pool.Push(jobs) json.NoContent(w, r) } diff --git a/internal/model/feed.go b/internal/model/feed.go index f35ebc56..1f87b416 100644 --- a/internal/model/feed.go +++ b/internal/model/feed.go @@ -122,7 +122,7 @@ func (f *Feed) ScheduleNextCheck(weeklyCount int) { } f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes)) default: - f.NextCheckAt = time.Now() + f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(config.Opts.PollingFrequency())) } } diff --git a/internal/model/feed_test.go b/internal/model/feed_test.go index 34f89e90..0a4c44a8 100644 --- a/internal/model/feed_test.go +++ b/internal/model/feed_test.go @@ -97,6 +97,10 @@ func TestFeedScheduleNextCheckDefault(t *testing.T) { if feed.NextCheckAt.IsZero() { t.Error(`The next_check_at must be set`) } + + if feed.NextCheckAt.After(time.Now().Add(time.Minute * time.Duration(config.Opts.PollingFrequency()))) { + t.Error(`The next_check_at should not be after the now + polling frequency`) + } } func TestFeedScheduleNextCheckEntryCountBasedMaxInterval(t *testing.T) { diff --git a/internal/storage/job.go b/internal/storage/job.go index de3fdb32..a4b355a0 100644 --- a/internal/storage/job.go +++ b/internal/storage/job.go @@ -38,7 +38,7 @@ func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, FROM feeds WHERE - user_id=$1 AND disabled is false + user_id=$1 AND disabled is false AND next_check_at < now() ORDER BY next_check_at ASC LIMIT %d ` return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID) @@ -55,7 +55,7 @@ func (s *Storage) NewCategoryBatch(userID int64, categoryID int64, batchSize int FROM feeds WHERE - user_id=$1 AND category_id=$2 AND disabled is false + user_id=$1 AND category_id=$2 AND disabled is false AND next_check_at < now() ORDER BY next_check_at ASC LIMIT %d ` return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID, categoryID) diff --git a/internal/ui/category_refresh.go b/internal/ui/category_refresh.go index 5d084c06..f852a64b 100644 --- a/internal/ui/category_refresh.go +++ b/internal/ui/category_refresh.go @@ -4,6 +4,7 @@ package ui // import "miniflux.app/v2/internal/ui" import ( + "log/slog" "net/http" "miniflux.app/v2/internal/http/request" @@ -31,9 +32,14 @@ func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) int64 return 0 } - go func() { - h.pool.Push(jobs) - }() + slog.Info( + "Triggered a manual refresh of all feeds for a given category from the web ui", + slog.Int64("user_id", userID), + slog.Int64("category_id", categoryID), + slog.Int("nb_jobs", len(jobs)), + ) + + go h.pool.Push(jobs) return categoryID } diff --git a/internal/ui/feed_refresh.go b/internal/ui/feed_refresh.go index 11c5cff5..07da1ed5 100644 --- a/internal/ui/feed_refresh.go +++ b/internal/ui/feed_refresh.go @@ -36,9 +36,13 @@ func (h *handler) refreshAllFeeds(w http.ResponseWriter, r *http.Request) { return } - go func() { - h.pool.Push(jobs) - }() + slog.Info( + "Triggered a manual refresh of all feeds from the web ui", + slog.Int64("user_id", userID), + slog.Int("nb_jobs", len(jobs)), + ) + + go h.pool.Push(jobs) html.Redirect(w, r, route.Path(h.router, "feeds")) }