This commit is contained in:
@@ -11,6 +11,7 @@ using EnumsNET;
|
|||||||
using PuppeteerSharp;
|
using PuppeteerSharp;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
@@ -432,6 +433,10 @@ namespace AyaNova.Biz
|
|||||||
//initialization
|
//initialization
|
||||||
log.LogDebug("Initializing report system");
|
log.LogDebug("Initializing report system");
|
||||||
var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "rpt");
|
var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "rpt");
|
||||||
|
|
||||||
|
//Ensure last reporting op has completed
|
||||||
|
ReportingProcessCache.EnsureReporterAvailable(ServerBootConfig.REPORT_RENDERING_OPERATION_TIMEOUT);
|
||||||
|
|
||||||
var lo = new LaunchOptions { Headless = true };
|
var lo = new LaunchOptions { Headless = true };
|
||||||
bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
|
bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
|
||||||
if (!isWindows)
|
if (!isWindows)
|
||||||
@@ -487,11 +492,8 @@ namespace AyaNova.Biz
|
|||||||
using (var browser = await Puppeteer.LaunchAsync(lo))
|
using (var browser = await Puppeteer.LaunchAsync(lo))
|
||||||
using (var page = await browser.NewPageAsync())
|
using (var page = await browser.NewPageAsync())
|
||||||
{
|
{
|
||||||
|
//mark this process so it can be cancelled if it times out
|
||||||
|
ReportingProcessCache.FlagNewProcess(browser.Process.Id);
|
||||||
|
|
||||||
//see catch block
|
|
||||||
var ChromiumProcessID = browser.Process.Id;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -710,15 +712,7 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
//render to pdf and return
|
//render to pdf and return
|
||||||
log.LogDebug($"Calling render page contents to PDF");
|
log.LogDebug($"Calling render page contents to PDF");
|
||||||
|
|
||||||
//============== TESTING =================
|
|
||||||
var process = System.Diagnostics.Process.GetProcessById(ChromiumProcessID);
|
|
||||||
if (ChromiumProcessID > 0 && process?.HasExited == false)
|
|
||||||
{
|
|
||||||
log.LogError($"Error during render, Chromium process (pid {ChromiumProcessID}) still active, forcing it to stop now");
|
|
||||||
process.Kill();
|
|
||||||
}
|
|
||||||
//============================================
|
|
||||||
await page.PdfAsync(outputFullPath, PdfOptions);//###### TODO: SLOW NEEDS TIMEOUT HERE ONCE SUPPORTED, open bug: https://github.com/hardkoded/puppeteer-sharp/issues/1710
|
await page.PdfAsync(outputFullPath, PdfOptions);//###### TODO: SLOW NEEDS TIMEOUT HERE ONCE SUPPORTED, open bug: https://github.com/hardkoded/puppeteer-sharp/issues/1710
|
||||||
|
|
||||||
log.LogDebug($"Completed, returning results");
|
log.LogDebug($"Completed, returning results");
|
||||||
|
|||||||
68
server/AyaNova/util/ReportingProcessCache.cs
Normal file
68
server/AyaNova/util/ReportingProcessCache.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
|
||||||
|
namespace AyaNova.Util
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used by reporting system to ensure headless browsers don't hang around in an untimely manner
|
||||||
|
/// needed due to bugs in puppeteersharp where it won't close the browser on timeout properly
|
||||||
|
/// also zombie process issues in linux etc, this just ensures it's safe
|
||||||
|
/// This is triggered when a report is rendered
|
||||||
|
/// </summary>
|
||||||
|
internal static class ReportingProcessCache
|
||||||
|
{
|
||||||
|
internal static int ReporterProcessId { get; set; } = -1;
|
||||||
|
internal static DateTime Started { get; set; }
|
||||||
|
|
||||||
|
internal static void EnsureReporterAvailable(int timeOutMilliSeconds)
|
||||||
|
{
|
||||||
|
if (ReporterProcess() == null) return;
|
||||||
|
//await it's completion in the specified timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void FlagNewProcess(int processId)
|
||||||
|
{
|
||||||
|
ReporterProcessId = processId;
|
||||||
|
Started = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Process ReporterProcess()
|
||||||
|
{
|
||||||
|
if (ReporterProcessId == -1) return null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Process.GetProcessById(ReporterProcessId);
|
||||||
|
}
|
||||||
|
catch (ArgumentException)
|
||||||
|
{
|
||||||
|
return null;//no process available / not running
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
//Is the report generator (browser) already running?
|
||||||
|
if(ReportingProcessCache.ReporterProcess()!=null){
|
||||||
|
//there is an existing process in action, let's wait for timeout seconds and kill it if it's still running before proceeding
|
||||||
|
//first check to see if it's still actually running or not:
|
||||||
|
var process = System.Diagnostics.Process.GetProcessById(ReportingProcessCache.ReporterProcessId);
|
||||||
|
// if (ChromiumProcessID > 0 && process?.HasExited == false)
|
||||||
|
// {
|
||||||
|
// log.LogError($"Error during render, Chromium process (pid {ChromiumProcessID}) still active, forcing it to stop now");
|
||||||
|
// process.Kill();
|
||||||
|
// }
|
||||||
|
|
||||||
|
bool keepOnWaiting=true;
|
||||||
|
while(keepOnWaiting){
|
||||||
|
var v= DateTime.UtcNow-ReportingProcessCache.Started;
|
||||||
|
if(v.TotalSeconds> ServerBootConfig.REPORT_RENDERING_OPERATION_TIMEOUT){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}//eoc
|
||||||
|
}//eons
|
||||||
Reference in New Issue
Block a user