/// <summary> /// Create Hangfire Jobs /// </summary> /// <param name="loggerFactory"></param> private void CreateHangfireAnnualRolloverJob(ILoggerFactory loggerFactory) { // HETS has one job that runs at the end of each year. ILogger log = loggerFactory.CreateLogger(typeof(Startup)); // first check to see if Hangfire already has the job. log.LogInformation("Attempting setup of Hangfire Annual rollover job ..."); try { string connectionString = GetConnectionString(); log.LogInformation("Creating Hangfire job for Annual rollover ..."); // every 5 minutes we see if a CCW record needs to be updated. We only update one CCW record at a time. // since the server is on UTC, we want UTC-7 for PDT midnight. RecurringJob.AddOrUpdate(() => SeniorityListExtensions.AnnualRolloverJob(null, connectionString), Cron.Yearly(3, 31, 17)); } catch (Exception e) { StringBuilder msg = new StringBuilder(); msg.AppendLine("Failed to setup Hangfire job."); log.LogCritical(new EventId(-1, "Hangfire job setup failed"), e, msg.ToString()); } }
/// <summary> /// Recalculate the block assignment for each piece of equipment /// </summary> /// <param name="performContext"></param> /// <param name="seniorityScoringRules"></param> /// <param name="dbContext"></param> /// <param name="systemId"></param> public static void ProcessBlocks(PerformContext performContext, string seniorityScoringRules, DbAppContext dbContext, string systemId) { try { performContext.WriteLine("*** Recalculating Equipment Block Assignment ***"); Debug.WriteLine("Recalculating Equipment Block Assignment"); int ii = 0; string _oldTableProgress = "BlockAssignment_Progress"; string _newTable = "BlockAssignment"; // check if the block assignment has already been completed int startPoint = ImportUtility.CheckInterMapForStartPoint(dbContext, _oldTableProgress, BcBidImport.SigId, _newTable); if (startPoint == BcBidImport.SigId) // this means the assignment job is complete { performContext.WriteLine("*** Recalculating Equipment Block Assignment is complete from the former process ***"); return; } // ************************************************************ // cleanup old block assignment status records // ************************************************************ List <ImportMap> importMapList = dbContext.ImportMaps .Where(x => x.OldTable == _oldTableProgress && x.NewTable == _newTable) .ToList(); foreach (ImportMap importMap in importMapList) { dbContext.ImportMaps.Remove(importMap); } dbContext.SaveChanges(); // ************************************************************ // get processing rules // ************************************************************ SeniorityScoringRules scoringRules = new SeniorityScoringRules(seniorityScoringRules); // ************************************************************ // get all local areas // (using active equipment to minimize the results) // ************************************************************ List <LocalArea> localAreas = dbContext.Equipments .Include(x => x.LocalArea) .Where(x => x.Status == Equipment.StatusApproved && x.ArchiveCode == "N") .Select(x => x.LocalArea) .Distinct() .ToList(); // ************************************************************ // get all district equipment types // (using active equipment to minimize the results) // ************************************************************ List <DistrictEquipmentType> equipmentTypes = dbContext.Equipments .Include(x => x.DistrictEquipmentType) .Where(x => x.Status == Equipment.StatusApproved && x.ArchiveCode == "N") .Select(x => x.DistrictEquipmentType) .Distinct() .ToList(); // ************************************************************************ // iterate the data and update the assugnment blocks // (seniority is already calculated) // ************************************************************************ Debug.WriteLine("Recalculating Equipment Block Assignment - Local Area Record Count: " + localAreas.Count); foreach (LocalArea localArea in localAreas) { foreach (DistrictEquipmentType districtEquipmentType in equipmentTypes) { // get the associated equipment type EquipmentType equipmentTypeRecord = dbContext.EquipmentTypes.FirstOrDefault(x => x.Id == districtEquipmentType.EquipmentTypeId); if (equipmentTypeRecord == null) { throw new DataException(string.Format("Invalid District Equipment Type. No associated Equipment Type record (District Equipment Id: {0})", districtEquipmentType.Id)); } // get rules int blockSize = equipmentTypeRecord.IsDumpTruck ? scoringRules.GetBlockSize("DumpTruck") : scoringRules.GetBlockSize(); int totalBlocks = equipmentTypeRecord.IsDumpTruck ? scoringRules.GetTotalBlocks("DumpTruck") : scoringRules.GetTotalBlocks(); // assign blocks SeniorityListExtensions.AssignBlocks(dbContext, localArea.Id, districtEquipmentType.Id, blockSize, totalBlocks, false); // save change to database periodically to avoid frequent writing to the database if (ii++ % 1000 == 0) { try { Debug.WriteLine("Recalculating Equipment Block Assignment - Index: " + ii); ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, ii.ToString(), BcBidImport.SigId, _newTable); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } } // ************************************************************ // save final set of updates // ************************************************************ try { performContext.WriteLine("*** Recalculating Equipment Block Assignment is Done ***"); Debug.WriteLine("Recalculating Equipment Block Assignment is Done"); ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, _newTable); dbContext.SaveChangesForImport(); } catch (Exception e) { string temp = string.Format("Error saving data (Record: {0}): {1}", ii, e.Message); performContext.WriteLine(temp); throw new DataException(temp); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }