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."); } }
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(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); }