/// <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; } } }
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 = @" §ion 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; } } }