Class: HotTub::Reaper
- Inherits:
-
Object
- Object
- HotTub::Reaper
- Defined in:
- lib/hot_tub/reaper.rb
Defined Under Namespace
Modules: Mixin
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(obj) ⇒ Reaper
constructor
Creates a new Reaper thread for work.
- #shutdown ⇒ Object
- #status ⇒ Object
- #wakeup ⇒ Object
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.}" 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
#thread ⇒ Object (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
47 48 49 |
# File 'lib/hot_tub/reaper.rb', line 47 def alive? @thread.alive? end |
#shutdown ⇒ Object
42 43 44 45 |
# File 'lib/hot_tub/reaper.rb', line 42 def shutdown @thread.kill @thread.join end |
#status ⇒ Object
34 35 36 |
# File 'lib/hot_tub/reaper.rb', line 34 def status @thread.status end |
#wakeup ⇒ Object
38 39 40 |
# File 'lib/hot_tub/reaper.rb', line 38 def wakeup @thread.wakeup end |