zoukankan      html  css  js  c++  java
  • 服务程序监控配置是否更改

    服务程序(Service1.cs)
            protected override void OnStart(string[] args)
            {
                //调试用
                //System.Threading.Thread.Sleep(15000);
                log4net.Config.XmlConfigurator.ConfigureAndWatch( new System.IO.FileInfo(Sxmobi.FileHelper.GetMapPath("")+ "log4net.config"));
                _timer = new System.Timers.Timer(_interval);
                _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
                _timer.Start();


            }

           void _timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                //要做的事情
                this._timer.Enabled = false;

                try
                {
                    while (true)
                    {

                        Sxmobi.LogHelper.Info(this.GetType().ToString(), "VP2抓取程序开始", null);
                        TaskProc tp = new TaskProc();
                        tp.LoadTask();
                        WaitHandle.WaitAll(tp.waitHandles);//配置文件更改后此语句会执行

                    }
                }
                catch (Exception ex)
                {
                    Sxmobi.LogHelper.Error(this.GetType().ToString() ,ex.Message,ex);
                    Sxmobi.LogHelper.Info(this.GetType().ToString(), "VP2抓取程序已停止", null);
                }

            }

    监控程序(TaskProc.cs)

            public WaitHandle[] waitHandles;
            public TaskProc()
            {
            }

            public void LoadTask()
            {
                ListParser lp = new ListParser();
                ListParser.iCurrent++;//结束上一个线程
                new Thread(new ThreadStart(lp.RunBat)).Start();
                JianKong();
            }


            void JianKong()
            {
                this.waitHandles = new WaitHandle[] { new AutoResetEvent(false) };
                AutoResetEvent are = (AutoResetEvent)this.waitHandles[0];
                do
                {
                    Thread.Sleep(10000);
                }
                while (!SiteConfig.ConfigIsUpdate);//配置文件更改后执行下面语句

                are.Set();
            }


     

     主程序(ListParser.cs)

    class ListParser
        {
            public static int iCurrent = 0;
            public ListParser() { }

            public void RunBat()
            {
                int ic = iCurrent;
                while (ic==iCurrent)//是否是同一线程
                {
                    RutAllFiles(SiteConfig.BatFileRoot);
                    Thread.Sleep(1000);
                }

            }

            private void RutAllFiles(string fileDirectory)
            {
                DirectoryInfo diSource = new DirectoryInfo(fileDirectory);
                FileSystemInfo[] fsi = diSource.GetFileSystemInfos();
                FileInfo fi;

                try
                {
                    for (int i = 0; i < fsi.Length; i++)
                    {
                        //不是目录,查看文件属性;是目录,继续遍历。
                        if (Directory.Exists(fsi[i].FullName) == false)
                        {
                            fi = new FileInfo(fsi[i].FullName);
                            //判断文件类型,进行相应的后继操作。
                            if (fi.Extension == ".bat")
                            {
                                try
                                {
                                    Process p = new Process();
                                    ProcessStartInfo psi = new ProcessStartInfo(fi.FullName);
                                    p.StartInfo = psi;
                                    p.Start();
                                    p.WaitForExit(30000);
                                    File.Delete(fi.FullName);
                                    Thread.Sleep(500);
                                }
                                catch (Exception ex)
                                {
                                    Sxmobi.LogHelper.Error(this.GetType().ToString(), ex.Message, ex);
                                }

                            }
                        }
                        else
                        {
                            RutAllFiles(fsi[i].FullName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Sxmobi.LogHelper.Error(this.GetType().ToString(), ex.Message, ex);

                }

            }

    读取配置程序(SiteConfig.cs)

            private static string xmlPath = Sxmobi.FileHelper.GetMapPath("~") + "\\Config\\Site.config";
            static XmlDocument xmlDoc = Sxmobi.XmlHelper.GetXmlDoc(xmlPath);

            private static DateTime LastestXmlModefyTime = Utility.getFileModifyTime(xmlPath);

            public static bool ConfigIsUpdate
            {
                get
                {
                    try
                    {
                        if ((Utility.getFileModifyTime(xmlPath) != DateTime.MinValue) && (LastestXmlModefyTime != Utility.getFileModifyTime(xmlPath)))
                        {
                            LastestXmlModefyTime = Utility.getFileModifyTime(xmlPath);
                            xmlDoc = Sxmobi.XmlHelper.GetXmlDoc(xmlPath);//重新读取config
                            return true;
                        }
                        return false;
                    }
                    catch
                    {
                        return false;
                    }
                }

            }

            public static string BatFileRoot
            {
                get
                {
                    return Sxmobi.XmlHelper.GetNodeValue(xmlDoc, "/Site/BatFileRoot");
                }
            }

  • 相关阅读:
    【Azure 应用服务】由 Azure Functions runtime is unreachable 的错误消息推导出 ASYNC(异步)和 SYNC(同步)混用而引起ThreadPool耗尽问题
    【Azure API 管理】是否可以将Swagger 的API定义导入导Azure API Management中
    【Azure 应用服务】Azure Function 不能被触发
    【Azure 环境】Azure Key Vault (密钥保管库)中所保管的Keys, Secrets,Certificates是否可以实现数据粒度的权限控制呢?
    【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心
    【Azure 事件中心】azure-spring-cloud-stream-binder-eventhubs客户端组件问题, 实践消息非顺序可达
    【Azure API 管理】Azure API Management通过请求中的Path来限定其被访问的频率(如1秒一次)
    【Azure 环境】前端Web通过Azure AD获取Token时发生跨域问题(CORS Error)
    【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
    【Azure 应用服务】App Service中抓取 Web Job 的 DUMP 办法
  • 原文地址:https://www.cnblogs.com/dashi/p/4034753.html
Copyright © 2011-2022 走看看