lib/reptile/heartbeat.rb in reptile-0.0.1 vs lib/reptile/heartbeat.rb in reptile-0.0.4

- old
+ new

@@ -26,62 +26,56 @@ # The default connection settings which override per-database settings. def self.user @user ||= {} end - - # Merge the connection settings in the configs parameter with HeartBeat defaults. + def self.connect(configs) - ReplicationMonitor.connect(configs.merge(user)) + Databases.connect(configs.merge(user).merge("database" => 'replication_monitor')) end # Write a heartbeat. # Thump thump. def self.write(name, configs) - connect(configs) + self.connect(configs) heartbeat = Heartbeat.create(:unix_time => Time.now.to_i, :db_time => "NOW()") - log "Wrote heartbeat to #{name} at #{Time.at(heartbeat.unix_time)}" + get_logger.info "Wrote heartbeat to #{name} at #{Time.at(heartbeat.unix_time)}" end # Read the most recent heartbeat and return the delay in seconds, or nil if no heartbeat are found. def self.read(name, configs) - connect(configs) + self.connect(configs) current_time = Time.now delay = nil heartbeat = Heartbeat.find(:first, :order => 'db_time DESC') # No heartbeats at all! if heartbeat.nil? - log "No heartbeats found on #{name} at #{Time.now}" + get_logger.info "No heartbeats found on #{name} at #{Time.now}" return nil; end # Not sure why we have both, (one is easier to read?). # Use one or the other to calculate delay... delay = (Time.now - Time.at(heartbeat.unix_time)).round #delay = (Time.now - heartbeat.db_time) - log "Read heartbeat from #{name} at #{Time.at(heartbeat.unix_time)}. The delay is #{strfdelay(delay)}" + get_logger.info "Read heartbeat from #{name} at #{Time.at(heartbeat.unix_time)}. The delay is #{strfdelay(delay)}" delay end -private - - # Open the 'heartbeat.log' file. - def self.open_log - logFile = 'heartbeat.log' - @logFileObj = File.open(logFile, "a") + def self.logger=(new_logger) + @logger = new_logger end - - # Log a message, both to the file and standard out. - def self.log(msg) - open_log if @logFileObj.nil? - puts msg - @logFileObj.puts msg + + def self.get_logger + @logger ||= Logger.new(STDOUT) end + +private # Format the delay (in seconds) as a human-readable string. def self.strfdelay(delay) seconds = delay % 60 minutes = delay / 60 % 60 \ No newline at end of file