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 { /// /// Preventive maintenance generator /// turn PMs into Work orders /// /// 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.LogDebug("Checking if CoreJobPMInventoryCheck should run"); if (IsRunning) { log.LogDebug("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.LogDebug($"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 "); 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