Esempio n. 1
0
 /// <summary>
 ///     系统关闭时调用
 /// </summary>
 internal static void OnZeroEnd()
 {
     RaiseEvent(ZeroNetEventType.AppStop);
     using (OnceScope.CreateScope(ZeroObjects))
     {
         ZeroTrace.SystemLog("[OnZeroEnd>>");
         SystemManager.Instance.HeartLeft();
         ApplicationState = StationState.Closing;
         if (HaseActiveObject)
         {
             Parallel.ForEach(ActiveObjects.ToArray(), obj =>
             {
                 try
                 {
                     ZeroTrace.SystemLog(obj.Name, "*Close");
                     obj.OnZeroEnd();
                 }
                 catch (Exception e)
                 {
                     ZeroTrace.WriteException(obj.Name, e, "*Close");
                 }
             });
             WaitAllObjectSemaphore();
         }
         GC.Collect();
         ApplicationState = StationState.Closed;
         ZeroTrace.SystemLog("<<OnZeroEnd]");
     }
 }
Esempio n. 2
0
 /// <summary>
 ///     系统启动时调用
 /// </summary>
 internal static void OnZeroStart()
 {
     Debug.Assert(!HaseActiveObject);
     using (OnceScope.CreateScope(ZeroObjects, ResetObjectActive))
     {
         ZeroTrace.SystemLog("[OnZeroStart>>");
         foreach (var obj in ZeroObjects.Values.ToArray())
         {
             try
             {
                 ZeroTrace.SystemLog(obj.Name, "*Start");
                 obj.OnZeroStart();
             }
             catch (Exception e)
             {
                 ZeroTrace.WriteException(obj.Name, e, "*Start");
             }
         }
         WaitAllObjectSemaphore();
     }
     SystemManager.Instance.HeartReady();
     ApplicationState = StationState.Run;
     RaiseEvent(ZeroNetEventType.AppRun);
     ZeroTrace.SystemLog("<<OnZeroStart]");
 }
Esempio n. 3
0
 /// <summary>
 ///     心跳
 /// </summary>
 internal static void OnHeartbeat()
 {
     if (!InRun)
     {
         return;
     }
     using (OnceScope.CreateScope(ZeroObjects))
     {
         if (!InRun)
         {
             return;
         }
         SystemManager.Instance.Heartbeat();
         foreach (var obj in ActiveObjects.ToArray())
         {
             try
             {
                 obj.OnHeartbeat();
             }
             catch (Exception e)
             {
                 ZeroTrace.WriteException(obj.Name, e, "OnHeartbeat");
             }
         }
         ;
     }
 }
Esempio n. 4
0
 /// <summary>
 ///     进入系统侦听
 /// </summary>
 internal static void Monitor()
 {
     using (OnceScope.CreateScope(ZeroApplication.Config))
     {
     }
     using (var poll = ZmqPool.CreateZmqPool())
     {
         poll.Prepare(new[] { ZSocket.CreateSubscriberSocket(ZeroApplication.Config.ZeroMonitorAddress) }, ZPollEvent.In);
         ZeroTrace.SystemLog("Zero center in monitor...");
         TaskEndSem.Release();
         while (ZeroApplication.IsAlive)
         {
             if (!poll.Poll() || !poll.CheckIn(0, out var message))
             {
                 continue;
             }
             if (!message.Unpack(out var item))
             {
                 continue;
             }
             OnMessagePush(item.ZeroEvent, item.SubTitle, item.Content);
         }
     }
     TaskEndSem.Release();
 }
Esempio n. 5
0
        /// <summary>
        ///     注销时调用
        /// </summary>
        internal static void OnZeroDestory()
        {
            if (!Monitor.TryEnter(ZeroObjects))
            {
                return;
            }
            ZeroTrace.SystemLog("[OnZeroDestory>>");
            RaiseEvent(ZeroNetEventType.AppEnd);
            using (OnceScope.CreateScope(ZeroObjects))
            {
                var array = ZeroObjects.Values.ToArray();
                ZeroObjects.Clear();
                Parallel.ForEach(array, obj =>
                {
                    try
                    {
                        ZeroTrace.SystemLog(obj.Name, "*Destory");
                        obj.OnZeroDestory();
                    }
                    catch (Exception e)
                    {
                        ZeroTrace.WriteException(obj.Name, e, "*Destory");
                    }
                });

                GC.Collect();
                ZeroTrace.SystemLog("<<OnZeroDestory]");

                ZeroTrace.SystemLog("[OnZeroDispose>>");
                Parallel.ForEach(array, obj =>
                {
                    try
                    {
                        ZeroTrace.SystemLog(obj.Name, "*Dispose");
                        obj.Dispose();
                    }
                    catch (Exception e)
                    {
                        ZeroTrace.WriteException(obj.Name, e, "*Dispose");
                    }
                });
                ZeroTrace.SystemLog("<<OnZeroDispose]");
            }
        }
Esempio n. 6
0
        /// <summary>
        ///     注册对象
        /// </summary>
        public static bool RegistZeroObject(IZeroObject obj)
        {
            if (obj.GetType().IsSubclassOf(typeof(ApiStation)))
            {
                ZeroDiscover.DiscoverApiDocument(obj.GetType());
            }
            using (OnceScope.CreateScope(ZeroObjects))
            {
                if (ZeroObjects.ContainsKey(obj.Name))
                {
                    return(false);
                }
                ZeroTrace.SystemLog("RegistZeroObject", obj.Name);
                ZeroObjects.Add(obj.Name, obj);
                if (ApplicationState >= StationState.Initialized)
                {
                    try
                    {
                        obj.OnZeroInitialize();
                        ZeroTrace.SystemLog(obj.Name, "Initialize");
                    }
                    catch (Exception e)
                    {
                        ZeroTrace.WriteException(obj.Name, e, "Initialize");
                    }
                }

                if (!CanDo)
                {
                    return(true);
                }
                try
                {
                    ZeroTrace.SystemLog(obj.Name, "Start");
                    obj.OnZeroStart();
                }
                catch (Exception e)
                {
                    ZeroTrace.WriteException(obj.Name, e, "Start");
                }
            }
            return(true);
        }
Esempio n. 7
0
 /// <summary>
 ///     系统启动时调用
 /// </summary>
 internal static void OnStationStateChanged(StationConfig config)
 {
     using (OnceScope.CreateScope(ZeroObjects))
     {
         ZeroTrace.WriteLine("[OnStationStateChanged>>");
         Parallel.ForEach(ActiveObjects.ToArray(), obj =>
         {
             try
             {
                 obj.OnStationStateChanged(config);
             }
             catch (Exception e)
             {
                 ZeroTrace.WriteException(obj.Name, e, "OnStationStateChanged");
             }
         });
         ZeroTrace.WriteLine("<<OnStationStateChanged]");
     }
 }
Esempio n. 8
0
 /// <summary>
 ///     系统启动时调用
 /// </summary>
 internal static void OnStationStateChanged(StationConfig config)
 {
     using (OnceScope.CreateScope(ZeroObjects))
     {
         ZeroTrace.SystemLog($"[OnStationStateChanged({config.StationName})>>");
         Parallel.ForEach(ActiveObjects.Where(p => string.Equals(config.StationName, p.StationName, StringComparison.OrdinalIgnoreCase)).ToArray(),
                          obj =>
         {
             try
             {
                 obj.OnStationStateChanged(config);
             }
             catch (Exception e)
             {
                 ZeroTrace.WriteException(obj.Name, e, "OnStationStateChanged");
             }
         });
         ZeroTrace.SystemLog($"<<OnStationStateChanged({config.StationName})]");
     }
 }
Esempio n. 9
0
 /// <summary>
 ///     系统启动时调用
 /// </summary>
 internal static void OnZeroInitialize()
 {
     using (OnceScope.CreateScope(ZeroObjects))
     {
         ZeroTrace.SystemLog("[OnZeroInitialize>>");
         Parallel.ForEach(ZeroObjects.Values.ToArray(), obj =>
         {
             try
             {
                 obj.OnZeroInitialize();
                 ZeroTrace.SystemLog(obj.Name, "Initialize");
             }
             catch (Exception e)
             {
                 ZeroTrace.WriteException(obj.Name, e, "*Initialize");
             }
         });
         ZeroTrace.SystemLog("<<OnZeroInitialize]");
     }
 }