module SystemTimer

Timer based on underlying ITIMER_REAL system timer. It is a solution to Ruby processes which hang beyond the time limit when accessing external resources. This is useful when timeout.rb, which relies on green threads, does not work consistently.

For more information and background check out:

Usage

require 'systemtimer'

SystemTimer.timeout_after(5) do

  # Something that should be interrupted if it takes too much time...
  # ... even if blocked on a system call!

end

Copyright 2008 David Vollbracht & Philippe Hanrigou

Copyright 2008 David Vollbracht & Philippe Hanrigou

Attributes

timer_pool[R]

Public Class Methods

timeout(seconds, exception_class = nil) click to toggle source

Backward compatibility with timeout.rb

Alias for: timeout_after
timeout_after(seconds, exception_class = nil) { || ... } click to toggle source

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+.

# File lib/system_timer.rb, line 48
def timeout_after(seconds, exception_class = nil)
  new_timer = nil                                      # just for scope
  @monitor.synchronize do
    new_timer = timer_pool.add_timer seconds, exception_class
    timer_interval = timer_pool.next_trigger_interval_in_seconds
    debug "==== Install Timer ==== at #{Time.now.to_f}, 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
  @monitor.synchronize do
    debug "==== Cleanup Timer ==== at #{Time.now.to_f}, #{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
Also aliased as: timeout, timeout

Protected Class Methods

debug(message) click to toggle source
# File lib/system_timer.rb, line 104
def debug(message)    #:nodoc
  puts message if debug_enabled?
end