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
+16 -19

No files matched your search

+8 -3
View File
@@ -54,7 +54,11 @@ GOOD_FEED_XML = """<?xml version="1.0"?>
@pytest.mark.django_db @pytest.mark.django_db
@responses.activate @responses.activate
def test_add_feed_view_subscribes_and_fetches_immediately(client, two_users): def test_add_feed_view_subscribes_and_enqueues_the_initial_fetch(client, two_users):
"""The subscription is created synchronously; the first fetch is
dispatched via fetch_feed.delay() (T3) rather than run inline — in
tests CELERY_TASK_ALWAYS_EAGER still runs it immediately, so the
article it ingests is visible by the time the response returns."""
alice, _ = two_users alice, _ = two_users
client.login(username="alice", password="pw-alice-123") client.login(username="alice", password="pw-alice-123")
responses.add(responses.GET, "https://example.com/feed.xml", body=GOOD_FEED_XML, status=200) responses.add(responses.GET, "https://example.com/feed.xml", body=GOOD_FEED_XML, status=200)
@@ -62,9 +66,10 @@ def test_add_feed_view_subscribes_and_fetches_immediately(client, two_users):
response = client.post(reverse("feeds:add_feed"), {"feed_url": "https://example.com/feed.xml"}) response = client.post(reverse("feeds:add_feed"), {"feed_url": "https://example.com/feed.xml"})
assert response.status_code == 302 assert response.status_code == 302
assert Subscription.objects.filter( subscription = Subscription.objects.get(
user=alice, feed__feed_url="https://example.com/feed.xml" user=alice, feed__feed_url="https://example.com/feed.xml"
).exists() )
assert subscription.feed.articles.filter(guid="guid-1").exists()
@pytest.mark.django_db @pytest.mark.django_db
+8 -16
View File
@@ -12,8 +12,7 @@ from apps.articles.models import SubscriptionTag, Tag
from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm
from .models import Feed, Subscription from .models import Feed, Subscription
from .opml import export_opml, extract_opml_bytes, import_opml, parse_opml from .opml import export_opml, extract_opml_bytes, import_opml, parse_opml
from .services.fetcher import fetch_and_ingest from .tasks import fetch_feed, import_opml_task
from .tasks import import_opml_task
OPML_ASYNC_THRESHOLD = 50 OPML_ASYNC_THRESHOLD = 50
@@ -39,22 +38,15 @@ def add_feed(request):
user=request.user, subscription=subscription, tag=tag user=request.user, subscription=subscription, tag=tag
) )
# Fetch synchronously so the user sees results immediately; the # Enqueue the initial fetch instead of running it inline (T3) — a
# periodic Celery poll takes over from here for subsequent fetches. # slow or large feed would otherwise tie up a Gunicorn worker for
result = fetch_and_ingest(feed) # the duration of the request; fetch_feed handles the save/error
feed.save() # bookkeeping the same way the periodic poll's dispatch does.
fetch_feed.delay(feed.pk)
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( messages.success(
request, request,
f"Subscribed to '{subscription.display_title}' — " f"Subscribed to {feed_url}. Fetching its articles now — "
f"{article_count} article(s) fetched.", "refresh in a few seconds if you don't see them yet.",
) )
return redirect("reader:stream") return redirect("reader:stream")