Example #1
0
        public void HeartMonitorTest()
        {
            ParametersHandler.LoadParametersConfig();

            HeartServerDirMonitor.Single.RefreshDir();

            Assert.AreEqual(1, HeartServerDirMonitor.Single.Count);

            HeartServerInfo hsi = HeartServerDirMonitor.Single["Heart_Monitor_Test"];

            hsi.Load();
            hsi.Run();

            System.Threading.Thread.Sleep(1000 * 10);

            //hsi.Pause();

            //System.Threading.Thread.Sleep(1000 * 10);

            Assert.AreEqual(HeartModel.StateMachine.HeartServerState.Exception, hsi.State);
            //Assert.AreEqual(HeartModel.StateMachine.HeartServerState.Exception, hsi.State);

            hsi.Unload();

            Assert.AreEqual(HeartModel.StateMachine.HeartServerState.NotLoaded, hsi.State);
        }
Example #2
0
        public void HeartMonitorTest2()
        {
            ParametersHandler.LoadParametersConfig();

            HeartServerDirMonitor.Single.RefreshDir();

            Assert.AreEqual(1, HeartServerDirMonitor.Single.Count);

            HeartServerInfo hsi = HeartServerDirMonitor.Single["Heart_Monitor_Test"];

            TimeSpan span = DateTime.Now.TimeOfDay;

            hsi.SpanInfo = new TimeConfig()
            {
                StartTime = span,
                EndTime   = span,
                Span      = new TimeSpan(0, 0, 30)
            };

            hsi.Load();
            hsi.Run();
            System.Threading.Thread.Sleep(1);
            Assert.AreEqual(HeartModel.StateMachine.HeartServerState.Running, hsi.State);
            hsi.Pause();
            Assert.AreEqual(HeartModel.StateMachine.HeartServerState.Running, hsi.State);

            System.Threading.Thread.Sleep(1000 * 12);
            Assert.AreEqual(HeartModel.StateMachine.HeartServerState.Loaded, hsi.State);
        }
Example #3
0
        public void IsDo_Test()
        {
            DateTime now = DateTime.Now;

            // 当前时间在区间段内
            HeartServerInfo hsi = new HeartServerInfo()
            {
                SpanInfo = new TimeConfig()
                {
                    StartTime = now.AddHours(-1).TimeOfDay,
                    EndTime   = now.AddHours(1).TimeOfDay
                }
            };
            RunningHeart r = new RunningHeart(hsi);

            Assert.AreEqual(true, r.IsDo());

            //时间相同
            hsi.SpanInfo.StartTime = now.AddHours(1).TimeOfDay;
            hsi.SpanInfo.EndTime   = now.AddHours(1).TimeOfDay;
            Assert.AreEqual(true, r.IsDo());

            // 当前时间在开始区间以外
            hsi.SpanInfo.StartTime = now.AddHours(1).TimeOfDay;
            hsi.SpanInfo.EndTime   = now.AddHours(2).TimeOfDay;
            Assert.AreEqual(false, r.IsDo());

            // 当前时间在结束时间以外
            hsi.SpanInfo.StartTime = now.AddHours(-1).TimeOfDay;
            hsi.SpanInfo.EndTime   = now.AddHours(-2).TimeOfDay;
            Assert.AreEqual(false, r.IsDo());
        }
Example #4
0
        /// <summary>
        /// 创建心跳AppDomain
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        private bool FilterHeartAssembly(string filePath, string dirPath)
        {
            bool   b        = false;
            string fileName = filePath.Substring(dirPath.Length + 1);

            if (ruleMap[RuleEnum.CheckFileName](null, fileName)) // 检验.dll文件名规则是否以 Heart_Monitor_ 开头
            {
                fileName = fileName.Split('.')[0];
                b        = effectiveHeartServerMap.ContainsKey(fileName);

                if (!b)
                {
                    AppDomain heartDomain = AppDomainHelper.CreateHeartServerAppDomain(dirPath, fileName);  //filePath,

                    if (heartDomain != null)
                    {
                        try
                        {
                            if (!ruleMap[RuleEnum.CheckAssemblyClassName](heartDomain, fileName))
                            {
                                DeleteHeartDir("加载的程序集没有继承于HeartBase的HeartServer对象", dirPath);
                            }
                            else
                            {
                                HeartServerInfo info = new HeartServerInfo();
                                info.AssemblyFilePath    = filePath;
                                info.DirPath             = dirPath;
                                info.Name                = fileName;
                                info.Description         = tempHear.GetDescription();
                                info.SpanInfo            = TimeConfigCollection.Single[fileName];
                                info.OnTimeConfigChange += info_OnTimeConfigChange;

                                effectiveHeartServerMap.TryAdd(fileName, info);
                            }

                            // 验证完后  不管是否与规则相符 都执行卸载AppDomain操作
                            AppDomain.Unload(heartDomain);
                            heartDomain = null;

                            b = true;
                        }
                        catch (FileLoadException flex)
                        {
                            LogServer.WriteException("Load dll", flex, filePath);
                            AppDomain.Unload(heartDomain);
                            heartDomain = null;

                            DeleteHeartDir("加载的程序集没有继承于HeartBase的HeartServer对象", dirPath);
                        }
                    }
                }
            }

            return(b);
        }
Example #5
0
        void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
        {
            HeartServerInfo hsInfo = this[e.Exception.Source];

            if (hsInfo != null)
            {
                hsInfo.ReceiveException(e.Exception);
            }
            else
            {
                LogServer.WriteException("Default AppDomain  First", e.Exception);

                Exception subException = e.Exception.InnerException;
                while (subException != null)
                {
                    LogServer.WriteException("InnerException First", subException);
                    subException = subException.InnerException;
                }
            }
        }
Example #6
0
        internal static OnceServer CreateOnceServerByKey(string key)
        {
            OnceServer os = null;

            HeartServerInfo hsi = HeartServerDirMonitor.Single[key];

            if (hsi != null)
            {
                DirectoryHelper.DirectoryCopy(hsi.DirPath, OnceDirPath + hsi.Name);
                os = new OnceServer();

                os.Domain = AppDomainHelper.CreateHeartServerAppDomain(OnceDirPath + hsi.Name, hsi.Name);

                if (os.Domain != null)
                {
                    os.HeartServer = AppDomainHelper.CreateHeart(os.Domain, hsi.Name, hsi.Name + "." + AppDomainHelper.HeartClassName);
                }
            }

            return(os);
        }
Example #7
0
        public void SynchronizeTimeConfigTest()
        {
            if (File.Exists(TimeConfigCollection.Single.configFilePath))
            {
                File.Delete(TimeConfigCollection.Single.configFilePath);
            }

            Assert.AreEqual(false, File.Exists(TimeConfigCollection.Single.configFilePath));

            TimeConfigCollection.Single.Clear();
            Assert.AreEqual(0, TimeConfigCollection.Single.Count);

            HeartServerDirMonitor.Single.RefreshDir();

            HeartServerInfo hsi = null;

            foreach (KeyValuePair <string, HeartServerInfo> item in HeartServerDirMonitor.Single)
            {
                hsi = item.Value;
                break;
            }

            DateTime   now = DateTime.Now;
            TimeConfig tc  = new TimeConfig()
            {
                StartTime = now.AddHours(-1).TimeOfDay,
                EndTime   = now.AddHours(1).TimeOfDay
            };

            if (hsi != null)
            {
                hsi.SpanInfo = tc;
            }

            Assert.AreEqual(true, File.Exists(TimeConfigCollection.Single.configFilePath));

            Assert.AreEqual(1, TimeConfigCollection.Single.Count);

            Assert.AreEqual(tc, TimeConfigCollection.Single[hsi.Name]);
        }
Example #8
0
        public void RefreshTest()
        {
            ParametersHandler.LoadParametersConfig();

            HeartServerDirMonitor.Single.RefreshDir();
            HeartServerInfo hsi = HeartServerDirMonitor.Single["Heart_Monitor_est"];

            hsi.Load();
            hsi.Run();

            System.Threading.Thread.Sleep(1000 * 10);
            ParametersHandler.LoadParametersConfig();
            for (int i = 0; i < 10; ++i)
            {
                HeartServerDirMonitor.Single.RefreshDir();
                System.Threading.Thread.Sleep(1000);
            }
            hsi = HeartServerDirMonitor.Single["Heart_Monitor_Test"];

            Assert.AreEqual(HeartModel.StateMachine.HeartServerState.Running, hsi.State);

            LogHelper.LogServer.WriteException("Test", new Exception("Over test"));
        }
Example #9
0
        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;

            if (ex != null)
            {
                HeartServerInfo hsInfo = this[ex.Source];
                if (hsInfo != null)
                {
                    hsInfo.ReceiveException(ex);
                }
                else
                {
                    LogServer.WriteException("Default AppDomain", ex);

                    Exception subException = ex.InnerException;
                    while (subException != null)
                    {
                        LogServer.WriteException("InnerException", subException);
                        subException = subException.InnerException;
                    }
                }
            }
        }