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
+104
-10
No files matched your search
@@ -31,6 +31,15 @@ FETCH_TIMEOUT_SECONDS = 15
|
||||
MAX_BACKOFF_MINUTES = 24 * 60
|
||||
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 = {
|
||||
"a",
|
||||
"p",
|
||||
@@ -83,7 +92,9 @@ class FetchResult:
|
||||
def sanitize_html(raw_html: str) -> str:
|
||||
if not raw_html:
|
||||
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:
|
||||
@@ -144,10 +155,10 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
||||
response = safe_get(feed.feed_url, headers=headers, timeout=FETCH_TIMEOUT_SECONDS)
|
||||
except SSRFBlocked as exc:
|
||||
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:
|
||||
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()
|
||||
|
||||
@@ -159,12 +170,12 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
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:
|
||||
logger.warning(
|
||||
@@ -186,7 +197,17 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
||||
new_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)
|
||||
title = (getattr(entry, "title", "") or "")[:1000]
|
||||
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_error_message = message[:2000]
|
||||
feed.fetch_error_count += 1
|
||||
|
||||
Reference in new issue
Block a user