protected override void ExecuteInternal(JobExecutionContext context) { string connStr = ConfigurationManager.AppSettings["mergeData"]; OracleConnection conn = new OracleConnection(connStr); conn.Open(); OracleTransaction tran = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); var startTime = DateTime.UtcNow; var logEntity = new SCHEDULERLOG { STARTTIME = startTime }; var strInfo = new StringBuilder(); strInfo.AppendFormat("Source [Email:{0},Title:{1}]\n", ConfigurationManager.AppSettings["LongZhongSenderFilter"], ConfigurationManager.AppSettings["LongZhongTitleFilter"]); strInfo.AppendFormat("Destination [Type: Oracle DB, Address: {0}]\n", connStr); var lastSyncTime = Convert.ToDateTime("2016-11-16"); using (var cneEntities = new CnEEntities()) { var date = cneEntities.SCHEDULERLOGs.Where(x => x.STATUS == 0 && x.JOBTYPE == JobType).Select( x => (DateTime?)x.STARTTIME).Max(); if (date != null) { //ToGMT8 lastSyncTime = date.Value.AddHours(8); } } try { #region 执行数据同步程序 var attFetcherSever = ConfigurationManager.AppSettings["CnEServer"]; int attFetcherPort = int.Parse(ConfigurationManager.AppSettings["CnEPort"]); bool attFetcherUsingSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["CnEUsingSsl"]); string attFetcherUserName = ConfigurationManager.AppSettings["CnEUserName"]; string attFetcherPassWord = ConfigurationManager.AppSettings["CnEPassWord"]; string attFetcherSavingPath = ConfigurationManager.AppSettings["LongZhongSavingPath"]; var attFetcher = new IMAP4AttFetcher(attFetcherSever, attFetcherPort, attFetcherUsingSsl, attFetcherUserName, attFetcherPassWord, attFetcherSavingPath); var attFetcherSenderFilter = new SenderFilter(); /*Filter example. If there are more filters , please write like below.*/ string[] senders = ConfigurationManager.AppSettings["LongZhongSenderFilter"].Split(';'); foreach (var sender in senders) { var senderRegex = new Regex(sender, RegexOptions.Compiled); attFetcherSenderFilter.SetRule(senderRegex); } attFetcher.AppendFilter(attFetcherSenderFilter); var attFetcherTitleFilter = new TitleFilter(); var titleRegex = new Regex(ConfigurationManager.AppSettings["LongZhongTitleFilter"], RegexOptions.Compiled); attFetcherTitleFilter.SetRule(titleRegex); attFetcher.AppendFilter(attFetcherTitleFilter); attFetcher.Execute(lastSyncTime); /*Get the saved attachment names.*/ var attachmentFileNames = attFetcher.GetAttachmentFileNames(); //List<string> attachmentFileNames = Directory.GetFiles(@"C:\DataFeedApp\Scheduler\CnE\LongZhong").ToList<string>(); foreach (var tempattachname in attachmentFileNames) { LongZhongExcelManager manager = new LongZhongExcelManager(); manager.GetCellsByFirstSheet(tempattachname, strInfo); } var fold = new DirectoryInfo(attFetcherSavingPath); if (fold.Exists) { FileInfo[] files = fold.GetFiles(); foreach (FileInfo f in files)//删除目录下所有文件 { f.Delete(); } } if (attachmentFileNames.Count > 0) { MergeData merge = new MergeData(); merge.ExecuteLongZhongExcel(conn, tran); tran.Commit(); strInfo.Append("Execute Procedure : CREATEMAXENERGYYIELDTABLE, CREATEMAXCHEMISTRYOUTPUTTABLE \r\n"); strInfo.Append("Insert Into Table : GDT_CHEMISTRYOUTPUTMAX, GDT_ENERGYYIELDMAX \r\n"); } else { strInfo.Append("No files found\r\n"); } #endregion var endTime = DateTime.UtcNow; logEntity.ENDTIME = endTime; logEntity.JobStatus = JobStatus.Success; logEntity.RUNDETAIL = strInfo.ToString(); WriteLogEntity(logEntity); //merge data from temp to persistence } catch (Exception exception) { tran.Rollback(); logEntity.ENDTIME = DateTime.UtcNow.AddDays(-1); logEntity.JobStatus = JobStatus.Fail; logEntity.RUNDETAIL = strInfo + "\n" + exception; WriteLogEntity(logEntity); } finally { conn.Close(); conn.Dispose(); } }
protected override void ExecuteInternal(JobExecutionContext context) { var startTime = DateTime.UtcNow; var logEntity = new SCHEDULERLOG { STARTTIME = startTime }; var strInfo = new StringBuilder(); strInfo.AppendFormat("Source [Type: {0} Address: {1} Title: {2}]\n", "Email", ConfigurationManager.AppSettings["OilChemSenderFilter"], ConfigurationManager.AppSettings["OilChemTitleFilter"]); strInfo.AppendFormat("Destination [Type: {0} Address: {1}]\n", "ORACLE", ConfigurationManager.AppSettings["CnECon"]); var lastSyncTime = DateTime.Now; using (var cneEntities = new CnEEntities()) { var date = cneEntities.SCHEDULERLOGs.Where(x => x.STATUS == 0 && x.JOBTYPE == JobType).Select( x => (DateTime?)x.STARTTIME).Max(); if (date != null) { lastSyncTime = date.Value; } } try { #region 执行数据同步程序 /*Attachment fetcher begins.*/ string attFetcherSever = ConfigurationManager.AppSettings["CnEServer"]; int attFetcherPort = int.Parse(ConfigurationManager.AppSettings["CnEPort"]); bool attFetcherUsingSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["CnEUsingSsl"]); string attFetcherUserName = ConfigurationManager.AppSettings["CnEUserName"]; string attFetcherPassWord = ConfigurationManager.AppSettings["CnEPassWord"]; string attFetcherSavingPath = ConfigurationManager.AppSettings["OilChemSavingPath"]; var attFetcher = new IMAP4AttFetcher(attFetcherSever, attFetcherPort, attFetcherUsingSsl, attFetcherUserName, attFetcherPassWord, attFetcherSavingPath); var attFetcherSenderFilter = new SenderFilter(); /*Filter example. If there are more filters , please write like below.*/ string[] senders = ConfigurationManager.AppSettings["OilChemSenderFilter"].Split(';'); foreach (var sender in senders) { var senderRegex = new Regex(sender, RegexOptions.Compiled); attFetcherSenderFilter.SetRule(senderRegex); } attFetcher.AppendFilter(attFetcherSenderFilter); var attFetcherTitleFilter = new TitleFilter(); var titleRegex = new Regex(ConfigurationManager.AppSettings["OilChemTitleFilter"], RegexOptions.Compiled); attFetcherTitleFilter.SetRule(titleRegex); attFetcher.AppendFilter(attFetcherTitleFilter); attFetcher.Execute(lastSyncTime); /*Get the saved attachment names.*/ var attachmentFileNames = attFetcher.GetAttachmentFileNames(); foreach (var tempattachname in attachmentFileNames) { var eos = new EnergyOilShandong(tempattachname); eos.ImportTheWholeExcel(strInfo); } var fold = new DirectoryInfo(attFetcherSavingPath); if (fold.Exists) { FileInfo[] files = fold.GetFiles(); foreach (FileInfo f in files)//删除目录下所有文件 { f.Delete(); } } #endregion var endTime = DateTime.UtcNow; logEntity.ENDTIME = endTime; logEntity.JobStatus = JobStatus.Success; logEntity.RUNDETAIL = strInfo.ToString(); WriteLogEntity(logEntity); } catch (Exception exception) { logEntity.ENDTIME = DateTime.UtcNow.AddDays(-1); logEntity.JobStatus = JobStatus.Fail; logEntity.RUNDETAIL = strInfo + "\n" + exception; WriteLogEntity(logEntity); } }
protected override void ExecuteInternal(JobExecutionContext context) { var connStr = ConfigurationManager.AppSettings["mergeData"]; var conn = new OracleConnection(connStr); conn.Open(); OracleTransaction tran = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); var startTime = DateTime.UtcNow; var logEntity = new SCHEDULERLOG { STARTTIME = startTime }; var strInfo = new StringBuilder(); strInfo.AppendFormat("Source [Email:{0}]\n", ConfigurationManager.AppSettings["CoffedSenderFilter"]); strInfo.AppendFormat("Destination [Type: Oracle DB, Address: {0}]\n", connStr); strInfo.AppendFormat("Destination Table:Cofeed\n" + "\r\n"); var lastSyncTime = Convert.ToDateTime("2016-09-03"); using (var cneEntities = new CnEEntities()) { var date = cneEntities.SCHEDULERLOGs.Where(x => x.STATUS == 0 && x.JOBTYPE == JobType).Select( x => (DateTime?)x.STARTTIME).Max(); if (date != null) { //ToGMT8 lastSyncTime = date.Value.AddHours(8); } } try { StringBuilder sb = new StringBuilder(); #region 执行数据同步程序 var attFetcherSever = ConfigurationManager.AppSettings["CnEServer"]; int attFetcherPort = int.Parse(ConfigurationManager.AppSettings["CnEPort"]); bool attFetcherUsingSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["CnEUsingSsl"]); string attFetcherUserName = ConfigurationManager.AppSettings["CnEUserName"]; string attFetcherPassWord = ConfigurationManager.AppSettings["CnEPassWord"]; string attFetcherSavingPath = ConfigurationManager.AppSettings["CoffedSavingPath"]; var attFetcher = new IMAP4AttFetcher(attFetcherSever, attFetcherPort, attFetcherUsingSsl, attFetcherUserName, attFetcherPassWord, attFetcherSavingPath); var attFetcherSenderFilter = new SenderFilter(); string[] senders = ConfigurationManager.AppSettings["CoffedSenderFilter"].Split(';'); foreach (var sender in senders) { var senderRegex = new Regex(sender, RegexOptions.Compiled); attFetcherSenderFilter.SetRule(senderRegex); } attFetcher.AppendFilter(attFetcherSenderFilter); var attFetcherTitleFilter = new TitleFilter(); var titleRegex = new Regex(ConfigurationManager.AppSettings["CoffedTitleFilter"], RegexOptions.Compiled); attFetcherTitleFilter.SetRule(titleRegex); attFetcher.AppendFilter(attFetcherTitleFilter); attFetcher.Execute(lastSyncTime); var attachmentFileNames = attFetcher.GetAttachmentFileNames(); foreach (var tempattachname in attachmentFileNames) { CofeedManager manager = new CofeedManager(); manager.GetCellsByFirstSheet(tempattachname, sb); } strInfo.Append(sb); var fold = new DirectoryInfo(attFetcherSavingPath); if (fold.Exists) { FileInfo[] files = fold.GetFiles(); foreach (FileInfo f in files)//删除目录下所有文件 { f.Delete(); } } #endregion if (attachmentFileNames.Count > 0) { MergeData merge = new MergeData(); merge.Execute(conn, tran); tran.Commit(); strInfo.Append("Execute Procedure : createMaxAgricultrueTable \r\n"); strInfo.Append("Insert Into Table : GDT_AgricultureMax \r\n"); } else { strInfo.Append("No files found\r\n"); } var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"config\Ftp-Cofeed-data-sync.xml"); var settingManager = new FtpSyncXmlManager(File.ReadAllText(settingFilePath), lastSyncTime, startTime.AddHours(8)); settingManager.Init(); var ftpSync = new FtpSyncLoad(settingManager, strInfo); ftpSync.Excute(); var endTime = DateTime.UtcNow; logEntity.ENDTIME = endTime; logEntity.JobStatus = JobStatus.Success; logEntity.RUNDETAIL = strInfo.ToString(); WriteLogEntity(logEntity); } catch (Exception exception) { tran.Rollback(); logEntity.ENDTIME = DateTime.UtcNow.AddDays(-1); logEntity.JobStatus = JobStatus.Fail; logEntity.RUNDETAIL = strInfo + "\n" + exception; WriteLogEntity(logEntity); } finally { conn.Close(); } }
public static void ExcuteMailSync(string jobType, DateTime syncStartTime, string attFetcherSavingPath, string senderFilter, string titleFilter, string userName, string password , Action <SCHEDULERLOG> saveLog , Action <List <string>, StringBuilder> syncCallback , Action <OracleConnection, OracleTransaction> mergeCallback ) { string connStr = ConfigurationManager.AppSettings["mergeData"]; OracleConnection conn = new OracleConnection(connStr); conn.Open(); OracleTransaction tran = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); var startTime = DateTime.UtcNow; var logEntity = new SCHEDULERLOG { STARTTIME = startTime }; var strInfo = new StringBuilder(); var lastSyncTime = syncStartTime; using (var cneEntities = new CnEEntities()) { var date = cneEntities.SCHEDULERLOGs.Where(x => x.STATUS == 0 && x.JOBTYPE == jobType).Select( x => (DateTime?)x.STARTTIME).Max(); if (date != null) { lastSyncTime = date.Value; } } try { #region 执行数据同步程序 strInfo.AppendFormat("Source [Type: Email , Sender: {0} ,Title {1} ]\n", senderFilter, titleFilter); strInfo.AppendFormat("Destination [Type: {0} Address: {1}]\n", "Oracle", connStr); var attFetcherSever = ConfigurationManager.AppSettings["CnEServer"]; int attFetcherPort = int.Parse(ConfigurationManager.AppSettings["CnEPort"]); bool attFetcherUsingSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["CnEUsingSsl"]); string attFetcherUserName = userName; string attFetcherPassWord = password; var attFetcher = new IMAP4AttFetcher(attFetcherSever, attFetcherPort, attFetcherUsingSsl, attFetcherUserName, attFetcherPassWord, attFetcherSavingPath); var attFetcherSenderFilter = new SenderFilter(); /*Filter example. If there are more filters , please write like below.*/ string[] senders = senderFilter.Split(';'); foreach (var sender in senders) { var senderRegex = new Regex(sender, RegexOptions.Compiled); attFetcherSenderFilter.SetRule(senderRegex); } attFetcher.AppendFilter(attFetcherSenderFilter); if (!string.IsNullOrEmpty(titleFilter)) { var attFetcherTitleFilter = new TitleFilter(); var titleRegex = new Regex(titleFilter, RegexOptions.Compiled); attFetcherTitleFilter.SetRule(titleRegex); attFetcher.AppendFilter(attFetcherTitleFilter); } attFetcher.Execute(lastSyncTime); /*Get the saved attachment names.*/ var attachmentFileNames = attFetcher.GetAttachmentFileNames(); //var attachmentFileNames=new List<string>(); //attachmentFileNames.Add(@"C:\DataFeedApp\Scheduler\CnE\路透数据20161109.xls"); StringBuilder sb = new StringBuilder(); //var attachmentFileNames = Directory.GetFiles(@"C:\xx\troil").ToList<string>(); syncCallback(attachmentFileNames, sb); var fold = new DirectoryInfo(attFetcherSavingPath); if (fold.Exists) { FileInfo[] files = fold.GetFiles(); foreach (FileInfo f in files)//删除目录下所有文件 { f.Delete(); } } if (attachmentFileNames.Count == 0) { strInfo.Append("No find files .\r\n"); } #endregion var endTime = DateTime.UtcNow; logEntity.ENDTIME = endTime; logEntity.JobStatus = JobStatus.Success; logEntity.RUNDETAIL = strInfo + sb.ToString(); saveLog(logEntity); //merge data from temp to persistence if (mergeCallback != null) { mergeCallback(conn, tran); } tran.Commit(); } catch (Exception exception) { tran.Rollback(); logEntity.ENDTIME = DateTime.UtcNow.AddDays(-1); logEntity.JobStatus = JobStatus.Fail; logEntity.RUNDETAIL = strInfo + "\n" + exception; saveLog(logEntity); } finally { conn.Close(); conn.Dispose(); } }