Class: ActiveRecord::Bogacs::Reaper

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/bogacs/reaper.rb

Overview

Note:

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

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.

Instead the thread will be restarted and reaping continues at the next tick.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, frequency) ⇒ Reaper

Reaper.new(self, spec.config).run



23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/bogacs/reaper.rb', line 23

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

Instance Attribute Details

#frequencyObject (readonly)

Returns the value of attribute frequency.



18
19
20
# File 'lib/active_record/bogacs/reaper.rb', line 18

def frequency
  @frequency
end

#poolObject (readonly)

Returns the value of attribute pool.



18
19
20
# File 'lib/active_record/bogacs/reaper.rb', line 18

def pool
  @pool
end

#retry_errorObject

Returns the value of attribute retry_error.



19
20
21
# File 'lib/active_record/bogacs/reaper.rb', line 19

def retry_error
  @retry_error
end

Instance Method Details

#runObject



34
35
36
37
# File 'lib/active_record/bogacs/reaper.rb', line 34

def run
  return unless frequency
  @running = true; start
end

#running?Boolean

Returns:

  • (Boolean)


43
# File 'lib/active_record/bogacs/reaper.rb', line 43

def running?; @running end

#start(delay = nil) ⇒ Object



39
40
41
# File 'lib/active_record/bogacs/reaper.rb', line 39

def start(delay = nil)
  Thread.new { exec(delay) }
end