diff --git a/apps/feeds/tests.py b/apps/feeds/tests.py index 2922760..73639e9 100644 --- a/apps/feeds/tests.py +++ b/apps/feeds/tests.py @@ -54,7 +54,11 @@ GOOD_FEED_XML = """ @pytest.mark.django_db @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 client.login(username="alice", password="pw-alice-123") 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"}) assert response.status_code == 302 - assert Subscription.objects.filter( + subscription = Subscription.objects.get( user=alice, feed__feed_url="https://example.com/feed.xml" - ).exists() + ) + assert subscription.feed.articles.filter(guid="guid-1").exists() @pytest.mark.django_db diff --git a/apps/feeds/views.py b/apps/feeds/views.py index 7fa77cd..9f53f95 100644 --- a/apps/feeds/views.py +++ b/apps/feeds/views.py @@ -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})