class RainCatcher::Subscriber

Public Class Methods

new(options) click to toggle source
# File lib/rain_catcher/subscriber.rb, line 5
def initialize(options)
  @interval = options.fetch(:interval)
  @log_level = options.fetch(:log_level).to_s.downcase.to_sym
  @application_name = options.fetch(:application_name)
  @environment = options.fetch(:environment)
  @last_logged = nil
end

Public Instance Methods

log_if_interval_elapsed() click to toggle source
# File lib/rain_catcher/subscriber.rb, line 13
def log_if_interval_elapsed
  return unless elapsed?

  data = queue_data
  return if data.nil?

  Rails.logger.send(@log_level, data.to_json)
  @last_logged = Time.now.utc
end

Private Instance Methods

elapsed?() click to toggle source
# File lib/rain_catcher/subscriber.rb, line 39
def elapsed?
  return true if @last_logged.nil?
  return true if (Time.now.utc - @last_logged).seconds >= @interval

  false
end
queue_data() click to toggle source
# File lib/rain_catcher/subscriber.rb, line 25
def queue_data
  return nil unless defined?(Unicorn)
  return nil unless Raindrops::Linux.respond_to?(:tcp_listener_stats)

  listener = Unicorn.listener_names.first
  return nil if listener.nil?

  Raindrops::Linux.tcp_listener_stats[listener]&.to_h&.merge(
    source: 'rain_catcher',
    application: @application_name,
    environment: @environment
  )
end