Fix news scraper feed discovery + summarizer startup race
Some checks failed
build-and-deploy / build (push) Failing after 4s

The vendored spider only started on urls.txt lines containing '/rss' or
'/feed' — but the curated urls.txt holds 200 homepages, so the crawler
matched ZERO feeds and silently did nothing (2ms, 0 items). Rewrite the
spider with feed autodiscovery: fetch each homepage, find its
<link rel="alternate" type="application/rss+xml"> (or /feed|/rss link),
parse the feed, then follow each item to extract the article. Bump
DEPTH_LIMIT 1->3 (homepage -> feed -> article).

Also make the summarizer resilient to booting before the app container has
run alembic (docker-compose only guarantees `db` is up): add idempotent
ensure_tables() mirroring the scraper's CREATE TABLE IF NOT EXISTS.
This commit is contained in:
Sirius DevOps 2026-08-24 17:42:06 -04:00
commit 5849d72299
3 changed files with 130 additions and 28 deletions

View file

@ -203,6 +203,40 @@ def build_futures_context() -> str:
# ── DB helpers ─────────────────────────────────────────────────────────────
def ensure_tables() -> None:
"""Idempotently create the news tables if missing.
Normally created by alembic 003_news when the app container starts, but
this summarizer may boot before the app has run migrations (compose only
guarantees `db` is up, not that alembic has run). Mirrors the scraper
pipeline's own CREATE TABLE IF NOT EXISTS so either start order is safe.
"""
ddl = """
CREATE TABLE IF NOT EXISTS articles (
id SERIAL PRIMARY KEY,
title TEXT,
url TEXT UNIQUE,
content TEXT,
domain TEXT,
timestamp TIMESTAMPTZ
);
CREATE TABLE IF NOT EXISTS article_summaries (
id SERIAL PRIMARY KEY,
summary_text TEXT NOT NULL,
batch_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
"""
try:
conn = psycopg2.connect(**DB_CONFIG)
cur = conn.cursor()
cur.execute(ddl)
conn.commit()
cur.close()
conn.close()
except Exception as exc: # noqa: BLE001
logger.error("Error ensuring news tables: %s", exc)
def get_recent_news() -> list[dict]:
"""Fetch articles from the last SUMMARY_WINDOW_HOURS (content > 100 chars)."""
query = """
@ -274,6 +308,7 @@ def build_master_prompt(final_input: str) -> str:
def summarize_news() -> None:
"""Map-reduce summarize recent articles and store the master summary."""
ensure_tables()
articles = get_recent_news()
if not articles:
logger.info("No new articles found in the last %sh.", SUMMARY_WINDOW_HOURS)