Ejemplo n.º 1
0
        /// <summary>
        /// To process the call data from database
        /// </summary>
        /// <param name="uploadSetting">DataReaderSettings from the configuration file</param>
        internal void StartUpload(object uploadSetting)
        {
            DataReaderSetting readerSetting = null;
            ILog log = null;
            CallDataImporation          dataImporation    = null;
            IvrCallDataInfo             callDataInfo      = null;
            Dictionary <string, string> dicImportSettings = null;
            Stopwatch sWatch    = new Stopwatch();
            Stopwatch swProcess = new Stopwatch();

            try
            {
                Logger.Log.Info("inside Method");
                readerSetting = uploadSetting as DataReaderSetting;
                log           = LogManager.GetLogger(readerSetting.LoggerName);

                bool writeXmlToLog   = readerSetting.WriteXmlToLog;
                int  maxWaitTime     = readerSetting.MaxThreadSleepTime;
                int  sleepIncrTime   = readerSetting.SleepTimeIncrement;
                int  currentWaitTime = 1;

                string connStr       = ConfigurationManager.ConnectionStrings[readerSetting.ConnectionStringName].ConnectionString;
                string queueName     = readerSetting.QueueName;
                string tempFolder    = StaticInfo.TempXmlPacketFolder;
                bool   hasTempFolder = !string.IsNullOrEmpty(tempFolder);

                dicImportSettings = new Dictionary <string, string>();
                dicImportSettings.Add("ApplicationName", StaticInfo.ApplicationName);
                dicImportSettings.Add("ApplicationServer", StaticInfo.ApplicationServer);
                dicImportSettings.Add("ApplicationThreadName", Thread.CurrentThread.Name);
                dicImportSettings.Add("ConnectionString", connStr);
                dicImportSettings.Add("CommandTimeout", StaticInfo.CommandTimeout.ToString());
                dicImportSettings.Add("QueueName", queueName);
                dataImporation = new CallDataImporation(dicImportSettings, log);


                while (StaticInfo.CanContinue)
                {
                    try
                    {
                        #region Reading Call Data from Database

                        sWatch.Start();
                        List <IvrCallDataInfo> callInfoList = dataImporation.GetCallData();
                        sWatch.Stop();
                        log.InfoFormat("Total time taken to read the data from DB : {0}, ", sWatch.Elapsed);

                        if (callInfoList.Count == 0)
                        {
                            log.InfoFormat("There is no data in queue. Putting thread to sleep for {0} seconds", currentWaitTime);
                            Thread.Sleep(currentWaitTime * 1000);
                            if (currentWaitTime < maxWaitTime)
                            {
                                currentWaitTime++;
                            }
                            continue;
                        }
                        #endregion

                        currentWaitTime = 1;

                        foreach (IvrCallDataInfo callInfo in callInfoList)
                        {
                            try
                            {
                                swProcess.Start();
                                callDataInfo = callInfo;

                                #region Log The Call Data Into Temp Folder / Log File For Testing Purpose
                                //To write the call data into log file if it is enabled
                                if (writeXmlToLog)
                                {
                                    log.InfoFormat("Call Data : {0},Queue Data Msg Id : {1}", callDataInfo.CallData, callDataInfo.QueueMsgId);
                                }

                                //To check whether the temp folder configured in the configuration file
                                if (hasTempFolder)
                                {
                                    try
                                    {
                                        if (!Directory.Exists(tempFolder))
                                        {
                                            Directory.CreateDirectory(tempFolder);
                                        }

                                        int fc = Directory.GetFiles(tempFolder).Count();
                                        fc++;
                                        string f  = string.Format("calldata_{0}_{1}_{2}_{3}.xml", fc, DateTime.Now.ToString("HHmmssfff"), new Random().Next(100), new Random().Next(999));
                                        string fp = string.Format("{0}\\{1}", tempFolder, f);
                                        File.AppendAllText(fp, callDataInfo.CallData);
                                    }
                                    catch (Exception e)
                                    {
                                        log.Error("Problem occured while writting the call data into temp folder", e);
                                    }
                                }
                                #endregion

                                #region Data Import Process
                                sWatch.Start();
                                ImportationResponse importResponse = dataImporation.Import(callDataInfo.CallData);
                                //ImportationResponse importResponse = dataImporation.Import(callDataInfo.CallID, callDataInfo.SessionID, callDataInfo.ApplicationID, callDataInfo.CallData, callDataInfo.CallDateTime);
                                sWatch.Stop();

                                swProcess.Stop();

                                if (importResponse.HasImported)
                                {
                                    log.Info("Call Data Imported Successfully");
                                    log.InfoFormat("Total time taken to process at data importation : {0}, Total process time : {1} milli seconds", sWatch.Elapsed, swProcess.Elapsed);
                                }
                                else
                                {
                                    log.ErrorFormat("Call Data Failed To Import. Failure Mode : {0}, Error Code : {1}, Error Message : {2}", importResponse.FailureMode, importResponse.ErrorCode, importResponse.ErrorMessage);
                                    LoadCallDataIntoFile(log, readerSetting, callDataInfo, importResponse.FailureMode);
                                }
                                #endregion
                            }
                            catch (Exception exAtForLoop)
                            {
                                log.Error("Error occured while processing the call data", exAtForLoop);
                                log.ErrorFormat("CALL DATA : {0}", (callDataInfo != null) ? callDataInfo.CallData : "NOT AVAILABLE");
                            }
                        }
                    }
                    catch (Exception exAtInnerWhile)
                    {
                        log.Error("Error occured while processing the db data read activity", exAtInnerWhile);
                        log.ErrorFormat("CALL DATA : {0}", (callDataInfo != null) ? callDataInfo.CallData : "NOT AVAILABLE");
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error("Data upload process failed for the readers setting : {0}" + readerSetting.Name, ex);
            }
            finally
            {
                Interlocked.Decrement(ref StaticInfo.ThreadCount);
                Logger.Log.InfoFormat("Decrementing Thread Count as Method is exiting ... Thread Count {0} ", Interlocked.Read(ref StaticInfo.ThreadCount));
                if (dataImporation != null)
                {
                    dataImporation.Dispose();
                }
                dataImporation = null;
                if (dicImportSettings != null)
                {
                    dicImportSettings.Clear();
                }
                dicImportSettings = null;
                log = null; readerSetting = null;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// To start the recovery process
        /// </summary>
        /// <param name="recoverySettings">FileReaderSettting in the configuration</param>
        internal void StartRecovery(object recoverySettings)
        {
            ILog log = null;
            FileReaderSetting  recoverySetting = null;
            Queue <String>     fileQ           = new Queue <string>();
            CallDataImporation dataImporation  = null;
            Stopwatch          sWatch          = new Stopwatch();
            Stopwatch          swProcess       = new Stopwatch();

            try
            {
                Logger.Log.Info("Inside Methosd");

                recoverySetting      = recoverySettings as FileReaderSetting;
                _fileRecoveryMaxTime = StaticInfo.FileRecoveryMaxTime;

                int maxWaitTime     = recoverySetting.MaxThreadSleepTime;
                int threadIncrTime  = recoverySetting.SleepTimeIncrement;
                int currentWaitTime = 1;

                string connStr     = System.Configuration.ConfigurationManager.ConnectionStrings[recoverySetting.ConnectionStringName].ConnectionString;
                string currentFile = string.Empty;
                string callData    = string.Empty;

                TimeSpan importationTime;
                TimeSpan totalProcessTime;

                log = LogManager.GetLogger(recoverySetting.LoggerName);

                Dictionary <string, string> dicImportSettings = new Dictionary <string, string>();
                dicImportSettings.Add("ApplicationName", StaticInfo.ApplicationName);
                dicImportSettings.Add("ApplicationServer", StaticInfo.ApplicationServer);
                dicImportSettings.Add("ApplicationThreadName", Thread.CurrentThread.Name);
                dicImportSettings.Add("ConnectionString", connStr);
                dicImportSettings.Add("CommandTimeout", StaticInfo.CommandTimeout.ToString());

                dataImporation = new CallDataImporation(dicImportSettings, log);

                while (StaticInfo.CanContinue)
                {
                    try
                    {
                        fileQ = GetFileQueue(log, recoverySetting);
                        log.InfoFormat("Total No of Files Availble - {0}", fileQ.Count);

                        #region Delaying Recovery Process If No File Exists
                        //Putting Thread Into Sleep If No File Exists
                        if (fileQ.Count == 0)
                        {
                            if (currentWaitTime < maxWaitTime)
                            {
                                currentWaitTime += threadIncrTime;
                            }
                            log.DebugFormat("Putting the thread [{0}] on sleep to [{1}] seconds ", Thread.CurrentThread.Name, currentWaitTime);
                            Thread.Sleep(currentWaitTime * 1000);
                            continue;
                        }
                        #endregion

                        currentWaitTime = 1;

                        while (fileQ.Count > 0 && StaticInfo.CanContinue)
                        {
                            #region Reading Call Data From Recovery File
                            swProcess.Start();
                            currentFile = fileQ.Dequeue();

                            try
                            {
                                callData = File.ReadAllText(currentFile);
                            }
                            catch (Exception ex)
                            {
                                log.ErrorFormat("Failed to read the call data file : {0}", currentFile);
                                log.Error(ex);
                                continue;
                            }

                            //To write the call data into log file if it is enabled
                            if (recoverySetting.WriteXmlToLog)
                            {
                                log.InfoFormat("Call Data : {0}", callData);
                            }

                            //Writing Empty Call Data as Bad Xml Packet
                            if (string.IsNullOrEmpty(callData))
                            {
                                MoveRecoveryFiles(log, currentFile, ImportationFailureMode.InvalidXml, recoverySetting);
                                continue;
                            }
                            #endregion

                            #region Import Process
                            sWatch.Start();
                            ImportationResponse importResponse = dataImporation.Import(callData);
                            sWatch.Stop();
                            importationTime = sWatch.Elapsed;

                            swProcess.Stop();
                            totalProcessTime = swProcess.Elapsed;
                            #endregion

                            #region Handling Import Response
                            if (importResponse.HasImported)
                            {
                                log.Info("Call Data Imported Successfully");
                                log.InfoFormat("Total time taken to process at data importation : {0}, Total process time : {1} milli seconds", importationTime.TotalMilliseconds, totalProcessTime.TotalMilliseconds);
                                DeleteProcessedFile(currentFile, log);
                            }
                            else
                            {
                                log.ErrorFormat("Call Data Failed To Import. Failure Mode : {0}, Error Code : {1}, Error Message : {2}", importResponse.FailureMode, importResponse.ErrorCode, importResponse.ErrorMessage);
                                MoveRecoveryFiles(log, currentFile, importResponse.FailureMode, recoverySetting);
                            }
                            #endregion
                        }

                        log.DebugFormat("Putting the thread [{0}] on sleep to [{1}] seconds ", Thread.CurrentThread.Name, maxWaitTime);
                        Thread.Sleep(maxWaitTime * 1000);
                    }
                    catch (Exception exAtInnerWhile)
                    {
                        log.Error("Error occured while processing the file data read activity", exAtInnerWhile);
                        log.ErrorFormat("The file being processed at that time : {0}", currentFile);
                        log.DebugFormat("Putting the thread [{0}] on sleep to [{1}] seconds ", Thread.CurrentThread.Name, maxWaitTime);
                        Thread.Sleep(maxWaitTime * 1000);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error("Data Recovery Process is failed for the folder : " + recoverySetting.RecoveryFolder, ex);
            }
            finally
            {
                Interlocked.Decrement(ref StaticInfo.ThreadCount);
                Logger.Log.InfoFormat("Decrementing Thread Count as Method is exiting ... Thread Count {0} ", Interlocked.Read(ref StaticInfo.ThreadCount));
                if (fileQ != null)
                {
                    fileQ.Clear();
                }
                fileQ = null;
                if (dataImporation != null)
                {
                    dataImporation.Dispose();
                }
                dataImporation  = null;
                recoverySetting = null; log = null;
            }
        }