Enqueue the initial feed fetch instead of running it inline (T3)

add_feed ran fetch_and_ingest synchronously during the POST request, so
a slow or large feed tied up a Gunicorn worker for the duration of the
fetch (up to the 15s HTTP timeout, plus parse/ingest time) and a user
could repeat it with distinct URLs to reduce request capacity.

The subscription is now created synchronously as before, but the first
fetch is dispatched via fetch_feed.delay() — the same task the periodic
poll uses, so the save/error bookkeeping is unchanged. The user sees an
immediate redirect with a message that the fetch is in progress rather
than waiting on it in the request/response cycle.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 14:44:09 -06:00
1 parent 7bb981b545
commit 0bfe9c8076
2 files changed
+19 -22

No files matched your search

+11 -19
View File
@@ -12,8 +12,7 @@ from apps.articles.models import SubscriptionTag, Tag
from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm
from .models import Feed, Subscription
from .opml import export_opml, extract_opml_bytes, import_opml, parse_opml
from .services.fetcher import fetch_and_ingest
from .tasks import import_opml_task
from .tasks import fetch_feed, import_opml_task
OPML_ASYNC_THRESHOLD = 50
@@ -39,23 +38,16 @@ def add_feed(request):
user=request.user, subscription=subscription, tag=tag
)
# Fetch synchronously so the user sees results immediately; the
# periodic Celery poll takes over from here for subsequent fetches.
result = fetch_and_ingest(feed)
feed.save()
if result.status == Feed.STATUS_ERROR:
messages.warning(
request,
f"Subscribed to {feed_url}, but the initial fetch failed: {result.error_message}",
)
else:
article_count = len(result.new_article_ids)
messages.success(
request,
f"Subscribed to '{subscription.display_title}' — "
f"{article_count} article(s) fetched.",
)
# Enqueue the initial fetch instead of running it inline (T3) — a
# slow or large feed would otherwise tie up a Gunicorn worker for
# the duration of the request; fetch_feed handles the save/error
# bookkeeping the same way the periodic poll's dispatch does.
fetch_feed.delay(feed.pk)
messages.success(
request,
f"Subscribed to {feed_url}. Fetching its articles now — "
"refresh in a few seconds if you don't see them yet.",
)
return redirect("reader:stream")
return render(request, "feeds/add_feed.html", {"form": form})