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)); }
private static void DeleteData(IEnumerable <dynamic> delDataList) { var errorMessage = ""; string id, size, sourcePath, targetPath; var rsyncCmd = new RsyncCommand(); SyncResultRecords.SyncResult resultRecord; foreach (var delData in delDataList) { id = size = sourcePath = targetPath = ""; try { id = delData.id; size = ""; targetPath = GetReplaceHostPath(delData.target.ToString()); Log.WriteLog("Start delete from factory.(id:" + id + ")"); Log.WriteLog("targetPath :" + targetPath); var targetDir = string.Join("\\", targetPath.Split('\\').Take(targetPath.Split('\\').Length - 1)); if (Directory.Exists(targetDir)) { Directory.Delete(targetDir, true); } resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearSMonthSDateTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Success", Message = targetPath.Replace("\\", "/") + " deleted is success.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } catch (Exception e) { Log.WriteLog(e.Message, Log.Type.Exception); resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearSMonthSDateTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Failed", Message = targetPath.Replace("\\", "/") + " deleted is failed.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } if (errorMessage != "") { resultRecord.Status = "Failed"; resultRecord.Message += (resultRecord.Message != "" ? " " : "") + errorMessage; } SyncResultRecords.Add(resultRecord); Log.WriteLog("Delete from factory is finish."); } }
private static void SyncData(IEnumerable <dynamic> syncDataList) { var errorMessage = ""; string id, size, sourcePath, targetPath; var rsyncCmd = new RsyncCommand(); SyncResultRecords.SyncResult resultRecord; foreach (var syncData in syncDataList) { id = size = sourcePath = targetPath = ""; try { id = syncData.id; size = ""; sourcePath = GetReplaceHostPath(syncData.source.ToString()); targetPath = GetReplaceHostPath(syncData.target.ToString()); Log.WriteLog("Start sync to factory.(id:" + id + ")"); Log.WriteLog(sourcePath + " -> " + targetPath); if (!File.Exists(sourcePath)) { Log.WriteLog("No such file or directory.", Log.Type.Failed); resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearSMonthSDateTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Failed", Message = "No such file or directory.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } else { FileInfo file = new FileInfo(sourcePath); size = file.Length.ToString(); rsyncCmd.ExeSyncCmd(sourcePath, targetPath); if (rsyncCmd.ErrorMessage != "") { throw new Exception(rsyncCmd.ErrorMessage); } resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearSMonthSDateTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Success", Message = targetPath.Replace("\\", "/") + " is already sync to factory.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } } catch (Exception e) { Log.WriteLog(e.Message, Log.Type.Exception); resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearSMonthSDateTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Failed", Message = targetPath.Replace("\\", "/") + " sync is failed.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } if (errorMessage != "") { resultRecord.Status = "Failed"; resultRecord.Message += (resultRecord.Message != "" ? " " : "") + errorMessage; } SyncResultRecords.Add(resultRecord); Log.WriteLog("Sync to factory is finish."); } }
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."); }
private static void SyncData(JArray syncdata, JArray storages) { //var storages = sourceData["storages"]; //var syncdata = sourceData["syncdata"]; var blob = new StorageService(storages); string id, size, sourcePath, targetPath, errorMessage; SyncResultRecords.SyncResult resultRecord; int SyncToAzureSuccessCount = 0; foreach (var syncInfo in syncdata) { errorMessage = ""; size = ""; id = syncInfo["id"].ToString(); sourcePath = syncInfo["source"].ToString(); targetPath = syncInfo["target"].ToString(); sourcePath = ComposeFilePath(sourcePath, true); targetPath = ComposeFilePath(targetPath, false); Log.WriteLog("Start Sync to Azure Storage.(id:" + id + ")"); Log.WriteLog(sourcePath + " -> " + targetPath); if (!File.Exists(sourcePath)) { Log.WriteLog("No such file or directory.", Log.Type.Failed); resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = Time.GetNow(Time.TimeFormatType.YearSMonthSDayTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Failed", Message = "No such file or directory.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } else { try { FileInfo file = new FileInfo(sourcePath); size = file.Length.ToString(); blob.SyncFileToAzureBlob(sourcePath, targetPath).Wait(); SyncToAzureSuccessCount++; resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = Time.GetNow(Time.TimeFormatType.YearSMonthSDayTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Success", Message = "Sync to azure storage is success.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } catch (Exception e) { Log.WriteLog(e.Message, Log.Type.Exception); resultRecord = new SyncResultRecords.SyncResult { Id = id, Size = size, FinishTime = Time.GetNow(Time.TimeFormatType.YearSMonthSDayTimeChange), SourcePath = sourcePath, TargetPath = targetPath, Status = "Exception", Message = "Sync to azure storage is failed.", }; errorMessage = UpdateStatus(new List <SyncResultRecords.SyncResult> { resultRecord }); } } if (errorMessage != "") { resultRecord.Status = "Failed"; resultRecord.Message += (resultRecord.Message != "" ? " " : "") + errorMessage; } SyncResultRecords.Add(resultRecord); Log.WriteLog("Sync to szure storage is finish."); } Setting.SetSyncFileTotalCount(SyncToAzureSuccessCount); }
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); } }