Files
Evection/apps/feeds/tasks.py
T
ksmithandClaude Sonnet 5 d200ab80f7 Add core data models and the feed fetch pipeline
Implements Folder/Feed/Subscription (feeds app) and Article/
UserArticleState/Tag/ArticleTag (articles app), preserving the
shared-Feed/shared-Article dedup design: a feed is fetched once
regardless of subscriber count, and per-user read/star/save/hide state
is created lazily rather than eagerly per subscriber.

Adds the fetch pipeline: feedparser-based parsing with conditional GET
(ETag/Last-Modified), nh3 HTML sanitization, guid-based dedup with
content-hash change detection that never resets a user's read/star
state, and exponential backoff on fetch errors respecting a configured
minimum interval floor. Wired into Celery via poll_due_feeds/fetch_feed
tasks, plus a minimal "add feed by URL" view that fetches synchronously
so new subscribers see results immediately.

Verified live against a real feed (hnrss.org) for ingestion, 304
dedup, and error backoff, in addition to the unit/integration test
suite (28 tests, all passing).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 16:03:20 -06:00

70 lines
2.0 KiB
Python

import logging
from datetime import timedelta
from celery import shared_task
from django.utils import timezone
from .models import Feed
from .services.fetcher import effective_interval_minutes, fetch_and_ingest
logger = logging.getLogger(__name__)
@shared_task
def poll_due_feeds():
"""Find every active Feed whose polling interval has elapsed and
dispatch a fetch_feed task for each — fan-out happens here, not via one
Celery Beat entry per feed.
"""
due_feed_ids = [
feed.id
for feed in Feed.objects.filter(is_active=True).only(
"id", "last_fetched_at", "fetch_interval_minutes", "fetch_error_count"
)
if _is_due(feed)
]
for feed_id in due_feed_ids:
fetch_feed.delay(feed_id)
return len(due_feed_ids)
def _is_due(feed: Feed) -> bool:
if feed.last_fetched_at is None:
return True
interval = effective_interval_minutes(feed)
return feed.last_fetched_at <= timezone.now() - timedelta(minutes=interval)
@shared_task
def fetch_feed(feed_id: int):
try:
feed = Feed.objects.get(pk=feed_id, is_active=True)
except Feed.DoesNotExist:
return None
result = fetch_and_ingest(feed)
feed.save()
if result.new_article_ids:
logger.info(
"Feed %s: %d new article(s), %d updated",
feed.feed_url,
len(result.new_article_ids),
len(result.updated_article_ids),
)
# Rule application (tag/star/hide/mark_read on new articles for each
# subscriber) is wired in once apps.rules exists — see milestone 6.
return {
"status": result.status,
"new": len(result.new_article_ids),
"updated": len(result.updated_article_ids),
}
@shared_task
def reset_feed_backoff(feed_id: int):
"""Admin-triggered: clear a feed's error count so it's retried at its
normal configured interval instead of the backed-off one."""
Feed.objects.filter(pk=feed_id).update(fetch_error_count=0)