Browse Source

Avoid excessive manual polling with default scheduler

Frédéric Guillot 2 years ago
parent
commit
cc44d14722

+ 9 - 3
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)
 }

+ 8 - 3
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)
 }

+ 1 - 1
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()))
 	}
 }
 

+ 4 - 0
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) {

+ 2 - 2
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)

+ 9 - 3
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
 }

+ 7 - 3
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"))
 }