From 871d2de50f2269c95f80de7451266e8d4a679165 Mon Sep 17 00:00:00 2001 From: Ben Schwitter Date: Mon, 9 Sep 2024 16:23:15 +0800 Subject: [PATCH] chore: update based on feedback --- load-test/boom.ts | 6 ++--- .../GeneratorFunction.cs | 23 ++++++++++++++++++- .../PdfGenerator.cs | 12 ++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/load-test/boom.ts b/load-test/boom.ts index 4b7a111..d48e286 100644 --- a/load-test/boom.ts +++ b/load-test/boom.ts @@ -30,7 +30,7 @@ async function main(): Promise { createCP(), createCP(), ]); - console.log(`block ${i} done`); + console.log(`\nblock ${i} done`); } } @@ -48,13 +48,13 @@ async function createCP(): Promise { const startTime = Date.now(); const fetchOptions: RequestInit = requestFetchOptions(); - console.log(`calling CP`); + process.stdout.write("."); response = await fetch(targetUrl + 'document-generation', { ...fetchOptions, }); const endTime = Date.now(); const a = endTime - startTime; - console.log(`CP create finished; time taken: ${a}ms`); + process.stdout.write(`\nCP create finished; time taken: ${a}ms`); const contentType = response.headers.get("content-type") ?? ''; if (response.status >= 200 && response.status < 300) { diff --git a/src/functions/document-generation/DocumentGeneration.Lambdas/GeneratorFunction.cs b/src/functions/document-generation/DocumentGeneration.Lambdas/GeneratorFunction.cs index 99b00cc..27073cf 100644 --- a/src/functions/document-generation/DocumentGeneration.Lambdas/GeneratorFunction.cs +++ b/src/functions/document-generation/DocumentGeneration.Lambdas/GeneratorFunction.cs @@ -17,11 +17,14 @@ public class GeneratorFunction private readonly PdfGenerator _generator; private readonly ILogger _logger; - static GeneratorFunction() + static GeneratorFunction() { var logger = Logger.Create(); logger.LogDebug("Static Generator Function triggered"); PdfGenerator.InitializeIronPdf(logger); + + AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; + logger.LogDebug("Static Generator Function finished."); } @@ -83,6 +86,11 @@ public APIGatewayProxyResponse DocumentGeneratorHandler(APIGatewayProxyRequest r IsBase64Encoded = false }; } + finally + { + //enable this to fix the issue. + //GC.Collect(); + } } private static ServiceProvider BuildServiceProvider() { @@ -96,5 +104,18 @@ private static ServiceProvider BuildServiceProvider() return serviceCollection.BuildServiceProvider(); } + + static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e) + { + try + { + var logger = Logger.Create(); + logger.LogError("Unhandled Exception: {Exception}", e.ExceptionObject); + } + catch + { + //to nothing since we can't log + } + } } diff --git a/src/functions/document-generation/DocumentGeneration.Lambdas/PdfGenerator.cs b/src/functions/document-generation/DocumentGeneration.Lambdas/PdfGenerator.cs index b105358..6c6039d 100644 --- a/src/functions/document-generation/DocumentGeneration.Lambdas/PdfGenerator.cs +++ b/src/functions/document-generation/DocumentGeneration.Lambdas/PdfGenerator.cs @@ -16,7 +16,8 @@ public PdfGenerator(DocumentGenerationSettings documentGenerationSettings, ILogg RenderingOptions = { UseMarginsOnHeaderAndFooter = UseMargins.None, - CreatePdfFormsFromHtml = true + CreatePdfFormsFromHtml = true, + } }; @@ -36,8 +37,8 @@ public static void InitializeIronPdf(ILogger logger) //add additional Iron Pdf logging when debug level is set if (logLevel == "Debug") { - IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom; - IronPdf.Logging.Logger.CustomLogger = logger; + // IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom; + // IronPdf.Logging.Logger.CustomLogger = logger; } // we configure the dependencies ourselves in the Dockerfile @@ -50,12 +51,13 @@ public static void InitializeIronPdf(ILogger logger) Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process); Installation.TempFolderPath = awsTmpPath; - Installation.CustomDeploymentDirectory = awsTmpPath; - Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled; Installation.ChromeBrowserCachePath = awsTmpPath; Installation.CustomDeploymentDirectory = awsTmpPath; + Installation.ChromeBrowserLimit = 1; + Installation.SingleProcess = true; + Installation.Initialize(); }