Setting NextCheckAt due to TTL of a feed in feed.go.

Add unit tests.
This commit is contained in:
Shizun Ge 2023-11-23 01:24:42 -08:00 committed by Frédéric Guillot
parent a8daee60fb
commit 27ec6dbd7d
3 changed files with 98 additions and 32 deletions

View File

@ -107,21 +107,27 @@ func (f *Feed) CheckedNow() {
} }
// ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
func (f *Feed) ScheduleNextCheck(weeklyCount int) { func (f *Feed) ScheduleNextCheck(weeklyCount int, newTTL int) {
f.TTL = newTTL
// Default to the global config Polling Frequency.
var intervalMinutes int
switch config.Opts.PollingScheduler() { switch config.Opts.PollingScheduler() {
case SchedulerEntryFrequency: case SchedulerEntryFrequency:
var intervalMinutes int if weeklyCount <= 0 {
if weeklyCount == 0 {
intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval() intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
} else { } else {
intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount*config.Opts.SchedulerEntryFrequencyFactor()))) intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount*config.Opts.SchedulerEntryFrequencyFactor())))
intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval()))) intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval())))
intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval()))) intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval())))
} }
f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
default: default:
f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(config.Opts.SchedulerRoundRobinMinInterval())) intervalMinutes = config.Opts.SchedulerRoundRobinMinInterval()
} }
// If the feed has a TTL defined, we use it to make sure we don't check it too often.
if newTTL > intervalMinutes && newTTL > 0 {
intervalMinutes = newTTL
}
f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
} }
// FeedCreationRequest represents the request to create a feed. // FeedCreationRequest represents the request to create a feed.

View File

@ -72,7 +72,8 @@ func TestFeedScheduleNextCheckDefault(t *testing.T) {
feed := &Feed{} feed := &Feed{}
weeklyCount := 10 weeklyCount := 10
feed.ScheduleNextCheck(weeklyCount) newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)
if feed.NextCheckAt.IsZero() { if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`) t.Error(`The next_check_at must be set`)
@ -97,7 +98,8 @@ func TestFeedScheduleNextCheckRoundRobinMinInterval(t *testing.T) {
} }
feed := &Feed{} feed := &Feed{}
weeklyCount := 100 weeklyCount := 100
feed.ScheduleNextCheck(weeklyCount) newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)
if feed.NextCheckAt.IsZero() { if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`) t.Error(`The next_check_at must be set`)
@ -124,7 +126,8 @@ func TestFeedScheduleNextCheckEntryCountBasedMaxInterval(t *testing.T) {
} }
feed := &Feed{} feed := &Feed{}
weeklyCount := maxInterval * 100 weeklyCount := maxInterval * 100
feed.ScheduleNextCheck(weeklyCount) newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)
if feed.NextCheckAt.IsZero() { if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`) t.Error(`The next_check_at must be set`)
@ -151,7 +154,8 @@ func TestFeedScheduleNextCheckEntryCountBasedMinInterval(t *testing.T) {
} }
feed := &Feed{} feed := &Feed{}
weeklyCount := minInterval / 2 weeklyCount := minInterval / 2
feed.ScheduleNextCheck(weeklyCount) newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)
if feed.NextCheckAt.IsZero() { if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`) t.Error(`The next_check_at must be set`)
@ -176,7 +180,8 @@ func TestFeedScheduleNextCheckEntryFrequencyFactor(t *testing.T) {
} }
feed := &Feed{} feed := &Feed{}
weeklyCount := 7 weeklyCount := 7
feed.ScheduleNextCheck(weeklyCount) newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)
if feed.NextCheckAt.IsZero() { if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`) t.Error(`The next_check_at must be set`)
@ -186,3 +191,69 @@ func TestFeedScheduleNextCheckEntryFrequencyFactor(t *testing.T) {
t.Error(`The next_check_at should not be after the now + factor * count`) t.Error(`The next_check_at should not be after the now + factor * count`)
} }
} }
func TestFeedScheduleNextCheckEntryFrequencySmallNewTTL(t *testing.T) {
// If the feed has a TTL defined, we use it to make sure we don't check it too often.
maxInterval := 500
minInterval := 100
os.Clearenv()
os.Setenv("POLLING_SCHEDULER", "entry_frequency")
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL", fmt.Sprintf("%d", maxInterval))
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MIN_INTERVAL", fmt.Sprintf("%d", minInterval))
var err error
parser := config.NewParser()
config.Opts, err = parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing failure: %v`, err)
}
feed := &Feed{}
weeklyCount := minInterval / 2
// TTL is smaller than minInterval.
newTTL := minInterval / 2
feed.ScheduleNextCheck(weeklyCount, newTTL)
if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
}
if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(minInterval))) {
t.Error(`The next_check_at should not be before the now + min interval`)
}
if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(newTTL))) {
t.Error(`The next_check_at should not be before the now + TTL`)
}
}
func TestFeedScheduleNextCheckEntryFrequencyLargeNewTTL(t *testing.T) {
// If the feed has a TTL defined, we use it to make sure we don't check it too often.
maxInterval := 500
minInterval := 100
os.Clearenv()
os.Setenv("POLLING_SCHEDULER", "entry_frequency")
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL", fmt.Sprintf("%d", maxInterval))
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MIN_INTERVAL", fmt.Sprintf("%d", minInterval))
var err error
parser := config.NewParser()
config.Opts, err = parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing failure: %v`, err)
}
feed := &Feed{}
// TTL is larger than minInterval.
weeklyCount := minInterval / 2
newTTL := minInterval * 2
feed.ScheduleNextCheck(weeklyCount, newTTL)
if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
}
if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(minInterval))) {
t.Error(`The next_check_at should not be before the now + min interval`)
}
if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(newTTL))) {
t.Error(`The next_check_at should not be before the now + TTL`)
}
}

View File

@ -7,7 +7,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"log/slog" "log/slog"
"time"
"miniflux.app/v2/internal/config" "miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/integration" "miniflux.app/v2/internal/integration"
@ -217,6 +216,7 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
} }
weeklyEntryCount := 0 weeklyEntryCount := 0
newTTL := 0
if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency { if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
var weeklyCountErr error var weeklyCountErr error
weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID) weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
@ -226,7 +226,7 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
} }
originalFeed.CheckedNow() originalFeed.CheckedNow()
originalFeed.ScheduleNextCheck(weeklyEntryCount) originalFeed.ScheduleNextCheck(weeklyEntryCount, newTTL)
requestBuilder := fetcher.NewRequestBuilder() requestBuilder := fetcher.NewRequestBuilder()
requestBuilder.WithUsernameAndPassword(originalFeed.Username, originalFeed.Password) requestBuilder.WithUsernameAndPassword(originalFeed.Username, originalFeed.Password)
@ -282,26 +282,15 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
} }
// If the feed has a TTL defined, we use it to make sure we don't check it too often. // If the feed has a TTL defined, we use it to make sure we don't check it too often.
if updatedFeed.TTL > 0 { newTTL = updatedFeed.TTL
minNextCheckAt := time.Now().Add(time.Minute * time.Duration(updatedFeed.TTL)) // Set the next check at with updated arguments.
slog.Debug("Feed TTL", originalFeed.ScheduleNextCheck(weeklyEntryCount, newTTL)
slog.Int64("user_id", userID), slog.Debug("Updated next check date",
slog.Int64("feed_id", feedID), slog.Int64("user_id", userID),
slog.Int("ttl", updatedFeed.TTL), slog.Int64("feed_id", feedID),
slog.Time("next_check_at", originalFeed.NextCheckAt), slog.Int("ttl", newTTL),
) slog.Time("new_next_check_at", originalFeed.NextCheckAt),
)
if originalFeed.NextCheckAt.IsZero() || originalFeed.NextCheckAt.Before(minNextCheckAt) {
slog.Debug("Updating next check date based on TTL",
slog.Int64("user_id", userID),
slog.Int64("feed_id", feedID),
slog.Int("ttl", updatedFeed.TTL),
slog.Time("new_next_check_at", minNextCheckAt),
slog.Time("old_next_check_at", originalFeed.NextCheckAt),
)
originalFeed.NextCheckAt = minNextCheckAt
}
}
originalFeed.Entries = updatedFeed.Entries originalFeed.Entries = updatedFeed.Entries
processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh) processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh)