class ActiveRecord::Bogacs::Reaper

Every frequency seconds, the reaper will “reap” a pool it belongs to. Reaping means detecting stale cached connections in the pool - those that were checked-out by a thread but never checked back in after the thread finished/died.

Configure the frequency by setting ‘:reaping_frequency` in your database.yml.

@note This version is fail safe - raised errors won’t stop the reaping process.

Attributes

frequency[R]
pool[R]
retry_error[RW]

Public Class Methods

new(pool, frequency) click to toggle source

‘Reaper.new(pool, spec.config).run` @private

# File lib/active_record/bogacs/reaper.rb, line 21
def initialize(pool, frequency)
  @pool = pool;
  if frequency
    frequency = frequency.to_f
    @frequency = frequency > 0.0 ? frequency : false
  else
    @frequency = nil
  end
  @retry_error = 1.5; @running = nil
end

Public Instance Methods

run() click to toggle source
# File lib/active_record/bogacs/reaper.rb, line 32
def run
  return unless frequency
  @running = true; start
end
running?() click to toggle source
# File lib/active_record/bogacs/reaper.rb, line 41
def running?; @running end
start(delay = nil) click to toggle source
# File lib/active_record/bogacs/reaper.rb, line 37
def start(delay = nil)
  Thread.new { exec(delay) }
end

Private Instance Methods

exec(delay = nil) click to toggle source
# File lib/active_record/bogacs/reaper.rb, line 45
def exec(delay = nil)
  sleep delay if delay
  while true
    begin
      sleep frequency
      pool.reap
    rescue => e
      log = logger
      if retry_delay = @retry_error
        log && log.warn("[reaper] reaping failed: #{e.inspect} restarting after #{retry_delay}s")
        start retry_delay
      else
        log && log.warn("[reaper] reaping failed: #{e.inspect} stopping reaper")
        @running = false
      end
      break
    end
  end
end
logger() click to toggle source
# File lib/active_record/bogacs/reaper.rb, line 77
def logger
  @logger ||= ( pool.respond_to?(:logger) ? pool.logger : nil ) rescue nil
end
set_thread_name(name) click to toggle source
# File lib/active_record/bogacs/reaper.rb, line 65
def set_thread_name(name)
  if ( thread = Thread.current ).respond_to?(:name)
    thread.name = name; return
  end
  if defined? JRUBY_VERSION
    thread = JRuby.reference(thread).getNativeThread
    thread.setName("#{name} #{thread.getName}")
  else
    thread[:name] = name
  end
end