Ejemplo n.º 1
0
        public void ExeSyncCmd(string sourcePath, string targetPath)
        {
            _errorMessage = "File sync is not finish.";

            if (!Directory.Exists(RsyncSetting.Config.RsyncExeLogPath))
            {
                Directory.CreateDirectory(RsyncSetting.Config.RsyncExeLogPath);
            }

            var targetDir = string.Join("\\", targetPath.Split('\\').Take(targetPath.Split('\\').Length - 1));

            if (Directory.Exists(targetDir))
            {
                Directory.Delete(targetDir, true);
            }
            Directory.CreateDirectory(targetDir);

            string rsyncExe = "", rsyncExePath = "";

            rsyncExe = RsyncSetting.Config.RsyncExeName + " " + RsyncSetting.Config.RsyncExeOption + " \""
                       + RsyncSetting.ConvertToRsyncFormat(sourcePath) + "\" \""
                       + RsyncSetting.ConvertToRsyncFormat(targetPath) + "\" "
                       + " --log-file=" + RsyncSetting.GetRsyncLogFileFullPath()
                       + " --log-file-format=\"%i %o %f\"";
            rsyncExePath = RsyncSetting.Config.RsyncExePath;
            Log.WriteLog("Cmd execute: " + rsyncExe);
            ExeCmd(rsyncExe, rsyncExePath);
        }
        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.");
        }