Example #1
0
        /// <summary>
        /// 处理文件 暂时只考虑Robocopy
        ///
        /// 构思:读日志、预处理、解析、存数据库
        /// </summary>
        /// <param name="fpath"></param>
        public void Analyse(Config config)
        {
            lock (_lock)
            {
                try
                {
                    //判断当前时间与配置时间是否匹配
                    if (TimeCheckHelper.CheckTime("readLogTime", config.ReadLogTime))
                    {
                        Common.Util.LogManager.WriteLog(Common.Util.LogFile.LogAnalyser, "start");

                        #region process
                        FileSystemUtil fsu = new FileSystemUtil();

                        //获取所有日志文件
                        IEnumerable <FileInfo> copyLogList = GetFileInfos(config.Path.InputLogPath);
                        if (copyLogList == null)
                        {
                            //执行了一个任务
                            Signal.CompletedTaskCount++;

                            Common.Util.LogManager.WriteLog(Common.Util.LogFile.LogAnalyser, "copyLogList is null");
                            return;
                        }

                        foreach (FileInfo fileinfo in copyLogList)
                        {
                            Common.Util.LogManager.WriteLog(Common.Util.LogFile.LogAnalyser, fileinfo.FullName);
                            #region 处理日志文件
                            ILogReader ilr = null;
                            try
                            {
                                //获取出错文件
                                ilr = new RoboCopyLogReader()
                                {
                                    Path = fileinfo.FullName
                                };
                                ErrorPathInfo epi = ilr.GetErrorList();
                                if (epi != null && epi.PathList != null && epi.PathList.Count > 0)
                                {
                                    //预先过滤不必要文件
                                    ErrorPathFilter.PreFilter(epi);
                                    if (epi.PathList.Count > 0)
                                    {
                                        //获取文件详细信息,然后判断是否更新,若更新则执行copy
                                        SSHCopyManager scm = new SSHCopyManager();
                                        scm.SSHCopy(config, epi);
                                    }
                                }
                            }
                            catch (System.Exception ex)
                            {
                                Common.Util.LogManager.WriteLog(Common.Util.LogFile.LogAnalyser, MessageUtil.GetExceptionMsg(ex, ""));
                                break;
                            }
                            finally
                            {
                                //释放资源
                                if (ilr != null)
                                {
                                    ilr.Dispose();
                                }
                            }
                            #endregion

                            //删除日志文件
                            fsu.DeleteFile(fileinfo);
                        }
                        #endregion

                        Common.Util.LogManager.WriteLog(Common.Util.LogFile.LogAnalyser, "end");

                        //执行了一个任务
                        Signal.CompletedTaskCount++;
                    }
                }
                catch (System.Exception ex)
                {
                    Common.Util.LogManager.WriteLog(Common.Util.LogFile.LogAnalyser, MessageUtil.GetExceptionMsg(ex, ""));
                }
            }
        }
Example #2
0
        /// <summary>
        /// 判断文件是否发生变化,若变化则copy文件
        /// </summary>
        /// <param name="config"></param>
        /// <param name="epi"></param>
        public void SSHCopy(Config config, ErrorPathInfo epi)
        {
            //获取mac端信息
            MonitorServerDAL msd = new MonitorServerDAL();
            MonitorServer    ms  = msd.GetMonitorServer(config, epi);

            if (ms == null)
            {
                Common.Util.LogManager.WriteLog(LogFile.Warning, "there is no MonitorServer that meet the conditions !");
                return;
            }
            SFTPProxy sftpProxy = null;
            var       sshlog    = new Common.Util.SSHLogManager();

            try
            {
                sftpProxy = new SFTPProxy(ms.monitorServerIP, ms.account, ms.password);
                #region 处理
                foreach (ErrorEntry ee in epi.PathList)
                {
                    string macPath = string.Empty;
                    try
                    {
                        //获取mac路径
                        macPath = MacPathConvert(epi.source, ee.Path, ms.monitorMacPath, ms.startFile);

                        //获取远端文件属性
                        SftpFile sf = sftpProxy.GetFileInfo(macPath);

                        if (!ErrorPathFilter.IsUpdate(config, sf, ms))
                        {
                            continue;
                        }
                        if (sf.IsDirectory)
                        {
                            #region  除job端已经删除的文件

                            #endregion
                            foreach (SftpFile sfile in sftpProxy.Ls(sf))
                            {
                                #region  过滤无关文件
                                if (String.Compare(sfile.Name.Trim('\r'), ".DS_Store", true) == 0 ||
                                    String.Compare(sfile.Name.Trim('\r'), ".com.apple.timemachine.supported", true) == 0 ||
                                    String.Compare(sfile.Name.Trim('\r'), "Icon", true) == 0)
                                {
                                    continue;
                                }
                                #endregion
                                if (!ErrorPathFilter.IsUpdate(config, sfile, ms))
                                {
                                    continue;
                                }

                                SSHCopyFile(sfile, ms, config.Path.OutputPath, sftpProxy);
                            }
                        }
                        else
                        {
                            SSHCopyFile(sf, ms, config.Path.OutputPath, sftpProxy);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Common.Util.LogManager.WriteLog(LogFile.Error, MessageUtil.GetExceptionMsg(ex, ""));
                        sshlog.WriteLog(new Common.Util.SSHLog()
                        {
                            DateTime = DateTime.Now, LogType = Common.Util.SSHLogType.Failure, Message = ee.Path
                        });
                    }
                }
                #endregion
            }
            catch (System.Exception ex)
            {
                Common.Util.LogManager.WriteLog(LogFile.Error, MessageUtil.GetExceptionMsg(ex, ""));
            }
            finally
            {
                if (sftpProxy != null)
                {
                    sftpProxy.Close();
                }
            }
        }