@@ -29,6 +29,7 @@ import {
29
29
PARTITION_FORM_FILES_KEY ,
30
30
PARTITION_FORM_SPLIT_PDF_PAGE_KEY ,
31
31
} from "./common.js" ;
32
+ import { retry , RetryConfig } from "../../lib/retries" ;
32
33
33
34
/**
34
35
* Represents a hook for splitting and sending PDF files as per page requests.
@@ -178,9 +179,11 @@ export class SplitPdfHook
178
179
file . name ,
179
180
firstPageNumber
180
181
) ;
182
+ const timeoutInMs = 60 * 10 * 1000 ;
181
183
const req = new Request ( requestClone , {
182
184
headers,
183
185
body,
186
+ signal : AbortSignal . timeout ( timeoutInMs )
184
187
} ) ;
185
188
requests . push ( req ) ;
186
189
setIndex += 1 ;
@@ -190,11 +193,19 @@ export class SplitPdfHook
190
193
191
194
const allowFailed = this . allowFailed ;
192
195
196
+ const retryConfig = { strategy : "backoff" } as RetryConfig ;
197
+ const retryCodes = [ "502" , "503" , "504" ] ;
198
+
193
199
this . partitionRequests [ operationID ] = async . parallelLimit (
194
200
requests . slice ( 0 , - 1 ) . map ( ( req , pageIndex ) => async ( ) => {
195
201
const pageNumber = pageIndex + startingPageNumber ;
196
202
try {
197
- const response = await this . client ! . request ( req ) ;
203
+ const response = await retry (
204
+ async ( ) => {
205
+ return await this . client ! . request ( req . clone ( ) ) ;
206
+ } ,
207
+ { config : retryConfig , statusCodes : retryCodes }
208
+ ) ;
198
209
if ( response . status === 200 ) {
199
210
( this . partitionSuccessfulResponses [ operationID ] as Response [ ] ) [ pageIndex ] =
200
211
response . clone ( ) ;
@@ -206,7 +217,7 @@ export class SplitPdfHook
206
217
}
207
218
}
208
219
} catch ( e ) {
209
- console . error ( `Failed to send request for page ${ pageNumber } .` ) ;
220
+ console . error ( `Failed to send request for page ${ pageNumber } .` , e ) ;
210
221
if ( ! allowFailed ) {
211
222
throw e ;
212
223
}
@@ -238,13 +249,13 @@ export class SplitPdfHook
238
249
const failedResponse = failedResponses [ 0 ] ?. clone ( ) ;
239
250
if ( failedResponse ) {
240
251
responseBody = await failedResponse . text ( ) ;
241
- responseStatus = failedResponse . status ;
242
252
responseStatusText = failedResponse . statusText ;
243
253
} else {
244
254
responseBody = JSON . stringify ( { "details:" : "Unknown error" } ) ;
245
- responseStatus = 503
246
255
responseStatusText = "Unknown error"
247
256
}
257
+ // if the response status is unknown or was 502, 503, 504, set back to 500 to ensure we don't cause more retries
258
+ responseStatus = 500 ;
248
259
console . warn (
249
260
`${ numFailedResponses } requests failed. The partition operation is cancelled.`
250
261
) ;
0 commit comments