M1: scrape spine — renderer, extractor, orchestrator /scrape, compose, health, render cache

- renderer: Node/Playwright, single browser + pooled contexts with concurrency
  cap and FIFO queue, Redis render cache (graceful no-op without Redis)
- extractor: FastAPI + Trafilatura, HTML -> {title, text, metadata}
- orchestrator: /scrape (render -> extract) with per-stage trace, aggregated
  /health, structured JSON logging on all services
- docker-compose for orchestrator/renderer/extractor/redis with healthchecks

Verified locally: end-to-end scrape of example.com and Wikipedia; error paths
for unreachable hosts and bad schemes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
User 2026-07-05 22:02:38 -07:00
parent 55317f3f75
commit e2b329b2bb
20 changed files with 1534 additions and 0 deletions

View file

@ -0,0 +1,54 @@
"""Orchestrator: the only externally exposed service (REST now, MCP in M4)."""
from contextlib import asynccontextmanager
import httpx
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from . import config, pipeline
from .logging_setup import setup
log = setup()
@asynccontextmanager
async def lifespan(app: FastAPI):
app.state.client = httpx.AsyncClient()
yield
await app.state.client.aclose()
app = FastAPI(title="LWSS Orchestrator", lifespan=lifespan)
class ScrapeRequest(BaseModel):
url: str
@app.get("/health")
async def health() -> dict:
"""Aggregate health across all downstream services."""
client: httpx.AsyncClient = app.state.client
services = {}
for name, base in [("renderer", config.RENDERER_URL), ("extractor", config.EXTRACTOR_URL)]:
try:
resp = await client.get(f"{base}/health", timeout=5)
services[name] = resp.json() if resp.status_code == 200 else {"status": "error"}
except httpx.HTTPError:
services[name] = {"status": "unreachable"}
overall = "ok" if all(s.get("status") == "ok" for s in services.values()) else "degraded"
return {"status": overall, "service": "orchestrator", "services": services}
@app.post("/scrape")
async def scrape(req: ScrapeRequest) -> dict:
if not req.url.startswith(("http://", "https://")):
raise HTTPException(status_code=400, detail="url must be http(s)")
try:
result = await pipeline.scrape_url(app.state.client, req.url)
except pipeline.StageError as exc:
log.error("scrape failed at %s: %s", exc.stage, exc.detail)
raise HTTPException(status_code=502, detail={"stage": exc.stage, "error": exc.detail}) from exc
log.info("scraped %s (%d chars)", req.url, len(result["content"]))
return result