Files
raven/server/AyaNova/generator/CoreJobPMInventoryCheck.cs
2022-09-10 22:01:48 +00:00

83 lines
2.7 KiB
C#

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Util;
namespace AyaNova.Biz
{
/// <summary>
/// Preventive maintenance generator
/// turn PMs into Work orders
///
/// </summary>
internal static class CoreJobPMInventoryCheck
{
private static bool IsRunning = false;
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobPMInventoryCheck");
private static DateTime lastRun = DateTime.MinValue;
#if (DEBUG)
private static TimeSpan RUN_EVERY_INTERVAL = new TimeSpan(0, 0, 23);//no more frequently than once every 20 seconds
#else
private static TimeSpan RUN_EVERY_INTERVAL = new TimeSpan(0, 30, 0);//no more frequently than once every half hour
#endif
public static async Task DoWorkAsync()
{
log.LogTrace("Checking if CoreJobPMInventoryCheck should run");
if (IsRunning)
{
log.LogTrace("CoreJobPMInventoryCheck is running already exiting this cycle");
return;
}
//This will get triggered roughly every minute, but we don't want to deliver that frequently
if (DateTime.UtcNow - lastRun < RUN_EVERY_INTERVAL)
{
log.LogTrace($"CoreJobPMInventoryCheck ran less than {RUN_EVERY_INTERVAL} ago, exiting this cycle");
return;
}
//CHECK INVENTORY AND NOTIFY
try
{
IsRunning = true;
log.LogDebug("CoreJobPMInventoryCheck set to RUNNING state and starting now");
using (AyContext ct = AyaNova.Util.ServiceProviderProvider.DBContext)
{
await PMBiz.ProcessInsufficientInventoryNotificationAsync(ct, log);
}
}
catch (Exception ex)
{
log.LogError(ex, $"Error processing CoreJobPMInventoryCheck ");
DbUtil.HandleIfDatabaseUnavailableTypeException(ex);
await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.PMGenerationFailed, "PM Inventory check failure", "Preventive Maintenance", ex);
}
finally
{
log.LogDebug("CoreJobPMInventoryCheck has completed; setting to not running state and tagging lastRun timestamp");
lastRun = DateTime.UtcNow;
IsRunning = false;
}
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons