Class: HotTub::Reaper

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

Defined Under Namespace

Modules: Mixin

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Reaper

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hot_tub/reaper.rb', line 15

def initialize(obj)
  @thread = 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
  }
  @thread[:name] = "HotTub::Reaper"
  @thread.abort_on_exception = true
  @thread
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



3
4
5
# File 'lib/hot_tub/reaper.rb', line 3

def thread
  @thread
end

Class Method Details

.spawn(obj) ⇒ Object



5
6
7
# File 'lib/hot_tub/reaper.rb', line 5

def self.spawn(obj)
  self.new(obj)
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/hot_tub/reaper.rb', line 47

def alive?
  @thread.alive?
end

#shutdownObject



42
43
44
45
# File 'lib/hot_tub/reaper.rb', line 42

def shutdown
  @thread.kill
  @thread.join
end

#statusObject



34
35
36
# File 'lib/hot_tub/reaper.rb', line 34

def status
  @thread.status
end

#wakeupObject



38
39
40
# File 'lib/hot_tub/reaper.rb', line 38

def wakeup
  @thread.wakeup
end