Files
Evection/apps/feeds/models.py
T
ksmithandClaude Sonnet 5 b7d9ee672c Add an atomic per-feed claim to poll_due_feeds (T1)
poll_due_feeds scanned for due feeds and dispatched fetch_feed without
marking them claimed, so overlapping beat ticks or a slow-running
fetch could dispatch the same feed twice, racing on ETag/status/error-
count writes. Feed.claimed_at is now set via an UPDATE ... WHERE
claimed_at IS NULL OR claimed_at < stale-cutoff, so only one caller's
UPDATE can ever match a given row; fetch_feed clears it when done.
A claim older than CLAIM_STALE_MINUTES is treated as abandoned so a
worker dying mid-fetch doesn't wedge the feed forever.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:03:18 -06:00

79 lines
2.9 KiB
Python

from django.conf import settings
from django.db import models
class Feed(models.Model):
"""A single polled RSS/Atom source, shared across every subscriber.
One row per feed_url regardless of how many users subscribe to it — the
fetch pipeline polls each feed exactly once and every Subscription below
just points at the same Feed/Article data. Never add per-user fetching.
"""
STATUS_PENDING = "pending"
STATUS_OK = "ok"
STATUS_NOT_MODIFIED = "not_modified"
STATUS_ERROR = "error"
STATUS_CHOICES = [
(STATUS_PENDING, "Pending"),
(STATUS_OK, "OK"),
(STATUS_NOT_MODIFIED, "Not modified"),
(STATUS_ERROR, "Error"),
]
feed_url = models.URLField(max_length=1000, unique=True)
site_url = models.URLField(max_length=1000, blank=True)
title = models.CharField(max_length=500, blank=True)
favicon_url = models.URLField(max_length=1000, blank=True)
etag = models.CharField(max_length=255, blank=True)
last_modified = models.CharField(max_length=255, blank=True)
last_fetched_at = models.DateTimeField(null=True, blank=True)
last_fetch_status = models.CharField(
max_length=20, choices=STATUS_CHOICES, default=STATUS_PENDING
)
last_error_message = models.TextField(blank=True)
# Instance-wide, not a per-user preference — see class docstring.
fetch_interval_minutes = models.PositiveIntegerField(default=60)
fetch_error_count = models.PositiveIntegerField(default=0)
is_active = models.BooleanField(default=True)
# Set by poll_due_feeds' atomic claim (an UPDATE ... WHERE clause, not
# select_for_update — Celery Beat and fetch_feed run in separate
# transactions) and cleared once fetch_feed finishes, so overlapping
# beat ticks or a fetch that runs long can't both dispatch the same
# feed. A claim older than CLAIM_STALE_MINUTES (tasks.py) is treated as
# abandoned and can be reclaimed, so a worker dying mid-fetch doesn't
# wedge the feed forever.
claimed_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title or self.feed_url
class Subscription(models.Model):
"""Per-user join between a User and a shared Feed."""
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="subscriptions"
)
feed = models.ForeignKey(Feed, on_delete=models.CASCADE, related_name="subscriptions")
custom_title = models.CharField(max_length=500, blank=True)
order = models.PositiveIntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = [("user", "feed")]
ordering = ["order", "id"]
def __str__(self):
return self.custom_title or self.feed.title or self.feed.feed_url
@property
def display_title(self):
return self.custom_title or self.feed.title or self.feed.feed_url