fix(writer-pipeline): retry featured-image fetches on transient network errors
Tests / writer-pipeline tests (push) Failing after 55s
Tests / bridge tests (push) Successful in 1m45s
Tests / frontend lib tests (push) Successful in 1m44s
Tests / bridge tests (pull_request) Successful in 1m28s
Tests / frontend lib tests (pull_request) Successful in 1m56s
Tests / writer-pipeline tests (pull_request) Successful in 1m59s

Handball Planet draft publish failed with undici "fetch failed" on a single
cycle; ingest already retries but publish-path HTML/image GETs did not.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
José AlvesandCursor committed 2026-06-14 19:20:37 +00:00
1 parent 70cdc516dc
commit 6079046c5c
3 files changed
+51 -8

No files matched your search

+40
View File
@@ -7,6 +7,13 @@ export interface FetchTextOptions {
accept?: string; accept?: string;
} }
export interface FetchBinaryOptions {
timeoutMs?: number;
retries?: number;
userAgent?: string;
headers?: Record<string, string>;
}
function sleep(ms: number): Promise<void> { function sleep(ms: number): Promise<void> {
return new Promise((r) => setTimeout(r, ms)); return new Promise((r) => setTimeout(r, ms));
} }
@@ -43,3 +50,36 @@ export async function fetchTextWithRetry(
} }
throw lastErr instanceof Error ? lastErr : new Error(String(lastErr)); throw lastErr instanceof Error ? lastErr : new Error(String(lastErr));
} }
/**
* GET url as binary with bounded retries (transient network / 5xx).
* Caller validates status, content-type, and size.
*/
export async function fetchBinaryWithRetry(
url: string,
opts: FetchBinaryOptions = {},
): Promise<Response> {
const timeoutMs = opts.timeoutMs ?? 20_000;
const retries = Math.max(0, opts.retries ?? 2);
const ua = opts.userAgent ?? DEFAULT_UA;
const extraHeaders = opts.headers ?? {};
let lastErr: unknown;
for (let attempt = 0; attempt <= retries; attempt += 1) {
try {
const res = await fetch(url, {
redirect: 'follow',
headers: { 'User-Agent': ua, ...extraHeaders },
signal: AbortSignal.timeout(timeoutMs),
});
if (res.status >= 500 && attempt < retries) {
await sleep(400 * (attempt + 1));
continue;
}
return res;
} catch (e) {
lastErr = e;
if (attempt < retries) await sleep(500 * (attempt + 1));
}
}
throw lastErr instanceof Error ? lastErr : new Error(String(lastErr));
}
+6 -5
View File
@@ -1,3 +1,5 @@
import { fetchTextWithRetry } from './http-retry.js';
/** /**
* Raw HTML (capped) for OpenGraph / featured image extraction before publish. * Raw HTML (capped) for OpenGraph / featured image extraction before publish.
*/ */
@@ -7,12 +9,11 @@ export async function fetchArticleHtmlForMeta(
maxBytes = 600_000, maxBytes = 600_000,
): Promise<string | null> { ): Promise<string | null> {
try { try {
const res = await fetch(url, { const html = await fetchTextWithRetry(url, {
headers: { 'User-Agent': 'BitReport-news-writer-pipeline/1.0 (featured-image)' }, timeoutMs,
signal: AbortSignal.timeout(timeoutMs), retries: 2,
userAgent: 'BitReport-news-writer-pipeline/1.0 (featured-image)',
}); });
if (!res.ok) return null;
const html = await res.text();
return html.slice(0, maxBytes); return html.slice(0, maxBytes);
} catch { } catch {
return null; return null;
+5 -3
View File
@@ -1,6 +1,7 @@
import { Buffer } from 'node:buffer'; import { Buffer } from 'node:buffer';
import type { AggregatedItem } from './types.js'; import type { AggregatedItem } from './types.js';
import { fetchArticleHtmlForMeta } from './legacy-fetch.js'; import { fetchArticleHtmlForMeta } from './legacy-fetch.js';
import { fetchBinaryWithRetry } from './http-retry.js';
import { absolutizeImageUrl, extractFeaturedImageFromHtml } from './html-meta.js'; import { absolutizeImageUrl, extractFeaturedImageFromHtml } from './html-meta.js';
import { import {
readWebDavArchiveFromEnv, readWebDavArchiveFromEnv,
@@ -131,10 +132,11 @@ async function downloadBinaryImage(
'User-Agent': 'BitReport-news-writer-pipeline/1.0 (media)', 'User-Agent': 'BitReport-news-writer-pipeline/1.0 (media)',
...(extraHeaders ?? {}), ...(extraHeaders ?? {}),
}; };
const res = await fetch(imageUrl, { const res = await fetchBinaryWithRetry(imageUrl, {
timeoutMs: 20_000,
retries: 2,
userAgent: headers['User-Agent'],
headers, headers,
signal: AbortSignal.timeout(20_000),
redirect: 'follow',
}); });
if (!res.ok) { if (!res.ok) {
throw new Error(`Featured image fetch failed ${res.status} for ${imageUrl}`); throw new Error(`Featured image fetch failed ${res.status} for ${imageUrl}`);