This commit is contained in:
2021-10-27 23:18:59 +00:00
parent bc802e2956
commit a900abf261
4 changed files with 66 additions and 31 deletions

View File

@@ -44,55 +44,77 @@ namespace AyaNova.Util
public class ReportRenderInstanceInfo
{
public int ReporterProcessId { get; set; }
public DateTime Started { get; set; }
public DateTime Expires { get; set; }
public ReportRenderInstanceInfo(int processId)
{
ReporterProcessId = processId;
Started = DateTime.UtcNow;
Expires = DateTime.UtcNow.AddMilliseconds(ServerBootConfig.AYANOVA_REPORT_RENDERING_TIMEOUT);
}
}
public static bool RenderSlotAvailable()
public static bool RenderSlotAvailable(ILogger log)
{
if (_baginstances.Count >= ServerBootConfig.AYANOVA_REPORT_RENDERING_MAX_INSTANCES)
log.LogTrace("RenderSlotAvailable check");
var count = _baginstances.Count;
#if (DEBUG)
log.LogInformation($"DBG: RenderSlotAvailable check, there are currently {count} instances in the bag");
#endif
if (count >= ServerBootConfig.AYANOVA_REPORT_RENDERING_MAX_INSTANCES)
{
log.LogTrace($"RenderSlotAvailable there are no free report rendering slots available, current count is {count}, checking for expired slots to force closed");
//check for expired and remove
var Instances = _baginstances.ToArray();
ReportRenderInstanceInfo oldest = null;
var dtNow = DateTime.UtcNow;
foreach (ReportRenderInstanceInfo i in Instances)
{
if (oldest == null)
if (i.Expires < dtNow)
{
oldest = i;
continue;
#if (DEBUG)
log.LogInformation($"DBG: RenderSlotAvailable attempting kill of expired process {i.ReporterProcessId}");
#endif
ForceCloseProcess(i, log);
}
if (i.Started < oldest.Started) oldest = i;
}
if (oldest != null)
{
try
{
var p = Process.GetProcessById(oldest.ReporterProcessId);
if (p != null)
{
//try to kill it
p.Kill();
if (p?.HasExited == false) return false;//can't kill it so can't free up a slot
}
_baginstances.TryTake(out oldest);
return true;//process that was there is now not there so while not perfect system we will consider it free
}
//allow to continue if there are now fewer than max instances in the bag
return _baginstances.Count < ServerBootConfig.AYANOVA_REPORT_RENDERING_MAX_INSTANCES;
}
}
catch (ArgumentException)
private static bool ForceCloseProcess(ReportRenderInstanceInfo instance, ILogger log)
{
log.LogTrace($"ForceCloseProcess on instance id {instance.ReporterProcessId} started {instance.Expires.ToString()} utc");
try
{
var p = Process.GetProcessById(instance.ReporterProcessId);
if (p != null)
{
//we have an existing process
//try to kill it
p.Kill();
if (p.HasExited == false)
{
return true;//no process available / not running
log.LogDebug($"RenderSlotAvailable oldest slot could not be stopped");
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
_baginstances.TryTake(out instance);
return true;//process that was there is now not there so while not perfect system we will consider it free
}
return true;
catch (ArgumentException)
{
//do nothing, this is normal, the process could not be found and this means it's already been removed:
//ArgumentException
//The process specified by the processId parameter is not running. The identifier might be expired.
_baginstances.TryTake(out instance);
return true;
}
}
@@ -101,6 +123,18 @@ namespace AyaNova.Util
_baginstances.Add(new ReportRenderInstanceInfo(processId));
}
internal static void RemoveProcess(int processId, ILogger log)
{
foreach (var i in _baginstances)
{
if (i.ReporterProcessId == processId)
{
ForceCloseProcess(i, log);
break;
}
}
}
// internal async static Task EnsureReporterAvailableAsync(ILogger log)