Class: ExitTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/exit_timer.rb

Overview

Timer that exits the process if it expires. Use a timer thread, as the Qpid::Proton::Container does not yet provide scheduled tasks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout) ⇒ ExitTimer

Start the timer to exit after timeout.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/utils/exit_timer.rb', line 26

def initialize(timeout)
  @timeout = timeout
  @lock = Mutex.new
  reset
  Thread.new do
    while (delta = @lock.synchronize { @deadline - Time.now } ) > 0
      sleep delta
    end
    puts "timeout expired"
    exit(0)
  end
end

Instance Attribute Details

#timeoutObject (readonly)

Returns the value of attribute timeout.



23
24
25
# File 'lib/utils/exit_timer.rb', line 23

def timeout
  @timeout
end

Instance Method Details

#resetObject

Reset the timer to count to timeout from now



40
41
42
# File 'lib/utils/exit_timer.rb', line 40

def reset()
  @lock.synchronize { @deadline = Time.now + @timeout }
end