lib/queue_bus/heartbeat.rb in queue-bus-0.9.0 vs lib/queue_bus/heartbeat.rb in queue-bus-0.9.1

- old
+ new

@@ -1,13 +1,15 @@ +# frozen_string_literal: true + module QueueBus - # publishes event about the current time + # When run, will calculate all of the heartbeats that need to be sent and then broadcasts + # those events out for execution. By always backfilling it ensures that no heartbeat is + # ever missed. class Heartbeat - class << self - def lock_key - "bus:heartbeat:lock" + 'bus:heartbeat:lock' end def lock_seconds 60 end @@ -36,73 +38,73 @@ def unlock! ::QueueBus.redis { |redis| redis.del(lock_key) } end - def redis_key - "bus:heartbeat:timestamp" + 'bus:heartbeat:timestamp' end def environment_name - ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["BUS_ENV"] + ENV['RAILS_ENV'] || ENV['RACK_ENV'] || ENV['BUS_ENV'] end def get_saved_minute! key = ::QueueBus.redis { |redis| redis.get(redis_key) } return nil if key.nil? + case environment_name when 'development', 'test' # only 3 minutes in development; otherwise, TONS of events if not run in a while - three_ago = Time.now.to_i - 3*60*60 + three_ago = Time.now.to_i / 60 - 3 key = three_ago if key.to_i < three_ago end - return key.to_i + key.to_i end def set_saved_minute!(epoch_minute) ::QueueBus.redis { |redis| redis.set(redis_key, epoch_minute) } end - def perform(*args) + def perform(*_args) real_now = Time.now.to_i run_until = lock! - 2 return if run_until < real_now - while((real_now = Time.now.to_i) < run_until) + while (real_now = Time.now.to_i) < run_until minutes = real_now.to_i / 60 last = get_saved_minute! if last break if minutes <= last + minutes = last + 1 end - seconds = minutes * (60) - hours = minutes / (60) - days = minutes / (60*24) + seconds = minutes * 60 + hours = minutes / 60 + days = minutes / (60 * 24) - now = Time.at(seconds) + now = Time.at(seconds) attributes = {} - attributes["epoch_seconds"] = seconds - attributes["epoch_minutes"] = minutes - attributes["epoch_hours"] = hours - attributes["epoch_days"] = days + attributes['epoch_seconds'] = seconds + attributes['epoch_minutes'] = minutes + attributes['epoch_hours'] = hours + attributes['epoch_days'] = days - attributes["minute"] = now.min - attributes["hour"] = now.hour - attributes["day"] = now.day - attributes["month"] = now.month - attributes["year"] = now.year - attributes["yday"] = now.yday - attributes["wday"] = now.wday + attributes['minute'] = now.min + attributes['hour'] = now.hour + attributes['day'] = now.day + attributes['month'] = now.month + attributes['year'] = now.year + attributes['yday'] = now.yday + attributes['wday'] = now.wday - ::QueueBus.publish("heartbeat_minutes", attributes) + ::QueueBus.publish('heartbeat_minutes', attributes) set_saved_minute!(minutes) end unlock! end end - end end