Example #1
0
        /// <summary>
        /// Stops the service immediately, terminating any user activity.
        /// </summary>
        public void Stop()
        {
            lock (syncLock)
            {
                if (state == ServiceState.Stopped)
                {
                    return;
                }

                SysLog.LogInformation("Heartbeat Service Stop");
                SysLog.Flush();

                base.Close();
                state = ServiceState.Stopped;
            }

            if (handler != null)
            {
                handler.Stop();
                handler = null;
            }

            if (router != null)
            {
                router.Stop();
                router = null;
            }

            Program.PerfCounters.Zero();
        }
Example #2
0
        private ServiceState state;                 // Current service state

        /// <summary>
        /// Constructor.
        /// </summary>
        public AppService()
        {
            this.router  = null;
            this.handler = null;
            this.state   = ServiceState.Stopped;

            // Prepare the service to accept remote ServiceControl commands.

            base.Open();
        }
Example #3
0
        /// <summary>
        /// Handles the performance counter installation if they don't
        /// already exist.
        /// </summary>
        /// <returns>The application's <see cref="PerfCounterSet" />.</returns>
        public static PerfCounterSet InstallPerfCounters()
        {
            bool           exists = PerformanceCounterCategory.Exists(Const.HeartbeatPerf);
            PerfCounterSet perfCounters;

            perfCounters = new PerfCounterSet(false, true, Const.HeartbeatPerf, Const.HeartbeatName);

            if (!exists)
            {
                HeartbeatHandler.InstallPerfCounters(perfCounters, null);
                perfCounters.Install();
            }

            return(perfCounters);
        }
Example #4
0
        /// <summary>
        /// Starts the service, associating it with an <see cref="IServiceHost" /> instance.
        /// </summary>
        /// <param name="serviceHost">The service user interface.</param>
        /// <param name="args">Command line arguments.</param>
        public void Start(IServiceHost serviceHost, string[] args)
        {
            lock (syncLock)
            {
                if (router != null)
                {
                    return;     // Already started
                }
                // Global initialization

                NetTrace.Start();
                Program.Config = new Config("LillTek.Datacenter.HeartbeatService");
                Program.InstallPerfCounters();

                // Service initialization

                this.serviceHost = serviceHost;
                SysLog.LogInformation("Heartbeat Service v{0} Start", Helper.GetVersionString());

                try
                {
                    router = new LeafRouter();
                    router.Start();

                    handler = new HeartbeatHandler();
                    handler.Start(router, null, Program.PerfCounters, null);

                    state = ServiceState.Running;
                }
                catch (Exception e)
                {
                    if (handler != null)
                    {
                        handler.Stop();
                        handler = null;
                    }

                    if (router != null)
                    {
                        router.Stop();
                        router = null;
                    }

                    SysLog.LogException(e);
                    throw;
                }
            }
        }
Example #5
0
        public void HeartbeatHandler_SingleService()
        {
            // Configure a heartbeart service to monitor a single URI and verify
            // that it can detect health and failure.

            HeartbeatHandler handler    = null;
            HttpServer       httpServer = null;
            const string     cfg        = @"

&section LillTek.Datacenter.HeartbeatService

    NetworkBinding = ANY:HTTP-HEARTBEAT
    Service[-]     = http://testservice01.lilltek.com:666/Heartbeat.aspx?global=0
    PollInterval   = 1s
    BkInterval     = 1s

&endsection
";

            try
            {
                Config.SetConfig(cfg.Replace('&', '#'));

                // Initialize the health status dictionary and crank up a HTTP listerner
                // to simulate target services.

                host2Health = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);
                host2Health["testservice01.lilltek.com"] = true;

                httpServer = new HttpServer(new IPEndPoint[] { new IPEndPoint(IPAddress.Any, 666) }, new IHttpModule[] { this }, 10, 10, 8196);
                httpServer.Start();

                // Start the heartbeat handler

                handler = new HeartbeatHandler();
                handler.Start(null, null, null, null);

                // Give the service a chance to ping the test a few times and then
                // make a request to the heartbeat handler to verify that it indicates
                // that the server is healthy.

                Thread.Sleep(3000);
                Assert.IsTrue(GetHealth());

                // Now indicate that the target service is unhealthy, give the heartbeat
                // handler a chance to ping the service, and verify that it determined
                // the server is unhealthy.

                host2Health["testservice01.lilltek.com"] = false;
                Thread.Sleep(3000);
                Assert.IsFalse(GetHealth());

                // Now indicate that the target service is healthy, give the heartbeat
                // handler a chance to ping the service, and verify that it determined
                // the server is healthy.

                host2Health["testservice01.lilltek.com"] = true;
                Thread.Sleep(3000);
                Assert.IsTrue(GetHealth());

                // Stop the simulated target HTTP server and verify that the handler
                // detects the failure.

                httpServer.Stop();
                httpServer = null;
                Thread.Sleep(3000);
                Assert.IsFalse(GetHealth());
            }
            finally
            {
                Config.SetConfig(null);

                if (handler != null)
                {
                    handler.Stop();
                }

                if (httpServer != null)
                {
                    httpServer.Stop();
                    httpServer = null;
                }
            }
        }