public static bool AddAccountProcessor(int accountId, AccountProcessor processor) { bool flag = false; _logger.Info("Adding AccountID " + accountId.ToString() + " Into ActiveAccountProcessors"); try { while (!_activeAccountsMutex.WaitOne()) { Thread.Sleep(10); } if (!_activeAccountProcessors.ContainsKey(accountId)) { _activeAccountProcessors.Add(accountId, processor); } flag = true; } catch (Exception ex) { _logger.Error("Error Adding UserProcessor To Map : " + ex.Message); } finally { _activeAccountsMutex.ReleaseMutex(); } return(flag); }
public void StartDbPoll(object input) { Environment environment = (Environment)input; SharedClass.Logger.Info("Started"); SqlCommand sqlCommand = new SqlCommand("VC_Get_PendingBulkVoiceRequests", new SqlConnection(SharedClass.GetConnectionString(environment))); sqlCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter sqlDataAdapter = null; DataSet dataSet = null; while (!SharedClass.HasStopSignal) { try { //if (environment == Environment.STAGING) ////_isIamPollingS = true; //{ // while (this._pollThreadStaging.ThreadState != ThreadState.Stopped) // { // SharedClass.Logger.Info(string.Format("Staging Poll Thread Not Yet Stopped. ThreadState:{0}", this._pollThreadStaging.ThreadState)); // Thread.Sleep(2000); // if (this._pollThreadStaging.ThreadState == ThreadState.WaitSleepJoin) // this._pollThreadStaging.Interrupt(); // } //} //else //{ // while (this._pollThread.ThreadState != ThreadState.Stopped) // { // SharedClass.Logger.Info(string.Format("Staging Poll Thread Not Yet Stopped. ThreadState:{0}", this._pollThread.ThreadState)); // Thread.Sleep(2000); // if (this._pollThread.ThreadState == ThreadState.WaitSleepJoin) // this._pollThread.Interrupt(); // } //} sqlCommand.Parameters.Clear(); sqlCommand.Parameters.Add("@LastRequestId", SqlDbType.BigInt).Value = BulkRequestId.GetLastId(environment); sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; dataSet = new DataSet(); sqlDataAdapter.Fill(dataSet); if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) { BulkRequest bulkRequest = null; foreach (DataRow dataRow in dataSet.Tables[0].Rows) { try { bulkRequest = new BulkRequest(); bulkRequest.Environment = environment; bulkRequest.Id = Convert.ToInt64(dataRow["Id"].ToString()); bulkRequest.Xml = dataRow["Xml"].ToString(); bulkRequest.Ip = dataRow["Ip"].ToString(); bulkRequest.ToolId = Convert.ToByte(dataRow["ToolId"]); bulkRequest.Destinations = new StringBuilder(dataRow["MobileNumbersList"].ToString()); bulkRequest.UUIDs = new StringBuilder(dataRow["UUIDsList"].ToString()); if (!dataRow["RingUrl"].IsDBNull()) { bulkRequest.RingUrl = dataRow["RingUrl"].ToString(); } else { bulkRequest.RingUrl = ""; } if (!dataRow["AnswerUrl"].IsDBNull()) { bulkRequest.AnswerUrl = dataRow["AnswerUrl"].ToString(); } else { bulkRequest.AnswerUrl = ""; } if (!dataRow["HangupUrl"].IsDBNull()) { bulkRequest.HangupUrl = dataRow["HangupUrl"].ToString(); } else { bulkRequest.HangupUrl = ""; } if (!dataRow["Retries"].IsDBNull()) { bulkRequest.Retries = Convert.ToByte(dataRow["Retries"].ToString()); } else { bulkRequest.Retries = 0; } if (!dataRow["CallerId"].IsDBNull()) { bulkRequest.CallerId = dataRow["CallerId"].ToString(); } else { bulkRequest.CallerId = ""; } if (!dataRow["Status"].IsDBNull()) { bulkRequest.Status = Convert.ToByte(dataRow["Status"].ToString()); } else { bulkRequest.Status = 0; } if (dataRow["ProcessedCount"] != DBNull.Value) { bulkRequest.ProcessedCount = Convert.ToInt32(dataRow["ProcessedCount"].ToString()); } if (!dataRow["TotalCount"].IsDBNull()) { bulkRequest.TotalCount = Convert.ToInt32(dataRow["TotalCount"].ToString()); } if (dataRow["RequestId"] != DBNull.Value) { bulkRequest.VoiceRequestId = Convert.ToInt64(dataRow["RequestId"].ToString()); } bulkRequest.CampaignScheduleId = Convert.ToInt64(dataRow["CampaignScheduleId"]); AccountProcessor accountProcessor = null; lock (SharedClass.ActiveAccountProcessors) { SharedClass.ActiveAccountProcessors.TryGetValue(Convert.ToInt32(dataRow["AccountId"].ToString()), out accountProcessor); if (accountProcessor == null) { accountProcessor = new AccountProcessor(); accountProcessor.AccountId = Convert.ToInt32(dataRow["AccountId"].ToString()); Thread accountProcessorThread = new Thread(new ThreadStart(accountProcessor.Start)); accountProcessorThread.Name = "Account_" + dataRow["AccountId"]; accountProcessorThread.Start(); } accountProcessor.EnQueue(bulkRequest); } BulkRequestId.SetLastId(bulkRequest.Id, environment); } catch (Exception ex1) { SharedClass.Logger.Error("Error In BulkPoll For Loop : " + ex1.ToString()); try { PropertyInfo[] properties = bulkRequest.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { if (propertyInfo.CanRead) { if (propertyInfo.GetValue(bulkRequest) == DBNull.Value) { SharedClass.DumpLogger.Error(propertyInfo.Name + " : NULL"); } else { SharedClass.DumpLogger.Error(propertyInfo.Name + " : " + propertyInfo.GetValue(bulkRequest).ToString()); } } } } catch (Exception ex2) { SharedClass.Logger.Error("Error Dumping Info : " + ex2.ToString()); } } } } } catch (Exception ex) { SharedClass.Logger.Error("Error In BulkPoller, " + ex.ToString()); } finally { NullReferenceException referenceException; try { sqlDataAdapter.Dispose(); } catch (NullReferenceException ex) { referenceException = ex; } try { dataSet.Dispose(); } catch (NullReferenceException ex) { referenceException = ex; } sqlDataAdapter = null; dataSet = null; } try { Thread.Sleep(5000); } catch (ThreadAbortException ex) { SharedClass.Logger.Error(ex); } catch (ThreadInterruptedException ex) { SharedClass.Logger.Error(ex); } } }