void JobProcessor_Elapsed(object sender, ElapsedEventArgs e) { try { if (DataSourceIsDisabled()) { return; } if (DataSourceIsTemporarilyStopped()) { return; } if (!this.IsRunning) { this.IsRunning = true; DataSource.TraceInformation(" - Starting"); Job = JobProcessor.Instantiate(DataSource.Id, DataSource.Name, ProcessingBy); Job.IsTriggeredBySqlPuller = true; this.DataSource.BusinessRules.Execute(Job, null, RuleSetTypes.SqlPullInit); DataTable table = ExecuteQuery(); InvokeAfterExecutingSelect(new SqlWatcherEventArgs(table)); if (table.Rows.Count == 0) { Job.TraceInformation(" - No records found."); lock (_lock) { if (Registry.Instance.Entries.ContainsKey(Job.JobIdentifier)) { Job job = Registry.Instance.Entries[Job.JobIdentifier] as Job; Registry.Instance.Entries.Remove(Job.JobIdentifier); job.Dispose(); job = null; } } } else { if (this.ReturnType == "I") { DataSource.TraceInformation("{0} - found {1} records. Processing with interface {2}", DataSource.Name, table.Rows.Count, InterfaceName); string fileName = GenerateFile(table); InvokeFileDownloaded(new FileSystemWatcherEventArgs(DataSourceParameters, fileName)); DataSource.TraceInformation("{0} - records processed with interface.", DataSource.Name); } else { DataSource.TraceInformation("{0} - found {1} records. Processing...", DataSource.Name, table.Rows.Count); Job.Feed(table); JobProcessor jobProcessor = new JobProcessor(); StringBuilder result = jobProcessor.ProcessJob(Job); if (Job.IsErrored) { new SqlWatcherHelper(Job).ExecuteRecoveryScript(); } string outputFolder = DataSource.GetOutputFolder(DataSource.Id, DataSource.Keys); string actualOutputFolder = outputFolder; string outputFileName = DataSource.GetOutputFileName(DataSource.Id, DataSource.Keys, outputFolder, DataSource.Id.ToString()); if (File.Exists(outputFileName)) { string buName = Path.Combine(outputFolder, string.Format("{0}{1}_{2}", DataSource.Name, Guid.NewGuid().ToString(), Path.GetFileName(outputFileName))); new FileUtility().FileCopy(outputFileName, buName, true); } if (result.Length > 0) { using (StreamWriter tw = new StreamWriter(outputFileName)) { tw.Write(result); tw.Close(); } DataSource.TraceInformation("{0} - A data set from {1} was successfully processed. Output file was {2}", DataSource.Name, DataSource.Name, outputFileName); Registry.Instance.Pullers.InvokeFileProcessed(DataSource.Id, jobProcessor.JobId, DataSource.Keys, outputFileName, outputFolder, string.Empty); } else { DataSource.TraceInformation("{0} - Failed to process the SQL data set from '{1}', empty data came from output writer! Check log for more details.", DataSource.Name, DataSource.Name); } jobProcessor.Dispose(); } } this.IsRunning = false; timer.Enabled = true; } } catch (Exception exp) { this.IsRunning = false; DataSource.TraceError(exp.ToString()); timer.Enabled = true; } Trace.Flush(); }
internal void FileDownloaded(FileSystemWatcherEventArgs e) { #region Getting all required information int dataSourceId = 0; int.TryParse(e.ApplicationParameters["DataSourceId"].ToString(), out dataSourceId); if (dataSourceId == 0) { return; } string processingBy = "u175675"; //todo string onlyFileName = Path.GetFileNameWithoutExtension(e.FileName); string fileExt = Path.GetExtension(e.FileName); List <SreKey> keys = Cache.Instance.Bag[dataSourceId + ".keys"] as List <SreKey>; if (keys == null) { if (_Manager == null) { _Manager = new Manager(); } keys = _Manager.GetApplicationKeys(dataSourceId, true); Cache.Instance.Bag.Add(dataSourceId + ".keys", keys); } string OutputFolder = DataSource.GetOutputFolder(dataSourceId, keys); string actualOutputFolder = OutputFolder; string outputExtension = keys.GetKeyValue(SreKeyTypes.OutputFileExtension); if (string.IsNullOrEmpty(outputExtension)) { outputExtension = ".xml"; } string outputFileName = string.Empty; string outputFileNameKey = keys.GetKeyValue(SreKeyTypes.OutputFileName); if (string.IsNullOrEmpty(outputFileNameKey)) { outputFileName = Path.Combine(OutputFolder, onlyFileName + outputExtension); } else { outputFileName = FormatOutputFileName(outputFileNameKey, onlyFileName); outputFileName = Path.Combine(OutputFolder, outputFileName + outputExtension); } Directory.CreateDirectory(Path.GetDirectoryName(outputFileName)); string withError = string.Empty; string withWarning = string.Empty; string appWatchFilter = keys.GetKeyValue(SreKeyTypes.WatchFilter); string zipInterfaceName = keys.GetKeyValue(SreKeyTypes.ZipInterfaceName); if ((fileExt.ToLower() == ".zip") || (fileExt.ToLower() == ".rar") || (fileExt.ToLower() == ".tar")) { OutputFolder = Path.Combine(SymplusCoreConfigurationSection.CurrentConfig.TempDirectory, Constants.SREBaseFolderName); OutputFolder = Path.Combine(OutputFolder, "RedirectedOutput"); OutputFolder = Path.Combine(OutputFolder, DateTime.Now.ToDBDateFormat()); OutputFolder = Path.Combine(OutputFolder, dataSourceId.ToString()); } if ((!string.IsNullOrEmpty(appWatchFilter)) && (appWatchFilter != FileExtensionSupportAll)) { List <string> filters = new List <string>(); if (appWatchFilter.Contains("|")) { filters.AddRange(appWatchFilter.ToLower().Split("|".ToCharArray())); } else { filters.Add(appWatchFilter.ToLower()); } var filterOrNot = (from f in filters where f == e.FileExtension.ToLower() select f).SingleOrDefault(); if (filterOrNot == null) { if (!onlyFileName.StartsWith(Constants.UnzippedFilePrefix)) { SreMessage warn = new SreMessage(SreMessageCodes.SRE_FILE_TYPE_NOT_SUPPORTED); DataSource dataSource = new DataSource(dataSourceId, string.Empty); withWarning = string.Format(warn.Message, dataSource.Name, appWatchFilter, Path.GetFileName(e.FileName)); Trace.TraceInformation(withWarning); new PostMan(dataSource).Send(PostMan.__warningStartTag + withWarning + PostMan.__warningEndTag, "File Ignored"); return; } } } string zipUniuqeId = string.Empty; bool isRequestFromWCF = false; string jobId = string.Empty; if (onlyFileName.StartsWith(Constants.WCFFilePrefix)) { isRequestFromWCF = true; jobId = onlyFileName.Replace(Constants.WCFFilePrefix, ""); jobId = jobId.Replace(fileExt, ""); } else if (onlyFileName.StartsWith(Constants.UnzippedFilePrefix)) { zipUniuqeId = ZipFileWatcher.ExtractUniqueId(onlyFileName); OutputFolder = Path.Combine(OutputFolder, zipUniuqeId); if (!Directory.Exists(OutputFolder)) { Directory.CreateDirectory(OutputFolder); } outputFileName = Path.Combine(OutputFolder, onlyFileName + outputExtension); outputFileName = ZipFileWatcher.ExtractActualFileName(outputFileName); } #endregion Getting all required information if (!isRequestFromWCF) { Trace.TraceInformation("Got a new file for {0} - '{1}'", dataSourceId, e.FileName); } else { Trace.TraceInformation("Got a new WCF request for {0}, JobId = '{1}'", dataSourceId, jobId); } if (DataSourceIsDisabled(dataSourceId, e)) { return; } StringBuilder result = new StringBuilder(); JobProcessor jobProcessor = new JobProcessor(); if (isRequestFromWCF) { jobProcessor.IsWCFRequest = true; result = jobProcessor.ProcessJob(dataSourceId, string.Empty, processingBy, onlyFileName, jobId, withError, withWarning); } else if ((fileExt.ToLower() == ".zip") || (fileExt.ToLower() == ".rar") || (fileExt.ToLower() == ".tar")) { zipUniuqeId = new ZipFileWatcher(e.FileName, dataSourceId, zipInterfaceName, processingBy, OutputFolder, e.RenamedToIdentifier).Handle(); OutputFolder = Path.Combine(DataSource.GetOutputFolder(dataSourceId, keys), zipUniuqeId); if (!Directory.Exists(OutputFolder)) { Directory.CreateDirectory(OutputFolder); } outputFileName = Path.Combine(OutputFolder, onlyFileName + fileExt); result.AppendLine(zipUniuqeId); } else if ((fileExt.ToLower() == ".xls") || (fileExt.ToLower() == ".xlsx")) { result = jobProcessor.ProcessSpreadSheet(dataSourceId, string.Empty, processingBy, e.FileName); } else if (fileExt.ToLower() == ".edi") { new EDIX12FileWatcher(dataSourceId, e.FileName).Process(); Trace.TraceInformation("{0} successfully processed. Output file was {1}", e.FileName, outputFileName); if (File.Exists(outputFileName)) { InvokeFileProcessed(dataSourceId, jobProcessor.JobId, keys, outputFileName, actualOutputFolder, zipUniuqeId); } jobProcessor.Dispose(); return; } else { result = jobProcessor.ProcessJob(dataSourceId, string.Empty, processingBy, e.FileName, string.Empty, withError, withWarning); } if (File.Exists(outputFileName)) { string buName = Path.Combine(OutputFolder, string.Format("{0}_{1}", e.RenamedToIdentifier, Path.GetFileName(outputFileName))); FileCopy(outputFileName, buName, true); //backup existing } if (((fileExt.ToLower() == ".zip") || (fileExt.ToLower() == ".rar") || (fileExt.ToLower() == ".tar")) && (keys.GetKeyValue(SreKeyTypes.ZipDoNotCreateAcknoledgementInOutputFolder).ParseBool())) { Trace.TraceInformation("The data source '{0}' has been configured as not to copy zip acknoledgement file. File will not be created!", dataSourceId); return; } if (result.Length > 0) { using (StreamWriter tw = new StreamWriter(outputFileName)) { tw.Write(result); tw.Close(); } Trace.TraceInformation("{0} successfully processed. Output file was {1}", e.FileName, outputFileName); InvokeFileProcessed(dataSourceId, jobProcessor.JobId, keys, outputFileName, actualOutputFolder, zipUniuqeId); } else { Trace.TraceInformation("Failed to process '{0}', empty data came from output writer! Check log for more details.", e.FileName); } jobProcessor.Dispose(); }