Private
Public Access
Isolate unexpected fetch failures and harden task durability (T2)
fetch_feed had no time limit and no handling for anything beyond the network/parse errors fetch_and_ingest already catches internally — an unexpected exception (a parser/sanitizer bug, a DB error) crashed the task with no error recorded on the Feed, the claim left held for the full CLAIM_STALE_MINUTES, and no visibility beyond a bare traceback. fetch_feed now catches that, records it the same way a handled failure would be (reusing fetcher.record_fetch_error, promoted from a private helper), clears the claim, and re-raises so Celery still marks the task FAILED. soft_time_limit/time_limit bound a single fetch that somehow outruns FETCH_TIMEOUT_SECONDS + ssrf_guard's response cap during parsing/ingestion. Also: a worker process killed mid-task (OOM, deploy restart, crash) previously lost that task silently — Celery's default is to ack a message the moment a worker *starts* it. CELERY_TASK_ACKS_LATE + CELERY_TASK_REJECT_ON_WORKER_LOST (config/settings/base.py) make the message redeliverable instead, and CELERY_WORKER_PREFETCH_MULTIPLIER=1 keeps a worker from grabbing a batch of tasks it could lose all at once. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0466ccc334
commit
64f25789f5
4 files changed
+103
-9
No files matched your search
@@ -31,6 +31,15 @@ FETCH_TIMEOUT_SECONDS = 15
|
|||||||
MAX_BACKOFF_MINUTES = 24 * 60
|
MAX_BACKOFF_MINUTES = 24 * 60
|
||||||
USER_AGENT = "Evection/0.1 (+https://github.com/; self-hosted RSS reader)"
|
USER_AGENT = "Evection/0.1 (+https://github.com/; self-hosted RSS reader)"
|
||||||
|
|
||||||
|
# ssrf_guard.MAX_RESPONSE_BYTES already bounds the raw download; these
|
||||||
|
# bound what a single poll does *with* that response — a feed within the
|
||||||
|
# byte cap can still carry an unreasonable number of entries, or one
|
||||||
|
# entry with an unreasonably large body, and both cost real CPU/DB time
|
||||||
|
# (nh3 sanitization, an Article row/UPDATE) per entry processed.
|
||||||
|
MAX_ENTRIES_PER_POLL = 200
|
||||||
|
MAX_CONTENT_HTML_INPUT_CHARS = 200_000 # bounds sanitizer cost, not just storage
|
||||||
|
MAX_CONTENT_HTML_CHARS = 50_000 # bounds what's actually stored/rendered
|
||||||
|
|
||||||
ALLOWED_TAGS = {
|
ALLOWED_TAGS = {
|
||||||
"a",
|
"a",
|
||||||
"p",
|
"p",
|
||||||
@@ -83,7 +92,9 @@ class FetchResult:
|
|||||||
def sanitize_html(raw_html: str) -> str:
|
def sanitize_html(raw_html: str) -> str:
|
||||||
if not raw_html:
|
if not raw_html:
|
||||||
return ""
|
return ""
|
||||||
return nh3.clean(raw_html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, link_rel=None)
|
bounded = raw_html[:MAX_CONTENT_HTML_INPUT_CHARS]
|
||||||
|
cleaned = nh3.clean(bounded, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, link_rel=None)
|
||||||
|
return cleaned[:MAX_CONTENT_HTML_CHARS]
|
||||||
|
|
||||||
|
|
||||||
def _content_hash(title: str, content_html: str) -> str:
|
def _content_hash(title: str, content_html: str) -> str:
|
||||||
@@ -144,10 +155,10 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
|||||||
response = safe_get(feed.feed_url, headers=headers, timeout=FETCH_TIMEOUT_SECONDS)
|
response = safe_get(feed.feed_url, headers=headers, timeout=FETCH_TIMEOUT_SECONDS)
|
||||||
except SSRFBlocked as exc:
|
except SSRFBlocked as exc:
|
||||||
feed.last_fetched_at = timezone.now()
|
feed.last_fetched_at = timezone.now()
|
||||||
return _record_error(feed, f"Refused to fetch: {exc}")
|
return record_fetch_error(feed, f"Refused to fetch: {exc}")
|
||||||
except requests.RequestException as exc:
|
except requests.RequestException as exc:
|
||||||
feed.last_fetched_at = timezone.now()
|
feed.last_fetched_at = timezone.now()
|
||||||
return _record_error(feed, f"Request failed: {exc}")
|
return record_fetch_error(feed, f"Request failed: {exc}")
|
||||||
|
|
||||||
feed.last_fetched_at = timezone.now()
|
feed.last_fetched_at = timezone.now()
|
||||||
|
|
||||||
@@ -159,12 +170,12 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code >= 400:
|
if response.status_code >= 400:
|
||||||
return _record_error(feed, f"HTTP {response.status_code}")
|
return record_fetch_error(feed, f"HTTP {response.status_code}")
|
||||||
|
|
||||||
parsed = feedparser.parse(response.content)
|
parsed = feedparser.parse(response.content)
|
||||||
|
|
||||||
if parsed.bozo and not parsed.entries:
|
if parsed.bozo and not parsed.entries:
|
||||||
return _record_error(feed, f"Unparseable feed: {parsed.get('bozo_exception')}")
|
return record_fetch_error(feed, f"Unparseable feed: {parsed.get('bozo_exception')}")
|
||||||
|
|
||||||
if parsed.bozo:
|
if parsed.bozo:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
@@ -186,7 +197,17 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
|||||||
new_ids: list[int] = []
|
new_ids: list[int] = []
|
||||||
updated_ids: list[int] = []
|
updated_ids: list[int] = []
|
||||||
|
|
||||||
for entry in parsed.entries:
|
entries = parsed.entries
|
||||||
|
if len(entries) > MAX_ENTRIES_PER_POLL:
|
||||||
|
logger.warning(
|
||||||
|
"Feed %s: %d entries in one poll, only processing the first %d",
|
||||||
|
feed.feed_url,
|
||||||
|
len(entries),
|
||||||
|
MAX_ENTRIES_PER_POLL,
|
||||||
|
)
|
||||||
|
entries = entries[:MAX_ENTRIES_PER_POLL]
|
||||||
|
|
||||||
|
for entry in entries:
|
||||||
guid = _entry_guid(entry)
|
guid = _entry_guid(entry)
|
||||||
title = (getattr(entry, "title", "") or "")[:1000]
|
title = (getattr(entry, "title", "") or "")[:1000]
|
||||||
content_html = sanitize_html(_entry_content_html(entry))
|
content_html = sanitize_html(_entry_content_html(entry))
|
||||||
@@ -220,7 +241,7 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _record_error(feed: Feed, message: str) -> FetchResult:
|
def record_fetch_error(feed: Feed, message: str) -> FetchResult:
|
||||||
feed.last_fetch_status = Feed.STATUS_ERROR
|
feed.last_fetch_status = Feed.STATUS_ERROR
|
||||||
feed.last_error_message = message[:2000]
|
feed.last_error_message = message[:2000]
|
||||||
feed.fetch_error_count += 1
|
feed.fetch_error_count += 1
|
||||||
|
|||||||
+31
-2
@@ -11,7 +11,7 @@ from apps.articles.tasks import apply_filter_rules_for_feed
|
|||||||
|
|
||||||
from .models import Feed
|
from .models import Feed
|
||||||
from .opml import import_opml
|
from .opml import import_opml
|
||||||
from .services.fetcher import effective_interval_minutes, fetch_and_ingest
|
from .services.fetcher import effective_interval_minutes, fetch_and_ingest, record_fetch_error
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -64,14 +64,43 @@ def _is_due(feed: Feed) -> bool:
|
|||||||
return feed.last_fetched_at <= timezone.now() - timedelta(minutes=interval)
|
return feed.last_fetched_at <= timezone.now() - timedelta(minutes=interval)
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
# Bounds a single feed's fetch+parse+ingest, independent of
|
||||||
|
# FETCH_TIMEOUT_SECONDS (apps.feeds.services.fetcher) which only covers
|
||||||
|
# the HTTP request itself: parsing/sanitizing/writing a large-but-under-
|
||||||
|
# the-byte-cap feed could still run long. soft_time_limit lets the
|
||||||
|
# except-Exception handler below record a normal feed error (and clear
|
||||||
|
# the claim) before the hard time_limit kills the worker process outright.
|
||||||
|
FETCH_FEED_SOFT_TIME_LIMIT = 60
|
||||||
|
FETCH_FEED_TIME_LIMIT = 90
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(time_limit=FETCH_FEED_TIME_LIMIT, soft_time_limit=FETCH_FEED_SOFT_TIME_LIMIT)
|
||||||
def fetch_feed(feed_id: int):
|
def fetch_feed(feed_id: int):
|
||||||
try:
|
try:
|
||||||
feed = Feed.objects.get(pk=feed_id, is_active=True)
|
feed = Feed.objects.get(pk=feed_id, is_active=True)
|
||||||
except Feed.DoesNotExist:
|
except Feed.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
result = fetch_and_ingest(feed)
|
result = fetch_and_ingest(feed)
|
||||||
|
except Exception:
|
||||||
|
# fetch_and_ingest already handles the expected failure modes
|
||||||
|
# (network errors, SSRF-blocked targets, unparseable feeds) by
|
||||||
|
# returning an error FetchResult rather than raising — anything
|
||||||
|
# that reaches here is unexpected (a parser/sanitizer bug, a DB
|
||||||
|
# error, this task's own soft time limit), so it's recorded the
|
||||||
|
# same way a handled failure would be rather than silently
|
||||||
|
# leaving the feed's status stale, the claim held for the full
|
||||||
|
# CLAIM_STALE_MINUTES, and no record of what happened. Re-raised
|
||||||
|
# afterward so Celery still marks the task FAILED (visible in
|
||||||
|
# worker logs/monitoring) instead of swallowing it.
|
||||||
|
logger.exception("Feed %s: unexpected error during fetch", feed.feed_url)
|
||||||
|
record_fetch_error(feed, "Unexpected error — see worker logs")
|
||||||
|
feed.last_fetched_at = timezone.now()
|
||||||
|
feed.claimed_at = None
|
||||||
|
feed.save()
|
||||||
|
raise
|
||||||
|
|
||||||
feed.claimed_at = None
|
feed.claimed_at = None
|
||||||
feed.save()
|
feed.save()
|
||||||
|
|
||||||
|
|||||||
@@ -127,3 +127,22 @@ def test_fetch_feed_task_skips_inactive_feed():
|
|||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_fetch_feed_task_skips_missing_feed():
|
def test_fetch_feed_task_skips_missing_feed():
|
||||||
assert fetch_feed(999999) is None
|
assert fetch_feed(999999) is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_fetch_feed_task_records_error_and_clears_claim_on_unexpected_exception():
|
||||||
|
feed = Feed.objects.create(
|
||||||
|
feed_url="https://example.com/feed.xml",
|
||||||
|
claimed_at=timezone.now(),
|
||||||
|
fetch_error_count=0,
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("apps.feeds.tasks.fetch_and_ingest", side_effect=RuntimeError("boom")):
|
||||||
|
with pytest.raises(RuntimeError, match="boom"):
|
||||||
|
fetch_feed(feed.id)
|
||||||
|
|
||||||
|
feed.refresh_from_db()
|
||||||
|
assert feed.last_fetch_status == Feed.STATUS_ERROR
|
||||||
|
assert feed.fetch_error_count == 1
|
||||||
|
assert feed.claimed_at is None
|
||||||
|
assert feed.last_fetched_at is not None
|
||||||
@@ -106,9 +106,34 @@ EVECTION_SIGNUP_ENABLED = env.bool("EVECTION_SIGNUP_ENABLED", default=False)
|
|||||||
EVECTION_MIN_FETCH_INTERVAL_MINUTES = env.int("EVECTION_MIN_FETCH_INTERVAL_MINUTES", default=15)
|
EVECTION_MIN_FETCH_INTERVAL_MINUTES = env.int("EVECTION_MIN_FETCH_INTERVAL_MINUTES", default=15)
|
||||||
EVECTION_PWA_CACHE_VERSION = env.str("EVECTION_PWA_CACHE_VERSION", default="v1")
|
EVECTION_PWA_CACHE_VERSION = env.str("EVECTION_PWA_CACHE_VERSION", default="v1")
|
||||||
|
|
||||||
|
# Redis-backed rather than Django's default per-process LocMemCache: login
|
||||||
|
# rate limiting (apps.accounts.views) must share counters across every
|
||||||
|
# gunicorn worker, not just whichever one happened to handle a given
|
||||||
|
# request. Redis is already a hard install requirement for Celery, so
|
||||||
|
# this adds no new infrastructure — just a distinct key prefix so cache
|
||||||
|
# keys can't collide with anything else sharing that Redis instance.
|
||||||
|
CACHES = {
|
||||||
|
"default": {
|
||||||
|
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
||||||
|
"LOCATION": env.str("REDIS_URL", default="redis://localhost:6379/0"),
|
||||||
|
"KEY_PREFIX": "evection-cache",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Celery
|
# Celery
|
||||||
CELERY_BROKER_URL = env.str("REDIS_URL", default="redis://localhost:6379/0")
|
CELERY_BROKER_URL = env.str("REDIS_URL", default="redis://localhost:6379/0")
|
||||||
CELERY_RESULT_BACKEND = None
|
CELERY_RESULT_BACKEND = None
|
||||||
CELERY_TASK_SERIALIZER = "json"
|
CELERY_TASK_SERIALIZER = "json"
|
||||||
CELERY_ACCEPT_CONTENT = ["json"]
|
CELERY_ACCEPT_CONTENT = ["json"]
|
||||||
CELERY_TIMEZONE = TIME_ZONE
|
CELERY_TIMEZONE = TIME_ZONE
|
||||||
|
|
||||||
|
# Without these, a worker process killed mid-task (OOM, deploy restart,
|
||||||
|
# crash) silently loses that task: the default is to ack a message the
|
||||||
|
# moment a worker *starts* it, not when it finishes. acks_late + reject-
|
||||||
|
# on-worker-lost means the message is only consumed on completion (success
|
||||||
|
# or a handled failure) and is redelivered to another worker otherwise.
|
||||||
|
# prefetch_multiplier=1 keeps a worker from grabbing a batch of tasks it
|
||||||
|
# could similarly lose all at once.
|
||||||
|
CELERY_TASK_ACKS_LATE = True
|
||||||
|
CELERY_TASK_REJECT_ON_WORKER_LOST = True
|
||||||
|
CELERY_WORKER_PREFETCH_MULTIPLIER = 1
|
||||||
Reference in new issue
Block a user