fix(writer-pipeline): retry featured-image fetches on transient network errors (BIT-25135) #20
No files matched your search
@@ -7,6 +7,13 @@ export interface FetchTextOptions {
|
||||
accept?: string;
|
||||
}
|
||||
|
||||
export interface FetchBinaryOptions {
|
||||
timeoutMs?: number;
|
||||
retries?: number;
|
||||
userAgent?: string;
|
||||
headers?: Record<string, string>;
|
||||
}
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((r) => setTimeout(r, ms));
|
||||
}
|
||||
@@ -43,3 +50,36 @@ export async function fetchTextWithRetry(
|
||||
}
|
||||
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));
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { fetchTextWithRetry } from './http-retry.js';
|
||||
|
||||
/**
|
||||
* Raw HTML (capped) for OpenGraph / featured image extraction before publish.
|
||||
*/
|
||||
@@ -7,12 +9,11 @@ export async function fetchArticleHtmlForMeta(
|
||||
maxBytes = 600_000,
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
headers: { 'User-Agent': 'BitReport-news-writer-pipeline/1.0 (featured-image)' },
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
const html = await fetchTextWithRetry(url, {
|
||||
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);
|
||||
} catch {
|
||||
return null;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Buffer } from 'node:buffer';
|
||||
import type { AggregatedItem } from './types.js';
|
||||
import { fetchArticleHtmlForMeta } from './legacy-fetch.js';
|
||||
import { fetchBinaryWithRetry } from './http-retry.js';
|
||||
import { absolutizeImageUrl, extractFeaturedImageFromHtml } from './html-meta.js';
|
||||
import {
|
||||
readWebDavArchiveFromEnv,
|
||||
@@ -131,10 +132,11 @@ async function downloadBinaryImage(
|
||||
'User-Agent': 'BitReport-news-writer-pipeline/1.0 (media)',
|
||||
...(extraHeaders ?? {}),
|
||||
};
|
||||
const res = await fetch(imageUrl, {
|
||||
const res = await fetchBinaryWithRetry(imageUrl, {
|
||||
timeoutMs: 20_000,
|
||||
retries: 2,
|
||||
userAgent: headers['User-Agent'],
|
||||
headers,
|
||||
signal: AbortSignal.timeout(20_000),
|
||||
redirect: 'follow',
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(`Featured image fetch failed ${res.status} for ${imageUrl}`);
|
||||
|
||||
Reference in new issue
Block a user