Class: HotTub::Reaper

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

Defined Under Namespace

Modules: Mixin

Class Method Summary collapse

Class Method Details

.spawn(obj) ⇒ Object

Creates a new Reaper thread for work. Expects an object that responses to: :reap! :shutdown and :reap_timeout Threads swallow exceptions until they are joined, so we rescue, log, and kill the reaper when an exception occurs bugs.ruby-lang.org/issues/6647



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hot_tub/reaper.rb', line 10

def self.spawn(obj)
  th = Thread.new {
    loop do
      begin
        break if obj.shutdown
        obj.reap!
        sleep(obj.reap_timeout || 600)
      rescue Exception => e
        HotTub.logger.error "[HotTub] Reaper for #{obj.class.name} terminated with exception: #{e.message}" if HotTub.logger
        HotTub.logger.error e.backtrace.map {|line| " #{line}"} if HotTub.logger
        break
      end
    end
  }
  th[:name] = "HotTub::Reaper"
  th.abort_on_exception = true
  th
end