/// <summary> /// /// </summary> /// <param name="item"></param> /// <param name="cache"></param> /// <param name="logger"></param> public void UpdateMakoAdmin(RunQueueItem item, SqlCacheRepo cache, ILogProvider logger) { if (item.PlanId == LastPlanId && item.RevisionId == LastRevisionId) { return; } if (CacheHelper.IsIncache(Config.CompanyId, item.PlanId, item.RevisionId, CacheHelper.CacheObjectType.Assembly)) { Assembly = CacheHelper.GetFromCache <Assembly>(Config.CompanyId, item.PlanId, item.RevisionId, CacheHelper.CacheObjectType.Assembly); PlanName = CacheHelper.GetFromCache <string>(Config.CompanyId, item.PlanId, item.RevisionId, CacheHelper.CacheObjectType.ProjectName); } else { var response = new VsProjectDll().GetPlanDll(item.PlanId, item.RevisionId, ThresherToken); // TODO: Set up token Assembly = Assembly.Load(response.Dll); PlanName = response.PlanName; CacheHelper.SaveToCache(Config.CompanyId, item.PlanId, item.RevisionId, Assembly, CacheHelper.CacheObjectType.Assembly); CacheHelper.SaveToCache(Config.CompanyId, item.PlanId, item.RevisionId, PlanName, CacheHelper.CacheObjectType.ProjectName); } // // TODO: Figure out why we are doing a 'new' here. Admin = new MakoAdmin(Config.CompanyId, Assembly, cache, logger, item.PlanId, item.RevisionId); LastPlanId = item.PlanId; LastRevisionId = item.RevisionId; }
/// <summary> /// /// </summary> /// <param name="appContract"></param> private void ProcessQueue(ApplicationContract appContract) { if (appContract != null) { // Retrieve the connection string for this Application Contract. var connString = appContract.GenerateConnectionStr(); // Retrieve the company data for the specified ID. CompanySettingsForId = new CompanySettings(connString, Config.CompanyId); // Create a new SQL Cache. MakoCache = new SqlCacheRepo(appContract.GenerateConnectionStr(), MakoLogger); // Create a new Queue management object. var queueManager = new QueueManagement(Config.CompanyId, appContract.GenerateConnectionStr()); // Clear the queue for this Company ID. queueManager.ClearQueue(); // IsRunning = true; // while (IsRunning) { // ProcessQueueItem(appContract, MakoLogger, MakoCache, queueManager); } } }
/// <summary> /// /// </summary> /// <param name="appContract"></param> /// <param name="makoLogger"></param> /// <param name="makoCache"></param> /// <param name="queueManager"></param> private void ProcessQueueItem(ApplicationContract appContract, DelegateLogProvider makoLogger, SqlCacheRepo makoCache, QueueManagement queueManager) { // TODO: Find a smart way to check to see if there are changes to the connection strings and so forth // while (queueManager.FindNextQueueItem(out RunQueueItem item)) // TODO: Write authentication for app { // var startTime = DateTimeOffset.Now; _logRunTy = item.RunTy; ConnectString = appContract.GenerateConnectionStr(); _logCompanyId = Config.CompanyId; _logQueueId = item.QueueId; _logPeriodType = item.PeriodTy; _logPeriodId = item.PeriodId; try { // UpdateMakoAdmin(item, makoCache, makoLogger); // _listLog = new List <string>(); _listLog.Add(""); _listLog.Add($"{DateTime.UtcNow.ToString("G")} ------------------------------------------"); _listLog.Add( $"{DateTime.UtcNow.ToString("G")} Now Processing Real Time - QueueId {item.QueueId}"); _listLog.Add( $"{DateTime.UtcNow.ToString("G")} RunTy({item.RunTy}), PlanId({item.PlanId}), RevisionId({item.RevisionId})"); _listLog.Add($"{DateTime.UtcNow.ToString("G")} ------------------------------------------"); _listLog.Add(""); // Assembly assm, string typesXML, string bonusXML, string glossaryXML, // string overrideXML, string rankXML, string volumeXML var myschema = new MakoXMLSchemas( Assembly, PlanName + ".MakoTypes.xml", PlanName + ".MakoBonuses.xml", "", PlanName + ".MakoOverrides.xml", PlanName + ".MakoRanks.xml", PlanName + ".MakoVolumes.xml" ); // MakoConfig comConfig = new MakoConfig() { PeriodID = item.PeriodId, PeriodTy = item.PeriodTy, QueueID = item.QueueId, RunDescr = item.RunDescr, RunID = item.RunId, RunTy = item.RunTy, XMLSchema = myschema, PlanID = item.PlanId, RevisonID = item.RevisionId }; // Admin.ProcessCommissions(comConfig); // var endTime = DateTimeOffset.Now; // if (comConfig.RunID != 0 && comConfig.RunTy != 4) { LoggingHelpers.UpdateRunLog(appContract.GenerateConnectionStr(), Config.CompanyId, item.QueueId, comConfig.RunID); queueManager.UpdateRunStartAndEndDate(appContract.GenerateConnectionStr(), Config.CompanyId, item.RunId, startTime, endTime); } queueManager.DeleteQueueItem(item.QueueId); var log = new FileLogger(Config.CompanyKey, item.QueueId, item.RunTy); log.GenerateLog(_listLog); _listLog.Clear(); // if (!IsRunning) { break; } } catch (Exception ex) { var log = new FileLogger(Config.CompanyKey, item.QueueId, item.RunTy); _listLog.Add($"{DateTime.UtcNow.ToString("G")} [Error] " + ex.Message); _listLog.Add($"{DateTime.UtcNow.ToString("G")} [Error] " + ex.StackTrace); log.GenerateLog(_listLog); _listLog.Clear(); // queueManager.LogQueueError(appContract.GenerateConnectionStr(), Config.CompanyId, item.QueueId, ex); } } // Thread.Sleep(TimeSpan.FromSeconds(1)); }
/// <summary> /// /// </summary> /// <param name="infoCompanyId"></param> /// <returns></returns> private Dictionary <string, object> GetCompanyRunnerInfo(string infoCompanyId) { // Dictionary <string, object> dict; try { // Retrieve the encoded Company Configuration data from the Settings. var encodedCompanyConfig = Settings.Default["CompanyId" + infoCompanyId].ToString(); // Convert the encoded company configuration value to a byte array. var data = Convert.FromBase64String(encodedCompanyConfig); // Decode the byte array to a string. var decodedString = Encoding.UTF8.GetString(data); // Deserialize the decoded string to a CompanyConfig object. var config = JsonConvert.DeserializeObject <CompanyConfig>(decodedString); // Create the object used for user and application authentication and authorization. var api = new AuthenticateAndAuthorize(); // Create an authentication token for the user encoded in the configuration file for the current Company ID. // TODO: Look into better ways of doing this. Try to use GetTokenForUser by adding to it the additional code found in this method call. Token = api.GetTokenForAzureUser(config.Username, config.Pwd); // Retrieve the encrypted data that is returned from the user-specific URL. var encryptStr = AuthenticateAndAuthorize.GetApplication(Token); // Authenticate and decrypt the data returned from the user-specific URL. var decrypt = AesThenHmac.SimpleDecrypt(encryptStr, Convert.FromBase64String(config.CryptKey), Convert.FromBase64String(config.AuthKey)); // Retrieve the List of application configurations. var list = JsonConvert.DeserializeObject <List <ApplicationContract> >(decrypt); // Select the First item in the List of application configuration objects. var appContract = list.FirstOrDefault(listItem => listItem.CompanyId == config.CompanyId && listItem.ApplicationTy == 1); if (appContract != null) { // Retrieve the connection string for this Application Contract. ConnectString = appContract.GenerateConnectionStr(); // Retrieve the company data for the specified ID. CompanySettingsForId = new CompanySettings(ConnectString, config.CompanyId); // Create a new SQL Cache. MakoCache = new SqlCacheRepo(appContract.GenerateConnectionStr(), MakoLogger); // Create a new Queue management object. QueueManager = new QueueManagement(config.CompanyId, appContract.GenerateConnectionStr()); // Clear the queue for this Company ID. QueueManager.ClearQueue(); // Create a Dictionary of values that will be used on the call to RunCommissions. // NOTE: 'item' and 'makoAdmin' values will be populated on each read from the queue. dict = new Dictionary <string, object> { { "config", config }, { "item", null }, { "makoAdmin", null }, { "connStr", ConnectString }, { "queueManager", QueueManager } }; } else { throw new ApplicationException("Invalid ApplicationContract in GetCompanyRunnerInfo."); } // return(dict); } catch (Exception e) { // TODO: Instead of throwing an exception and breaking the loop, log the error and continue. throw new ApplicationException("Error starting Windows Service for company ID" + infoCompanyId, e); } }