From 50d1ef540d4f551c8dff962865e6dae47babd507 Mon Sep 17 00:00:00 2001 From: Robert Fletcher Date: Sun, 22 Nov 2026 16:59:01 -0800 Subject: [PATCH] Fix feeds stalling on a pinned first entry **What** Stop cutting the scan short when we reach the newest stored story. Check every entry in the feed instead, dropping ones older than three days, then drop the ones already stored using a single query (`StoryRepository.existing_entry_ids`, which replaces `exists?`). **Why** Some feeds pin an item to the top, above newer entries. Once that item was the newest stored story, every fetch stopped at the first entry and no new stories were ever added, while the feed still showed green. **Notes** Entries that were skipped while a feed was stuck and are now older than three days will not be picked up by this change. --- AGENTS.md | 2 +- app/commands/feed/fetch_one.rb | 6 +----- app/commands/feed/find_new_stories.rb | 19 +++++-------------- app/repositories/story_repository.rb | 4 ++-- spec/commands/feed/find_new_stories_spec.rb | 12 ++++++------ spec/repositories/story_repository_spec.rb | 16 ++++++++++++++++ 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0798b0a3d..a53d020e3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -30,7 +30,7 @@ Feed operations are exposed as rake tasks: `rake fetch_feeds`, `rake fetch_feed[ **Background jobs** funnel through a single `CallableJob`, which just calls any callable: `CallableJob.perform_later(Feed::FetchOne, feed)` enqueues `Feed::FetchOne.call(feed)`. There is rarely a need to write a new `ApplicationJob` subclass — enqueue a command via `CallableJob` instead. Jobs run on GoodJob (Postgres-backed). -**Feed fetching flow:** `Feed::FetchAll` → enqueues `Feed::FetchOne` per feed → `HTTParty` fetches, `Feedjira` parses → `Feed::FindNewStories` diffs against the latest stored entry → `StoryRepository.add`. Fetch failures set the feed status to `:red` (vs `:green`) and log rather than raise. +**Feed fetching flow:** `Feed::FetchAll` → enqueues `Feed::FetchOne` per feed → `HTTParty` fetches, `Feedjira` parses → `Feed::FindNewStories` drops entries that are too old or already stored → `StoryRepository.add`. Fetch failures set the feed status to `:red` (vs `:green`) and log rather than raise. **Admin routes** under `/admin` (GoodJob dashboard, settings, debug) are gated by `AdminConstraint` (`lib/admin_constraint.rb`). Stringer is single-user; auth is password-based (`bcrypt`). diff --git a/app/commands/feed/fetch_one.rb b/app/commands/feed/fetch_one.rb index e1c5e39da..094aef932 100644 --- a/app/commands/feed/fetch_one.rb +++ b/app/commands/feed/fetch_one.rb @@ -26,11 +26,7 @@ def fetch_raw_feed(feed) end def new_entries_from(feed, raw_feed) - Feed::FindNewStories.call(raw_feed, feed.id, latest_entry_id(feed)) - end - - def latest_entry_id(feed) - feed.stories.first.entry_id unless feed.stories.empty? + Feed::FindNewStories.call(raw_feed, feed.id) end end end diff --git a/app/commands/feed/find_new_stories.rb b/app/commands/feed/find_new_stories.rb index e46ea9ca1..510f411ec 100644 --- a/app/commands/feed/find_new_stories.rb +++ b/app/commands/feed/find_new_stories.rb @@ -3,23 +3,14 @@ module Feed::FindNewStories STORY_AGE_THRESHOLD_DAYS = 3 - def self.call(raw_feed, feed_id, latest_entry_id = nil) - stories = [] + def self.call(raw_feed, feed_id) + recent = raw_feed.entries.reject { |story| too_old?(story) } + existing_ids = StoryRepository.existing_entry_ids(recent.map(&:id), feed_id) - raw_feed.entries.each do |story| - break if latest_entry_id && story.id == latest_entry_id - next if story_age_exceeds_threshold?(story) || StoryRepository.exists?( - story.id, - feed_id - ) - - stories << story - end - - stories + recent.reject { |story| existing_ids.include?(story.id) } end - def self.story_age_exceeds_threshold?(story) + def self.too_old?(story) max_age = Time.now - STORY_AGE_THRESHOLD_DAYS.days story.published && story.published < max_age end diff --git a/app/repositories/story_repository.rb b/app/repositories/story_repository.rb index 69e15caf7..012bc6d6d 100644 --- a/app/repositories/story_repository.rb +++ b/app/repositories/story_repository.rb @@ -53,8 +53,8 @@ def self.fetch_unread_for_feed_by_timestamp(feed_id, timestamp) .where("created_at < ? AND is_read = ?", timestamp, false) end - def self.exists?(id, feed_id) - Story.exists?(entry_id: id, feed_id:) + def self.existing_entry_ids(entry_ids, feed_id) + Story.where(entry_id: entry_ids, feed_id:).pluck(:entry_id) end def self.unread(order: "desc") diff --git a/spec/commands/feed/find_new_stories_spec.rb b/spec/commands/feed/find_new_stories_spec.rb index 3cc645fad..38ee25478 100644 --- a/spec/commands/feed/find_new_stories_spec.rb +++ b/spec/commands/feed/find_new_stories_spec.rb @@ -40,13 +40,13 @@ def create_raw_feed(feed, entries: []) end end - it "scans until matching the last story id" do - new_entry = create_entry(published: Time.zone.now, url: "www.new.com") - old_entry = create_entry(title: "old entry", url: "www.old.com") - raw_feed = create_raw_feed(create(:feed), entries: [new_entry, old_entry]) - result = described_class.call(raw_feed, 1, "www.old.com") + it "finds new stories listed after an existing story" do + story = create(:story, entry_id: "www.pinned.com") + entries = [story.entry_id, "www.new.com"].map { |url| create_entry(url:) } + raw_feed = create_raw_feed(story.feed, entries:) - expect(result).to eq([raw_feed.entries.first]) + expect(described_class.call(raw_feed, story.feed_id)) + .to eq([raw_feed.entries.last]) end it "ignores stories older than 3 days" do diff --git a/spec/repositories/story_repository_spec.rb b/spec/repositories/story_repository_spec.rb index d4c5fff58..d39bc69b2 100644 --- a/spec/repositories/story_repository_spec.rb +++ b/spec/repositories/story_repository_spec.rb @@ -253,6 +253,22 @@ def create_feed(url: "http://blog.golang.org/feed.atom") end end + describe ".existing_entry_ids" do + it "returns the given entry ids that are stored for the feed" do + story = create(:story, entry_id: "stored") + ids = described_class.existing_entry_ids(["stored", "new"], story.feed_id) + + expect(ids).to eq(["stored"]) + end + + it "ignores entry ids stored for other feeds" do + create(:story, entry_id: "stored") + ids = described_class.existing_entry_ids(["stored"], create(:feed).id) + + expect(ids).to be_empty + end + end + describe ".unread" do it "returns unread stories ordered by published date descending" do story1 = create(:story, published: 5.minutes.ago)