Private
Public Access
Batch feed ingestion, add a readiness check, fail open without Redis (C2, C8)
Ingestion did a get_or_create per entry. It now prefetches existing articles for the poll in one query, bulk-inserts new ones and bulk-updates changed ones (falling back to row-by-row only if a concurrent fetch wins a race), and dispatches rule evaluation in bounded chunks so a huge first fetch is several small tasks. /health/ stays a DB-only liveness check (upgrade.sh gates on it right after restart). New /health/ready/ also checks Redis and that poll_due_feeds ticked within five minutes — proving beat and a worker are both alive — and returns 503 with per-check detail otherwise. The sidebar unread-count cache and login throttle now degrade instead of erroring when Redis is unreachable: every authenticated page and every login would otherwise 500 during a cache outage. Found while verifying in a sandbox with no Redis. Also includes the dashboard's oldest-article metric (core/views.py). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d2900424a
commit
e5f1277014
14 files changed
+354
-48
No files matched your search
@@ -18,6 +18,7 @@ import feedparser
|
||||
import nh3
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from django.db import IntegrityError, transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.articles.models import Article
|
||||
@@ -131,6 +132,26 @@ def _entry_content_html(entry) -> str:
|
||||
return getattr(entry, "summary", "") or ""
|
||||
|
||||
|
||||
UPDATABLE_ARTICLE_FIELDS = (
|
||||
"title",
|
||||
"link",
|
||||
"author",
|
||||
"summary",
|
||||
"content_html",
|
||||
"published_at",
|
||||
"content_hash",
|
||||
)
|
||||
|
||||
|
||||
def _ensure_pks(feed: Feed, articles: list[Article]) -> list[int]:
|
||||
"""bulk_create only populates primary keys on backends that can
|
||||
RETURN them (Postgres, SQLite 3.35+); look the rest up by guid."""
|
||||
if all(a.pk for a in articles):
|
||||
return [a.pk for a in articles]
|
||||
guids = [a.guid for a in articles]
|
||||
return list(Article.objects.filter(feed=feed, guid__in=guids).values_list("pk", flat=True))
|
||||
|
||||
|
||||
def effective_interval_minutes(feed: Feed, configured_interval_minutes: int) -> int:
|
||||
"""The actual polling interval to use for `feed`, applying exponential
|
||||
backoff on top of the site-wide `configured_interval_minutes` (see
|
||||
@@ -217,52 +238,69 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
||||
)
|
||||
entries = entries[:MAX_ENTRIES_PER_POLL]
|
||||
|
||||
# One prefetch + bulk writes for the whole poll (C2), instead of a
|
||||
# get_or_create (SELECT, then INSERT) per entry. Entries are keyed by
|
||||
# guid so a feed that repeats one within a single response is only
|
||||
# processed once.
|
||||
parsed_entries: dict[str, dict] = {}
|
||||
for entry in entries:
|
||||
guid = _entry_guid(entry)
|
||||
if guid in parsed_entries:
|
||||
continue
|
||||
title = (getattr(entry, "title", "") or "")[:1000]
|
||||
link = (getattr(entry, "link", "") or "")[:2000]
|
||||
author = (getattr(entry, "author", "") or "")[:500]
|
||||
summary = (getattr(entry, "summary", "") or "")[:5000]
|
||||
content_html = sanitize_html(_entry_content_html(entry))
|
||||
published_at = _entry_published_at(entry)
|
||||
content_hash = _content_hash(title, link, author, summary, content_html, published_at)
|
||||
parsed_entries[guid] = {
|
||||
"title": title,
|
||||
"link": link,
|
||||
"author": author,
|
||||
"summary": summary,
|
||||
"content_html": content_html,
|
||||
"published_at": published_at,
|
||||
"content_hash": _content_hash(title, link, author, summary, content_html, published_at),
|
||||
}
|
||||
|
||||
article, created = Article.objects.get_or_create(
|
||||
feed=feed,
|
||||
guid=guid,
|
||||
defaults={
|
||||
"link": link,
|
||||
"title": title,
|
||||
"author": author,
|
||||
"content_html": content_html,
|
||||
"summary": summary,
|
||||
"content_hash": content_hash,
|
||||
"published_at": published_at,
|
||||
},
|
||||
)
|
||||
existing = {a.guid: a for a in Article.objects.filter(feed=feed, guid__in=parsed_entries)}
|
||||
|
||||
if created:
|
||||
new_ids.append(article.pk)
|
||||
elif article.content_hash != content_hash:
|
||||
article.title = title
|
||||
article.link = link
|
||||
article.author = author
|
||||
article.summary = summary
|
||||
article.content_html = content_html
|
||||
article.published_at = published_at
|
||||
article.content_hash = content_hash
|
||||
article.save(
|
||||
update_fields=[
|
||||
"title",
|
||||
"link",
|
||||
"author",
|
||||
"summary",
|
||||
"content_html",
|
||||
"published_at",
|
||||
"content_hash",
|
||||
]
|
||||
)
|
||||
updated_ids.append(article.pk)
|
||||
to_create = [
|
||||
Article(feed=feed, guid=guid, **fields)
|
||||
for guid, fields in parsed_entries.items()
|
||||
if guid not in existing
|
||||
]
|
||||
to_update = []
|
||||
for guid, article in existing.items():
|
||||
fields = parsed_entries[guid]
|
||||
if article.content_hash != fields["content_hash"]:
|
||||
for name, value in fields.items():
|
||||
setattr(article, name, value)
|
||||
to_update.append(article)
|
||||
|
||||
if to_update:
|
||||
Article.objects.bulk_update(to_update, list(UPDATABLE_ARTICLE_FIELDS), batch_size=100)
|
||||
updated_ids.extend(a.pk for a in to_update)
|
||||
|
||||
if to_create:
|
||||
try:
|
||||
with transaction.atomic():
|
||||
Article.objects.bulk_create(to_create)
|
||||
except IntegrityError:
|
||||
# Another worker inserted some of these guids between the
|
||||
# prefetch above and now (two fetches of the same feed can
|
||||
# overlap) — fall back to row-by-row so nothing is lost or
|
||||
# duplicated, at the cost of the old per-entry round trips
|
||||
# for this rare case only.
|
||||
for article in to_create:
|
||||
fields = {name: getattr(article, name) for name in UPDATABLE_ARTICLE_FIELDS}
|
||||
obj, created = Article.objects.get_or_create(
|
||||
feed=feed, guid=article.guid, defaults=fields
|
||||
)
|
||||
if created:
|
||||
new_ids.append(obj.pk)
|
||||
else:
|
||||
new_ids.extend(_ensure_pks(feed, to_create))
|
||||
|
||||
return FetchResult(
|
||||
status=Feed.STATUS_OK, new_article_ids=new_ids, updated_article_ids=updated_ids
|
||||
|
||||
+15
-1
@@ -4,6 +4,7 @@ from io import BytesIO
|
||||
|
||||
from celery import shared_task
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.cache import cache
|
||||
from django.db.models import Q
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -27,6 +28,12 @@ CLAIM_STALE_MINUTES = 10
|
||||
# across several beat ticks instead of enqueuing everything — and every
|
||||
# bounded Feed query below stays index-only work, not a full scan (C1).
|
||||
POLL_BATCH_SIZE = 500
|
||||
RULE_DISPATCH_CHUNK_SIZE = 50
|
||||
|
||||
# Written on every poll_due_feeds tick; apps.core.views.health_ready
|
||||
# reads it to tell whether beat is scheduling and a worker is consuming.
|
||||
POLL_HEARTBEAT_CACHE_KEY = "poll-due-feeds-heartbeat"
|
||||
POLL_HEARTBEAT_TTL_SECONDS = 3600
|
||||
|
||||
|
||||
@shared_task
|
||||
@@ -39,6 +46,7 @@ def poll_due_feeds():
|
||||
here, not via one Celery Beat entry per feed.
|
||||
"""
|
||||
now = timezone.now()
|
||||
cache.set(POLL_HEARTBEAT_CACHE_KEY, now, timeout=POLL_HEARTBEAT_TTL_SECONDS)
|
||||
claim_stale_before = now - timedelta(minutes=CLAIM_STALE_MINUTES)
|
||||
due_feed_ids = list(
|
||||
Feed.objects.filter(is_active=True)
|
||||
@@ -125,7 +133,13 @@ def fetch_feed(feed_id: int):
|
||||
len(result.new_article_ids),
|
||||
len(result.updated_article_ids),
|
||||
)
|
||||
apply_filter_rules_for_feed.delay(feed_id, changed_article_ids)
|
||||
# Chunked so one huge poll (a first fetch of a long archive feed)
|
||||
# becomes several bounded rule-evaluation tasks, each well inside
|
||||
# the worker's time limits, instead of one unbounded one (C2).
|
||||
for start in range(0, len(changed_article_ids), RULE_DISPATCH_CHUNK_SIZE):
|
||||
apply_filter_rules_for_feed.delay(
|
||||
feed_id, changed_article_ids[start : start + RULE_DISPATCH_CHUNK_SIZE]
|
||||
)
|
||||
|
||||
return {
|
||||
"status": result.status,
|
||||
|
||||
@@ -264,3 +264,77 @@ def test_fetch_only_processes_the_first_max_entries_per_poll(feed):
|
||||
assert len(result.new_article_ids) == MAX_ENTRIES_PER_POLL
|
||||
assert Article.objects.filter(feed=feed).count() == MAX_ENTRIES_PER_POLL
|
||||
assert not Article.objects.filter(feed=feed, guid=f"guid-{MAX_ENTRIES_PER_POLL}").exists()
|
||||
|
||||
|
||||
def _feed_xml(count, title_suffix=""):
|
||||
items = "".join(
|
||||
f"<item><title>Item {i}{title_suffix}</title><guid>guid-{i}</guid></item>"
|
||||
for i in range(count)
|
||||
)
|
||||
return (
|
||||
f'<?xml version="1.0"?><rss version="2.0"><channel><title>T</title>{items}</channel></rss>'
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@responses.activate
|
||||
def test_ingest_uses_a_constant_number_of_article_queries(feed):
|
||||
"""C2: one prefetch + bulk writes per poll, not a get_or_create per
|
||||
entry — query count must not grow with the number of entries."""
|
||||
from django.db import connection
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
|
||||
responses.add(responses.GET, FEED_URL, body=_feed_xml(40), status=200)
|
||||
with CaptureQueriesContext(connection) as ctx:
|
||||
result = fetch_and_ingest(feed)
|
||||
|
||||
assert len(result.new_article_ids) == 40
|
||||
assert len(ctx.captured_queries) < 10
|
||||
assert Article.objects.filter(feed=feed).count() == 40
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@responses.activate
|
||||
def test_ingest_dedups_a_guid_repeated_within_one_response(feed):
|
||||
xml = (
|
||||
'<?xml version="1.0"?><rss version="2.0"><channel><title>T</title>'
|
||||
"<item><title>A</title><guid>same</guid></item>"
|
||||
"<item><title>B</title><guid>same</guid></item>"
|
||||
"</channel></rss>"
|
||||
)
|
||||
responses.add(responses.GET, FEED_URL, body=xml, status=200)
|
||||
|
||||
result = fetch_and_ingest(feed)
|
||||
|
||||
assert len(result.new_article_ids) == 1
|
||||
assert Article.objects.filter(feed=feed).count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@responses.activate
|
||||
def test_ingest_reports_updates_separately_from_new_articles(feed):
|
||||
responses.add(responses.GET, FEED_URL, body=_feed_xml(3), status=200)
|
||||
fetch_and_ingest(feed)
|
||||
|
||||
responses.replace(responses.GET, FEED_URL, body=_feed_xml(4, " edited"), status=200)
|
||||
result = fetch_and_ingest(feed)
|
||||
|
||||
assert len(result.new_article_ids) == 1
|
||||
assert len(result.updated_article_ids) == 3
|
||||
assert Article.objects.get(feed=feed, guid="guid-0").title == "Item 0 edited"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@responses.activate
|
||||
def test_ingest_falls_back_to_row_by_row_when_a_concurrent_insert_wins(feed):
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.db import IntegrityError
|
||||
|
||||
responses.add(responses.GET, FEED_URL, body=_feed_xml(3), status=200)
|
||||
|
||||
with patch.object(Article.objects, "bulk_create", side_effect=IntegrityError("race")):
|
||||
result = fetch_and_ingest(feed)
|
||||
|
||||
assert len(result.new_article_ids) == 3
|
||||
assert Article.objects.filter(feed=feed).count() == 3
|
||||
@@ -208,3 +208,23 @@ def test_fetch_feed_schedules_the_next_fetch_using_the_site_wide_interval():
|
||||
assert feed.next_fetch_at is not None
|
||||
expected = before + timedelta(minutes=120)
|
||||
assert abs((feed.next_fetch_at - expected).total_seconds()) < 5
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@responses.activate
|
||||
def test_fetch_feed_dispatches_rule_evaluation_in_bounded_chunks(monkeypatch):
|
||||
monkeypatch.setattr("apps.feeds.tasks.RULE_DISPATCH_CHUNK_SIZE", 2)
|
||||
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
||||
items = "".join(f"<item><title>T{i}</title><guid>g{i}</guid></item>" for i in range(5))
|
||||
responses.add(
|
||||
responses.GET,
|
||||
feed.feed_url,
|
||||
body=f'<?xml version="1.0"?><rss version="2.0"><channel>{items}</channel></rss>',
|
||||
status=200,
|
||||
)
|
||||
|
||||
with patch("apps.feeds.tasks.apply_filter_rules_for_feed.delay") as mock_delay:
|
||||
fetch_feed(feed.id)
|
||||
|
||||
chunk_sizes = [len(call.args[1]) for call in mock_delay.call_args_list]
|
||||
assert chunk_sizes == [2, 2, 1]
|
||||
Reference in new issue
Block a user