private static string GenerateResultMessage() { string message = ""; int count = 0; SyncResultRecords.All().ForEach(e => { count++; if (e.Status.ToLower() == "failed" || e.Status.ToLower() == "exception") { message += "<tr><td width = \"3%\" bgcolor = \"#EEF4FD\"><font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">" + count + "</font></td>" + "<td width = \"19%\" bgcolor = \"#f28c9b\"><font size = \"2\" face = \"Verdana, sans-serif\">" + e.FinishTime + "</font></td>" + "<td width = \"39%\"><font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">" + e.SourcePath.Replace("\\", "/") + "</font></td>" + "<td width = \"39%\"><font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\"> " + e.Message + "</font></td></tr>"; } else { message += "<tr><td width = \"3%\" bgcolor = \"#EEF4FD\"><font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">" + count + "</font></td>" + "<td width = \"19%\" bgcolor = \"#EEF4FD\"><font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">" + e.FinishTime + "</font></td>" + "<td width = \"39%\"><font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">" + e.SourcePath.Replace("\\", "/") + "</font></td>" + "<td width = \"39%\"><font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">already Copy to Storage " + e.TargetPath.Replace("\\", "/") + "</font></td></tr>"; } }); return(message); }
public void Send() { string ccList = ""; try { string[] ccGroup = RsyncSetting.Config.EmailCC.Split(new char[] { ',' }); for (int i = 0; i < ccGroup.Length; i++) { if (string.IsNullOrWhiteSpace(ccGroup[i])) { continue; } if (!string.IsNullOrWhiteSpace(ccList)) { ccList += ","; } ccList += "\"" + ccGroup[i] + "\""; } bool isImportant = false; SyncResultRecords.All().ForEach(e => { if (e.Status.ToLower() == "failed" || e.Status.ToLower() == "exception") { isImportant = true; } }); string postContent = "{ 'subject' : '" + RsyncSetting.Config.EmailSubject + "'" + ", 'content' : '" + GetContent() + "'" + ", 'To':[" + ccList + "]" + ", 'Cc':'null'" + ", 'Bcc':'null'" + (isImportant ? ", 'Priority':'high'" : "") + " }"; HttpHelper http = new HttpHelper(); var result = http.Post(RsyncSetting.Config.EmailUrl, postContent, HttpHelper.ContnetTypeEnum.Json); if (result == null) { Log.WriteLog("Send mail fail.", Log.Type.Failed); } else { var EmailMessageLog = new JavaScriptSerializer().Deserialize <Dictionary <string, string> >(result); Log.WriteLog("Send mail result :" + EmailMessageLog["message"]); } } catch (Exception e) { Log.WriteLog(e.Message, Log.Type.Exception); } }
public Tuple <string, string> sendMail() { string resultMessage = ""; string errorMessage = ""; try { string emailCC = ""; var emailGroup = Setting.Config.EmailCCList.Split(new char[] { ',' }); for (int i = 0; i < emailGroup.Length; i++) { if (string.IsNullOrWhiteSpace(emailGroup[i])) { continue; } if (!string.IsNullOrWhiteSpace(emailCC)) { emailCC += ","; } emailCC += "\"" + emailGroup[i] + "\""; } bool isImportant = false; SyncResultRecords.All().ForEach(e => { if (e.Status.ToLower() == "failed" || e.Status.ToLower() == "exception") { isImportant = true; } }); var postContent = "{ 'subject' : '" + Setting.Config.EmailSubjectContent + "','content' : '" + GetContent() + "', 'To':[" + emailCC + "],'Cc':'null', 'Bcc':'null'" + (isImportant ? ", 'Priority':'high'" : "") + "}"; HttpHelper http = new HttpHelper(); var result = http.Post(Setting.Config.EmailURL, postContent, HttpHelper.ContnetTypeEnum.Json); if (!string.IsNullOrEmpty(result)) { var ResultMessage = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize <Dictionary <string, string> >(result); resultMessage = ResultMessage["message"]; } } catch (Exception e) { Log.WriteLog(e.Message, Log.Type.Exception); errorMessage = e.Message; } return(Tuple.Create(resultMessage, errorMessage)); }
static void Main(string[] args) { RsyncSetting.SetConfigSettings(); RsyncSetting.RuntimeSettings.ScheduleStartTime = RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearSMonthSDateTimeChange); Log.WriteLog("#######################################################################"); Log.WriteLog("Rsync schedule start."); SyncResultRecords.Init(); if (CheckScheduleReady("DownloadCenterRsync")) { try { //1.取api list var api = new ApiService(); var result = api.GetFileList(); if (string.IsNullOrEmpty(result)) { throw new Exception("Get file list api failed."); } var fileList = JsonConvert.DeserializeObject <dynamic>(result); var syncDataList = (IEnumerable <dynamic>)fileList.syncdata; var delDataList = (IEnumerable <dynamic>)fileList.deletedata; Log.WriteLog("Will to deal records sync(" + syncDataList.Count() + ") + del(" + delDataList.Count() + ")"); if (syncDataList.Count() + delDataList.Count() > 0) { //2.確認工廠連線 if (RsyncSetting.Config.TargetServerTest || !CheckConnectionOK(RsyncSetting.Config.TargetServerIp)) { Log.WriteLog("Reset factory vnet"); api.RestFactoryGatway(); if (!CheckConnectionOK(RsyncSetting.Config.TargetServerIp)) { throw new Exception("Connect to factory (IP:" + RsyncSetting.Config.TargetServerIp + ") failed."); } } //3.確認StoreSimple連線 var cmd = new NetCommand(); if (!cmd.ExeLoginCmd()) { throw new Exception("Not login source server :" + RsyncSetting.Config.SourceServerIP); } if (!cmd.ExeLoginFactoryCmd()) { throw new Exception("Not login target server :" + RsyncSetting.Config.TargetServerIp); } //4.rsync SyncData(syncDataList); DeleteData(delDataList); if (SyncResultRecords.All().Count > 0) { RsyncSetting.RuntimeSettings.RsyncResultMessage = GenerateResultMessage(SyncResultRecords.All()); } } else { Log.WriteLog("No file need to be sync"); RsyncSetting.RuntimeSettings.RsyncResultMessage = "<font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">" + "No file need to be sync </font>"; } } catch (Exception e) { Log.WriteLog(e.Message, Log.Type.Exception); RsyncSetting.RuntimeSettings.RsyncResultMessage = "<font color = \"#c61919\" size = \"2\" face = \"Verdana, sans-serif\">" + e.Message + "</font>"; } } else { Log.WriteLog("Wait for anoher schedule is finish", Log.Type.Failed); RsyncSetting.RuntimeSettings.RsyncResultMessage = "<font color = \"#c61919\" size = \"2\" face = \"Verdana, sans-serif\">" + "Waiting for anoher schedule is finish</font>"; } Mail mail = new Mail(); mail.Send(); Log.WriteLog("Rsync schedule finish."); }
static void Main(string[] args) { try { Setting.SetConfigureSettings(); Setting.SetScheduleStartTime(); Setting.SetScheduleID(Time.GetNow(Time.TimeFormatType.YearMonthDayHourMinute)); Log.WriteLog("#######################################################################"); Log.WriteLog("Schedule Start."); SyncResultRecords.Init(); var isScheduleReady = CheckScheduleReady("DownloadCenter"); if (!isScheduleReady) { Log.WriteLog("Waiting for anoher schedule is finish", Log.Type.Failed); Setting.SetSyncResultMessage( "<font color = \"#c61919\" size = \"2\" face = \"Verdana, sans-serif\">" + "Waiting for anoher schedule is finish</font>"); } else { //1.取SourceSata資料 var api = new FileApi(); JArray syncDatas = null; JArray storages = null; var sourceData = api.GetSyncData(); if (sourceData.Item1 != null) { syncDatas = (JArray)sourceData.Item1["syncdata"]; storages = (JArray)sourceData.Item1["storages"]; Setting.SetAzureStorageRegion(storages); //Setting.SetAzureStorageRegionCount(api.GetStorageRegionCount()); } if (syncDatas == null || syncDatas.Count == 0) { Log.WriteLog("No file need to be sync"); Setting.SetSyncResultMessage( "<font color = \"#4A72A2\" size = \"2\" face = \"Verdana, sans-serif\">" + "No file need to be sync </font>"); } else if (string.IsNullOrEmpty(Setting.RuntimeSettings.SyncAzureStorageRegion)) { Log.WriteLog("Not get azure storage setting", Log.Type.Failed); Setting.SetSyncResultMessage( "<font color = \"#c61919\" size = \"2\" face = \"Verdana, sans-serif\">" + "Not get azure storage setting</font>"); } else { //2.登入StoreSimple var cmd = new NetCommand(); var isLogin = cmd.ExeLoginCmd(); if (!isLogin) { Log.WriteLog("Not login server " + Setting.Config.TargetServerIP, Log.Type.Failed); Setting.SetSyncResultMessage( "<font color = \"#c61919\" size = \"2\" face = \"Verdana, sans-serif\">" + "Not login server " + Setting.Config.TargetServerIP + "</font>"); } else { //3. Sync data SyncData(syncDatas, storages); if (SyncResultRecords.All().Count > 0) { Setting.SetSyncResultMessage(GenerateResultMessage()); } } } } Setting.SetScheduleFinishTime(); var mail = new Mail(); mail.sendMail(); Log.WriteLog("Schedule Finish"); } catch (Exception e) { Log.WriteLog(e.Message, Log.Type.Exception); } }