public static void WriteLog(string log, Type type = Type.Info) { //string prefix = "[Schedule:" + RsyncSetting.RuntimeSettings.ScheduleID + "]"; string prefix = ""; switch (type) { case Type.Info: prefix += "[Info]"; break; case Type.Failed: prefix += "[Failed]"; break; case Type.Exception: prefix += "[Exception]"; break; } Console.WriteLine(prefix + " " + log); var path = RsyncSetting.Config.LogPath + RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearMonth) + "\\"; CommonLibrary.LogHelper.Write(prefix + " " + log, RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearMonthDate) + ".txt", path); }
public static string GetRsyncLogFileFullPath() { string LogPath = Config.RsyncExeLogPath; if (!(LogPath.Substring(LogPath.Length - 1) == "/" || LogPath.Substring(LogPath.Length - 1) == "\\")) { LogPath += "\\"; } return(ConvertToRsyncFormat(LogPath + RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearMonthDate) + "_" + Config.RsyncExeLogFileName)); }
private string GetContent() { var content = GetHtmlTemplate().Replace('"', '\"'); content = content.Replace("{RsyncFromHost}", RsyncSetting.Config.SourceServerHost); content = content.Replace("{RsyncFromHostFolder}", ""); content = content.Replace("{RsyncToDestination}", RsyncSetting.Config.TargetServerHost); content = content.Replace("{RsyncToDestinationFolder}", ""); content = content.Replace("{RsyncLog}", RsyncSetting.RuntimeSettings.RsyncResultMessage); content = content.Replace("{count}", ""); content = content.Replace("{RsyncStartTime}", RsyncSetting.RuntimeSettings.ScheduleStartTime); content = content.Replace("{RsyncFinishTime}", RsyncDateTime.GetNow(RsyncDateTime.TimeFormatType.YearSMonthSDateTimeChange)); return(content); }
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."); }