@@ -449,39 +449,50 @@ async function printPdf(req, numOfRetries = 0) {
449
449
} ) ;
450
450
451
451
log . info ( 'printPdf :: starting page load' ) ;
452
+ const startTime = Date . now ( ) ;
452
453
await page . goto ( url , { waitUntil : 'networkidle0' } ) ;
454
+ log . info ( `printPdf :: Page loaded successfully in ${ Date . now ( ) - startTime } ms` ) ;
455
+
456
+ log . info ( 'printPdf :: Waiting for signature field to appear' ) ;
453
457
await page . waitForSelector ( '#signatureTextField' , { visible : true } ) ;
454
458
455
- log . info ( 'printPdf :: page loaded starting pdf creation' ) ;
459
+ log . info ( 'printPdf :: Begin Generating PDF' ) ;
460
+ const pdfStartTime = Date . now ( ) ;
456
461
const pdfBuffer = await page . pdf ( {
457
462
displayHeaderFooter : false ,
458
463
printBackground : true ,
459
464
timeout : 300000 ,
460
465
width : 1280 ,
461
466
} ) ;
467
+ log . info ( `printPdf :: PDF generated in ${ Date . now ( ) - pdfStartTime } ms` ) ;
462
468
463
- log . info ( 'printPdf :: pdf buffer created starting compression ' ) ;
469
+ log . info ( 'printPdf :: Compressing PDF ' ) ;
464
470
const compressedPdfBuffer = await compress ( pdfBuffer , {
465
471
gsModule : process . env . GHOSTSCRIPT_PATH , // this is set in dockerfile to fix ghostscript error on deploy
466
472
} ) ;
467
- log . info ( ' printPdf :: compression completed for applicationId' , req . params . applicationId ) ;
473
+ log . info ( ` printPdf :: Compression completed for applicationId= ${ req . params . applicationId } ` ) ;
468
474
475
+ log . info ( 'printPdf :: Sending PDF to storage' ) ;
469
476
const payload = await postPdf ( req , compressedPdfBuffer ) ;
477
+
478
+ log . info ( `printPdf :: PDF successfully stored for applicationId=${ req . params . applicationId } ` ) ;
470
479
await browserContext . close ( ) ;
471
480
closeBrowser ( ) ;
472
481
473
482
return payload ;
474
483
} catch ( e ) {
475
- log . error ( e ) ;
484
+ const errorMessage = e instanceof Error ? e . stack || e . message : JSON . stringify ( e ) ;
485
+ log . error ( `printPdf :: Error occurred for applicationId=${ req . params . applicationId } , ${ errorMessage } ` ) ;
486
+
476
487
await browserContext ?. close ( ) ;
477
488
478
- if ( numOfRetries >= 3 ) {
479
- log . info ( ' printPdf :: maximum number of retries reached' ) ;
480
- log . error ( `printPdf :: unable to save pdf for application id ${ req . params . applicationId } ` ) ;
489
+ if ( numOfRetries >= 5 ) {
490
+ log . error ( ` printPdf :: Maximum retries reached for applicationId= ${ req . params . applicationId } . Aborting.` ) ;
491
+ closeBrowser ( ) ;
481
492
} else {
482
- let retryCount = numOfRetries + 1 ;
483
- log . info ( 'printPdf :: failed retrying' ) ;
484
- log . info ( `printPdf :: retry count ${ retryCount } ` ) ;
493
+ const retryCount = numOfRetries + 1 ;
494
+ await sleep ( 5000 ) ;
495
+ log . info ( `printPdf :: Retrying ( ${ retryCount } /5) for applicationId= ${ req . params . applicationId } ` ) ;
485
496
await printPdf ( req , retryCount ) ;
486
497
}
487
498
}
0 commit comments