public void Stop() { SharedClass.Logger.Info("Stopping AccountId : " + this.AccountId.ToString() + " Processor"); BulkRequest bulkRequest = null; this._shouldIProcess = false; while (this.QueueCount() > 0) { bulkRequest = this.DeQueue(); if (bulkRequest != null) { bulkRequest.ReEnQueueToDataBase(isDeQueued: false, reason: ""); } } while ((int)this._activeThreads > 0) { SharedClass.Logger.Info("AccountId " + this.AccountId.ToString() + " has still " + this.ActiveThreads.ToString() + " active process threads running"); Thread.Sleep(1000); } SharedClass.ReleaseAccountProcessor(this.AccountId); }
private BulkRequest DeQueue() { BulkRequest bulkRequest = null; try { while (!this._queueMutex.WaitOne()) { Thread.Sleep(200); } bulkRequest = this._bulkRequestsQueue.Dequeue(); } catch (Exception ex) { SharedClass.Logger.Error("Error DeQueuing In AccountId : " + this._accountId.ToString() + " Processor. Reason : " + ex.ToString()); } finally { this._queueMutex.ReleaseMutex(); } return(bulkRequest); }
public void Start() { SharedClass.Logger.Info("Started"); while (this._shouldIProcess && !SharedClass.HasStopSignal) { if (this.QueueCount() > 0 && this._activeThreads < this._maxThreads) { BulkRequest bulkRequest = this.DeQueue(); if (bulkRequest != null) { Thread thread = new Thread(new ParameterizedThreadStart(this.StartBulkProcess)); SharedClass.Logger.Info("Spawning New Thread For BulkRequest Id : " + bulkRequest.Id.ToString()); thread.Name = "Account_" + this._accountId.ToString() + "_Processor_" + (this._activeThreads + 1).ToString(); thread.Start(bulkRequest); } } Thread.Sleep(2000); if (this.ActiveThreads == 0 && this.QueueCount() == 0) { this.Stop(); } } }
public bool EnQueue(BulkRequest bulkRequest) { SharedClass.Logger.Info("EnQueuing BulkRequest " + bulkRequest.Id.ToString() + " Into AccountId " + this._accountId.ToString() + " Processor"); bool flag = false; try { while (!this._queueMutex.WaitOne()) { Thread.Sleep(200); } this._bulkRequestsQueue.Enqueue(bulkRequest); flag = true; } catch (Exception ex) { SharedClass.Logger.Error("Error EnQueuing BulkRequest Id : " + bulkRequest.Id.ToString() + " Into AccountId : " + this._accountId.ToString() + " Processor. Reason : " + ex.ToString()); } finally { this._queueMutex.ReleaseMutex(); } return(flag); }
public void StartBulkProcess(object input) { BulkRequest bulkRequest = input as BulkRequest; SharedClass.Logger.Info("Started Processing BulkRequest " + bulkRequest.DisplayString()); ++this.ActiveThreads; System.Data.DataTable mobileUUIDsTable = new System.Data.DataTable(); string[] destinationsArray = null; string[] uuidsArray = null; JObject ChunkProcessResponse = null; mobileUUIDsTable.Columns.Add("Mobile", typeof(string)); mobileUUIDsTable.Columns.Add("UUID", typeof(string)); try { destinationsArray = bulkRequest.Destinations.ToString().Split(','); uuidsArray = bulkRequest.UUIDs.ToString().Split(','); if (destinationsArray.Length != uuidsArray.Length) { SharedClass.Logger.Error("Destinations Count (" + destinationsArray.Length.ToString() + ") And UUIDs Count (" + uuidsArray.Length.ToString() + ") Mismatch. Terminating Process."); bulkRequest.ReEnQueueToDataBase(isDeQueued: true, reason: "Destinations and UUIDs count mismatch"); return; } for (int iterator = bulkRequest.ProcessedCount; iterator < destinationsArray.Length; iterator++) { mobileUUIDsTable.Rows.Add(destinationsArray[iterator], uuidsArray[iterator]); if (mobileUUIDsTable.Rows.Count == SharedClass.BulkRequestBatchCount) { ChunkProcessResponse = ProcessChunk(bulkRequest, mobileUUIDsTable); if (Convert.ToBoolean(ChunkProcessResponse.SelectToken("Success").ToString()) == false) { SharedClass.Logger.Error("Error Processing BulkRequest : " + bulkRequest.DisplayString() + ", Reason : " + ChunkProcessResponse.SelectToken("Message").ToString()); bulkRequest.ReEnQueueToDataBase(isDeQueued: true, reason: ChunkProcessResponse.SelectToken("Message").ToString()); return; } else { bulkRequest.ProcessedCount += mobileUUIDsTable.Rows.Count; bulkRequest.UpdateProcessedCount(); } mobileUUIDsTable.Rows.Clear(); } } if (mobileUUIDsTable.Rows.Count > 0) { ChunkProcessResponse = ProcessChunk(bulkRequest, mobileUUIDsTable); if (Convert.ToBoolean(ChunkProcessResponse.SelectToken("Success").ToString()) == false) { SharedClass.Logger.Error("Error Processing BulkRequest : " + bulkRequest.DisplayString() + ", Reason : " + ChunkProcessResponse.SelectToken("Message").ToString()); bulkRequest.ReEnQueueToDataBase(isDeQueued: true, reason: ChunkProcessResponse.SelectToken("Message").ToString()); return; } else { bulkRequest.ProcessedCount += mobileUUIDsTable.Rows.Count; bulkRequest.UpdateProcessedCount(); } mobileUUIDsTable.Rows.Clear(); } } catch (Exception e) { SharedClass.Logger.Error("Error Processing BulkRequest : " + e.ToString()); bulkRequest.UpdateProcessedCount(); } finally { --this.ActiveThreads; } }
private JObject ProcessChunk(BulkRequest bulkRequest, System.Data.DataTable mobileUUIDsTable) { SqlConnection sqlCon = new SqlConnection(SharedClass.GetConnectionString(bulkRequest.Environment)); SqlCommand sqlCmd = new SqlCommand("Create_Call", sqlCon); byte retryAttempt = 0; SqlParameter mobileUUIDParameter = null; SqlParameter xmlTagNamesParameter = null; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(bulkRequest.Xml); DataTable xmlTagNamesTable = GetXmlTagNames(xmlDoc); retryLabel: try { sqlCmd.Parameters.Add("@AccountId", SqlDbType.BigInt).Value = this._accountId; sqlCmd.Parameters.Add("@AccountType", SqlDbType.TinyInt).Value = this._accountType; sqlCmd.Parameters.Add("@ToolId", SqlDbType.TinyInt).Value = bulkRequest.ToolId; sqlCmd.Parameters.Add("@Xml", SqlDbType.VarChar, bulkRequest.Xml.Length).Value = bulkRequest.Xml; mobileUUIDParameter = sqlCmd.Parameters.Add("@MobileNumbersAndUUIDs", SqlDbType.Structured); mobileUUIDParameter.TypeName = "dbo.MobileNumberUUIDType"; mobileUUIDParameter.Value = mobileUUIDsTable; xmlTagNamesParameter = sqlCmd.Parameters.Add("@XmlTagNames", SqlDbType.Structured); xmlTagNamesParameter.TypeName = "dbo.XmlTagNames"; xmlTagNamesParameter.Value = xmlTagNamesTable; sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.Add("@IpAddress", SqlDbType.VarChar, bulkRequest.Ip.Length).Value = bulkRequest.Ip; sqlCmd.Parameters.Add("@AnswerUrl", SqlDbType.VarChar, bulkRequest.AnswerUrl.Length).Value = bulkRequest.AnswerUrl; sqlCmd.Parameters.Add("@RingUrl", SqlDbType.VarChar, bulkRequest.RingUrl.Length).Value = bulkRequest.RingUrl; sqlCmd.Parameters.Add("@HangupUrl", SqlDbType.VarChar, bulkRequest.HangupUrl.Length).Value = bulkRequest.HangupUrl; sqlCmd.Parameters.Add("@CallerId", SqlDbType.VarChar, bulkRequest.CallerId.Length).Value = bulkRequest.CallerId; sqlCmd.Parameters.Add("@Retries", SqlDbType.TinyInt).Value = bulkRequest.Retries; sqlCmd.Parameters.Add("@BulkRequestId", SqlDbType.BigInt).Value = bulkRequest.Id; sqlCmd.Parameters.Add("@CampaignScheduleId", SqlDbType.BigInt).Value = bulkRequest.CampaignScheduleId; sqlCmd.Parameters.Add("@StatusCode", SqlDbType.Int).Direction = System.Data.ParameterDirection.Output; sqlCmd.Parameters.Add("@Success", SqlDbType.Bit).Direction = System.Data.ParameterDirection.Output; sqlCmd.Parameters.Add("@Message", SqlDbType.VarChar, 1000).Direction = System.Data.ParameterDirection.Output; sqlCon.Open(); sqlCmd.ExecuteNonQuery(); return(GetOutputParametersAsJSon(sqlCmd.Parameters)); } catch (Exception e) { SharedClass.Logger.Error("Error Processing BulkRequest (retryAttempt : " + retryAttempt + ") : " + e.ToString()); ++retryAttempt; if (retryAttempt <= 3) { goto retryLabel; } else { SharedClass.Logger.Error("Max Retry Attempts Reached with error : " + e.ToString()); return(new JObject(new JProperty("Success", false), new JProperty("Message", e.ToString()))); } } finally { if (sqlCon.State == System.Data.ConnectionState.Open) { sqlCon.Close(); } sqlCon.Dispose(); sqlCmd.Dispose(); } }
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); } } }