fix(scrapeURL/pdf): retry
This commit is contained in:
@@ -62,6 +62,8 @@ async function scrapePDFWithLlamaParse(meta: Meta, tempFilePath: string): Promis
|
|||||||
schema: z.object({
|
schema: z.object({
|
||||||
markdown: z.string(),
|
markdown: z.string(),
|
||||||
}),
|
}),
|
||||||
|
tryCount: 16,
|
||||||
|
tryCooldown: 250,
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export type RobustFetchParams<Schema extends z.Schema<any>> = {
|
|||||||
ignoreFailure?: boolean;
|
ignoreFailure?: boolean;
|
||||||
requestId?: string;
|
requestId?: string;
|
||||||
tryCount?: number;
|
tryCount?: number;
|
||||||
|
tryCooldown?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function robustFetch<Schema extends z.Schema<any>, Output = z.infer<Schema>>({
|
export async function robustFetch<Schema extends z.Schema<any>, Output = z.infer<Schema>>({
|
||||||
@@ -28,8 +29,9 @@ export async function robustFetch<Schema extends z.Schema<any>, Output = z.infer
|
|||||||
ignoreFailure = false,
|
ignoreFailure = false,
|
||||||
requestId = uuid(),
|
requestId = uuid(),
|
||||||
tryCount = 1,
|
tryCount = 1,
|
||||||
|
tryCooldown,
|
||||||
}: RobustFetchParams<Schema>): Promise<Output> {
|
}: RobustFetchParams<Schema>): Promise<Output> {
|
||||||
const params = { url, logger, method, body, headers, schema, ignoreResponse, ignoreFailure, tryCount };
|
const params = { url, logger, method, body, headers, schema, ignoreResponse, ignoreFailure, tryCount, tryCooldown };
|
||||||
|
|
||||||
let request: Response;
|
let request: Response;
|
||||||
try {
|
try {
|
||||||
@@ -86,6 +88,9 @@ export async function robustFetch<Schema extends z.Schema<any>, Output = z.infer
|
|||||||
if (request.status >= 300) {
|
if (request.status >= 300) {
|
||||||
if (tryCount > 1) {
|
if (tryCount > 1) {
|
||||||
logger.debug("Request sent failure status, trying " + (tryCount - 1) + " more times", { params, request, response, requestId });
|
logger.debug("Request sent failure status, trying " + (tryCount - 1) + " more times", { params, request, response, requestId });
|
||||||
|
if (tryCooldown !== undefined) {
|
||||||
|
await new Promise((resolve) => setTimeout(() => resolve(null), tryCooldown));
|
||||||
|
}
|
||||||
return await robustFetch({
|
return await robustFetch({
|
||||||
...params,
|
...params,
|
||||||
requestId,
|
requestId,
|
||||||
|
|||||||
Reference in New Issue
Block a user