This commit is contained in:
2021-12-27 22:01:29 +00:00
parent aff0fa3a46
commit 68332a7290
6 changed files with 631 additions and 519 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;
using AyaNova.Biz;
using Microsoft.Extensions.Logging;
namespace AyaNova.Util
@@ -9,7 +11,7 @@ namespace AyaNova.Util
//Track processes and kill any that go past their expiry date
internal static class ReportRenderManager
{
internal static ConcurrentBag<ReportRenderInstanceInfo> _baginstances;
static ReportRenderManager()
@@ -21,16 +23,18 @@ namespace AyaNova.Util
{
internal int ReporterProcessId { get; set; }
internal DateTime Expires { get; set; }
internal Guid JobId { get; set; }
internal ReportRenderInstanceInfo(int processId, DateTime expires)
internal ReportRenderInstanceInfo(Guid jobId, DateTime expires)
{
ReporterProcessId = processId;
JobId = jobId;
Expires = expires;
ReporterProcessId = -1;
}
}
internal static void KillExpiredRenders(ILogger log)
internal static async Task KillExpiredRenders(ILogger log)
{
log.LogDebug("Clear potential expired render jobs check");
//check for expired and remove
@@ -40,35 +44,39 @@ namespace AyaNova.Util
{
if (i.Expires < dtNow)
{
log.LogDebug($"attempting close of expired process {i.ReporterProcessId}");
ForceCloseProcess(i, log);
log.LogDebug($"attempting close of expired process {i.ReporterProcessId} for job {i.JobId}");
await CloseRenderProcess(i, log);
}
}
}
internal static bool ForceCloseProcess(ReportRenderInstanceInfo instance, ILogger log)
internal static async Task<bool> CloseRenderProcess(ReportRenderInstanceInfo instance, ILogger log)
{
log.LogDebug($"ForceCloseProcess on report render instance id {instance.ReporterProcessId} expired {instance.Expires.ToString()} utc");
try
{
var p = Process.GetProcessById(instance.ReporterProcessId);
//either way, clear the job so the client gets informed
await JobsBiz.LogJobAsync(instance.JobId, $"rendererror:timeout,{ServerBootConfig.AYANOVA_REPORT_RENDERING_TIMEOUT}");//parseable for print client
await JobsBiz.UpdateJobStatusAsync(instance.JobId, JobStatus.Failed);
if (p != null)
if (instance.ReporterProcessId != -1)//if a job doesn't have a process id yet it will be -1
{
//we have an existing process
//try to kill it
p.Kill(true);
if (p.HasExited == false)
var p = Process.GetProcessById(instance.ReporterProcessId);
if (p != null)
{
log.LogWarning($"Expired report render instance id {instance.ReporterProcessId} could not be force closed");
return false;//can't kill it so can't free up a slot
//we have an existing process
//try to kill it
p.Kill(true);
if (p.HasExited == false)
{
log.LogWarning($"Expired report render instance id {instance.ReporterProcessId} could not be force closed");
return false;//can't kill it so can't free up a slot
}
}
}
//remove it from the list, it's either gone or killed at this point
//this would not be unexpected since it will normally just close on it's own
//at the finally in render report
//at the finally block in render report
_baginstances.TryTake(out instance);
return true;//process that was there is now not there so while not perfect system we will consider it free
@@ -83,21 +91,61 @@ namespace AyaNova.Util
}
}
internal static void AddProcess(int processId, DateTime expires, ILogger log)
{
log.LogDebug($"AddProcess - {processId} to the collection");
_baginstances.Add(new ReportRenderInstanceInfo(processId, expires));
log.LogInformation($"AddProcess - there are currently {_baginstances.Count} instances in the collection");
internal static void AddJob(Guid jobId, DateTime expires, ILogger log)
{
log.LogDebug($"AddJob - {jobId} to the collection");
_baginstances.Add(new ReportRenderInstanceInfo(jobId, expires));
log.LogInformation($"AddJob - there are currently {_baginstances.Count} instances in the collection");
}
internal static void RemoveProcess(int processId, ILogger log)
internal static void SetProcess(Guid jobId, int processId, ILogger log)
{
log.LogDebug($"SetProcess - setting {jobId} to render process id {processId}");
foreach (var i in _baginstances)
{
if (i.JobId == jobId)
{
i.ReporterProcessId = processId;
break;
}
}
}
internal static async Task RemoveJob(Guid jobId, ILogger log)
{
foreach (var i in _baginstances)
{
if (i.JobId == jobId)
{
await CloseRenderProcess(i, log);
break;
}
}
}
internal static bool KeepGoing(Guid jobId)
{
foreach (var i in _baginstances)
{
if (i.JobId == jobId)
{
return true;
}
}
return false;
}
internal static async Task RemoveProcess(int processId, ILogger log)
{
foreach (var i in _baginstances)
{
if (i.ReporterProcessId == processId)
{
ForceCloseProcess(i, log);
await CloseRenderProcess(i, log);
break;
}
}