Method: SystemTimer.timeout_after

Defined in:
lib/system_timer.rb,
lib/system_timer_stub.rb

.timeout_after(seconds) ⇒ Object Also known as: timeout

Executes the method’s block. If the block execution terminates before seconds seconds has passed, it returns true. If not, it terminates the execution and raises a Timeout::Error.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/system_timer.rb', line 43

def timeout_after(seconds)
  new_timer = nil                                      # just for scope
  @mutex.synchronize do
    new_timer = timer_pool.add_timer seconds
    timer_interval = timer_pool.next_trigger_interval_in_seconds
    debug "==== Install Timer ==== at #{Time.now.to_i}, next interval: #{timer_interval}"
    if timer_pool.first_timer?
      install_first_timer_and_save_original_configuration timer_interval
    else
      install_next_timer timer_interval
    end
 end
  return yield
ensure
  @mutex.synchronize do
    debug "==== Cleanup Timer ==== at #{Time.now.to_i}, #{new_timer} "
    timer_pool.cancel new_timer
    timer_pool.log_registered_timers if debug_enabled?
    next_interval = timer_pool.next_trigger_interval_in_seconds
    debug "Cleanup Timer : next interval #{next_interval.inspect} "
    if next_interval
      install_next_timer next_interval
    else
      restore_original_configuration          
    end
  end
end