private void ReportServiceDown(IServiceAddress address, ServiceType type, ServiceStatus status) { // Default old status, ServiceStatus oldStatus = ServiceStatus.Up; // Search and return, lock (monitoredServers) { TrackedService tracked = null; foreach (TrackedService s in monitoredServers) { if (s.ServiceAddress.Equals(address) && s.ServiceType == type) { tracked = s; break; } } if (tracked == null) { // Not found so add it to the tracker, monitoredServers.Add(new TrackedService(address, type, status)); } else { oldStatus = tracked.CurrentStatus; tracked.CurrentStatus = status; } } // Fire the event if the status changed, if (!oldStatus.Equals(status)) { if (StatusChange != null) { StatusChange(this, new ServiceStatusEventArgs(address, type, oldStatus, status)); } } }
public override bool Equals(object obj) { TrackedService other = obj as TrackedService; if (other == null) { return(false); } if (serviceAddress != other.serviceAddress && (serviceAddress == null || !serviceAddress.Equals(other.serviceAddress))) { return(false); } if (!serviceType.Equals(other.serviceType)) { return(false); } return(true); }
private void PollServer(TrackedService server) { bool pollOk = true; string commandArg = null; if (server.ServiceType == ServiceType.Block) commandArg = "heartbeatB"; else if (server.ServiceType == ServiceType.Root) commandArg = "heartbeatR"; else if (server.ServiceType == ServiceType.Manager) commandArg = "heartbeatM"; else { tracker.log.Error(String.Format("Don't know how to poll type {0}", server.ServiceType)); pollOk = false; } // Send the poll command to the server, IMessageProcessor p = connector.Connect(server.ServiceAddress, ServiceType.Block); MessageStream outputStream = new MessageStream(); outputStream.AddMessage(new Message("poll", commandArg)); IEnumerable<Message> inputStream = p.Process(outputStream); foreach (Message m in inputStream) { // Any error with the poll means no status change, if (m.HasError) { pollOk = false; } } // If the poll is ok, set the status of the server to UP and remove from // the monitor list, if (pollOk) { // The server status is set to 'STATUS_UP' if either the current state // is 'DOWN CLIENT REPORT' or 'DOWN HEARTBEAT' // Synchronize over 'servers_map' for safe alteration of the ref. ServiceStatus oldStatus; lock (monitoredServers) { oldStatus = server.CurrentStatus; if (oldStatus == ServiceStatus.DownClientReport || oldStatus == ServiceStatus.DownHeartbeat) { server.CurrentStatus = ServiceStatus.Up; } // Remove the server from the monitored_servers list. monitoredServers.Remove(server); } if (tracker.log.IsInterestedIn(LogLevel.Information)) { tracker.log.Info(String.Format("Poll ok. Status now UP for {0} {1}", server.ServiceAddress, server.ServiceType)); } // Fire the event if the status changed, try { if (tracker.StatusChange != null) tracker.StatusChange(this, new ServiceStatusEventArgs(server.ServiceAddress, server.ServiceType, oldStatus, ServiceStatus.Up)); } catch (Exception e) { // Catch any exception generated. Log it but don't terminate the // thread. tracker.log.Error("Exception in listener during poll", e); } } else { // Make sure the server status is set to 'DOWN HEARTBEAT' if the poll // failed, // Synchronize over 'servers_map' for safe alteration of the ref. lock (monitoredServers) { ServiceStatus sts = server.CurrentStatus; if (sts == ServiceStatus.Up || sts == ServiceStatus.DownClientReport) { server.CurrentStatus = ServiceStatus.DownHeartbeat; } } } }
private void PollServer(TrackedService server) { bool pollOk = true; string commandArg = null; if (server.ServiceType == ServiceType.Block) { commandArg = "heartbeatB"; } else if (server.ServiceType == ServiceType.Root) { commandArg = "heartbeatR"; } else if (server.ServiceType == ServiceType.Manager) { commandArg = "heartbeatM"; } else { tracker.log.Error(String.Format("Don't know how to poll type {0}", server.ServiceType)); pollOk = false; } // Send the poll command to the server, IMessageProcessor p = connector.Connect(server.ServiceAddress, ServiceType.Block); MessageStream outputStream = new MessageStream(); outputStream.AddMessage(new Message("poll", commandArg)); IEnumerable <Message> inputStream = p.Process(outputStream); foreach (Message m in inputStream) { // Any error with the poll means no status change, if (m.HasError) { pollOk = false; } } // If the poll is ok, set the status of the server to UP and remove from // the monitor list, if (pollOk) { // The server status is set to 'STATUS_UP' if either the current state // is 'DOWN CLIENT REPORT' or 'DOWN HEARTBEAT' // Synchronize over 'servers_map' for safe alteration of the ref. ServiceStatus oldStatus; lock (monitoredServers) { oldStatus = server.CurrentStatus; if (oldStatus == ServiceStatus.DownClientReport || oldStatus == ServiceStatus.DownHeartbeat) { server.CurrentStatus = ServiceStatus.Up; } // Remove the server from the monitored_servers list. monitoredServers.Remove(server); } if (tracker.log.IsInterestedIn(LogLevel.Information)) { tracker.log.Info(String.Format("Poll ok. Status now UP for {0} {1}", server.ServiceAddress, server.ServiceType)); } // Fire the event if the status changed, try { if (tracker.StatusChange != null) { tracker.StatusChange(this, new ServiceStatusEventArgs(server.ServiceAddress, server.ServiceType, oldStatus, ServiceStatus.Up)); } } catch (Exception e) { // Catch any exception generated. Log it but don't terminate the // thread. tracker.log.Error("Exception in listener during poll", e); } } else { // Make sure the server status is set to 'DOWN HEARTBEAT' if the poll // failed, // Synchronize over 'servers_map' for safe alteration of the ref. lock (monitoredServers) { ServiceStatus sts = server.CurrentStatus; if (sts == ServiceStatus.Up || sts == ServiceStatus.DownClientReport) { server.CurrentStatus = ServiceStatus.DownHeartbeat; } } } }