private void OnHeartbet(object state) { CancellationToken cancellationToken = (CancellationToken)state; while (cancellationToken.IsCancellationRequested == false) { logger.WriteTrace("Sending heartbeat for server id = {0}...", ServerId); ServerHeartbeatModel serverHeartbeatModel = new ServerHeartbeatModel(ServerId, heartbeatLifetime); serverDataContext.SendHeartbeat(serverHeartbeatModel); logger.WriteTrace("The heartbeat has been sent."); //Find servers that have been expired and delete them. logger.WriteTrace("Finding and deleting servers that have been expired..."); int serverCount = serverDataContext.DeleteExpiredServers(); if (serverCount > 0) { logger.WriteTrace("There were(was) {0} servers that have(has) been expired. They were(was) deleted.", serverCount); } else { logger.WriteTrace("There was no any expired servers."); } cancellationToken.WaitHandle.WaitOne(sleepTime); } }
/// <summary> /// Send a heartbeat from the server. /// </summary> /// <param name="serverHeartbeatModel">The heartbeat information.</param> public void SendHeartbeat(ServerHeartbeatModel serverHeartbeatModel) { logger.WriteTrace("Sending heartbeat from the server ({0}) to the database...", serverHeartbeatModel); if (serverHeartbeatModel.ServerId <= 0) { throw new ArgumentException("Server's identifier should be greate than 0. Now it is " + serverHeartbeatModel.ServerId, "serverHeartbeatModel"); } string sqlCommand = string.Format(heartbeatStatement, sqlSettings.WiseQueueDefaultSchema, serverTableName, serverHeartbeatModel.HeartbeatLifetime, serverHeartbeatModel.ServerId); logger.WriteTrace("The SqlCommand has been generated. Result: {0}", sqlCommand); logger.WriteTrace("Executing sql command..."); using (IDbConnection connection = connectionFactory.CreateConnection()) { using (IDbCommand command = connectionFactory.CreateCommand(connection)) { command.CommandText = sqlCommand; int affectedRows = command.ExecuteNonQuery(); logger.WriteTrace("The command has been executed. The entity has been deleted. Affected rows = {0}.", affectedRows); } } }