示例#1
0
        public void Run(object o)
        {
            try
            {
                // Get DLL versions
                collectComponentVersions();
                Random random       = new Random();
                int    initialSleep = random.Next(CatConstants.HEARTBEAT_MIN_INITIAL_SLEEP_MILLISECONDS, CatConstants.HEARTBEAT_MAX_INITIAL_SLEEP_MILLISECONDS);
                Console.WriteLine("Heartbeat initial sleep: " + initialSleep + " ms");
                Thread.Sleep(initialSleep);
            }
            catch (Exception ex) { Cat.lastException = ex; return; }

            // In Java, try to avoid send heartbeat at 59-01 second, which is missing here in .NET

            // In Java, try to build class paths, which is a list of jar file names.
            try
            {
                ITransaction reboot = Cat.NewTransaction("System", "Reboot");
                reboot.Status = CatConstants.SUCCESS;
                Cat.LogEvent("Reboot", NetworkInterfaceManager.HostIP, CatConstants.SUCCESS);
                reboot.Complete();

                DateTime lastSendVersionsTimestamp = default(DateTime);
                while (true)
                {
                    ITransaction t = Cat.NewTransaction("System", "Status");
                    t.AddData(CatConstants.DUMP_LOCKED, false);
                    IHeartbeat h = Cat.NewHeartbeat("Heartbeat", NetworkInterfaceManager.HostIP);
                    try
                    {
                        var  now = DateTime.Now;
                        bool isLongIntevalHeartbeat = false;
                        if (default(DateTime) == lastSendVersionsTimestamp ||
                            now.Hour != lastSendVersionsTimestamp.Hour ||
                            (now - lastSendVersionsTimestamp >= TimeSpan.FromMinutes(LONG_INTERVAL_HEARBEAT_MINUTES)))
                        {
                            isLongIntevalHeartbeat    = true;
                            lastSendVersionsTimestamp = now;
                        }
                        h.AddData(BuildStatusData(isLongIntevalHeartbeat));
                        h.Status = CatConstants.SUCCESS;

                        if (isLongIntevalHeartbeat)
                        {
                            string configHeartBeatMessage = config.GetConfigHeartbeatMessage();
                            if (!String.IsNullOrWhiteSpace(configHeartBeatMessage))
                            {
                                Cat.LogEvent("Cat.Client.InconsistentAppId", configHeartBeatMessage);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Cat.lastException = ex;
                        h.SetStatus(ex);
                        Cat.LogError(ex);
                    }
                    finally
                    {
                        h.Complete();
                    }

                    if (!String.IsNullOrEmpty(fileVersion))
                    {
                        Cat.LogEvent("Cat.Client.Version", fileVersion);
                    }

                    t.Status = CatConstants.SUCCESS;
                    t.Complete();

                    // Append Cat.lastException if not null
                    if (null != Cat.lastException)
                    {
                        Exception ex = Cat.lastException;
                        Cat.lastException = null;
                        Cat.LogEvent("Cat.Client.LastException", ex.GetType().Name, CatConstants.SUCCESS, ex.ToString());
                    }

                    // Append Cat.lastMessage if not null
                    if (!String.IsNullOrWhiteSpace(Cat.lastMessage))
                    {
                        Cat.LogEvent("Cat.Client.LastMessage", "msg", CatConstants.SUCCESS, "message=" + Cat.lastMessage);
                        Cat.lastMessage = null;
                    }

                    // Sleep to the 30th second of the next minute, not to the 30th second of the current minute.
                    var sleepInSeconds = 90 - DateTime.Now.Second;
                    Thread.Sleep(sleepInSeconds * 1000);
                }
            }
            catch (Exception ex) { Cat.lastException = ex; }
        }