Module SystemTimer
In: lib/system_timer/concurrent_timer_pool.rb
lib/system_timer/thread_timer.rb
lib/system_timer.rb
lib/system_timer_stub.rb

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

Methods

Classes and Modules

Class SystemTimer::ConcurrentTimerPool
Class SystemTimer::ThreadTimer

External Aliases

timeout_after -> timeout
  Backward compatibility with timeout.rb
timeout_after -> timeout
  Backward compatibility with timeout.rb

Attributes

timer_pool  [R] 

Public Class methods

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

[Source]

    # File lib/system_timer.rb, line 48
48:     def timeout_after(seconds, exception_class = nil)
49:       new_timer = nil                                      # just for scope
50:       @monitor.synchronize do
51:         new_timer = timer_pool.add_timer seconds, exception_class
52:         timer_interval = timer_pool.next_trigger_interval_in_seconds
53:         debug "==== Install Timer ==== at #{Time.now.to_f}, next interval: #{timer_interval}"
54:         if timer_pool.first_timer?
55:           install_first_timer_and_save_original_configuration timer_interval
56:         else
57:           install_next_timer timer_interval
58:         end
59:       end
60:       return yield
61:     ensure
62:       @monitor.synchronize do
63:         debug "==== Cleanup Timer ==== at #{Time.now.to_f}, #{new_timer} "
64:         timer_pool.cancel new_timer
65:         timer_pool.log_registered_timers if debug_enabled?
66:         next_interval = timer_pool.next_trigger_interval_in_seconds
67:         debug "Cleanup Timer : next interval #{next_interval.inspect} "
68:         if next_interval
69:           install_next_timer next_interval
70:         else
71:           restore_original_configuration
72:         end
73:       end
74:     end

[Source]

    # File lib/system_timer_stub.rb, line 8
 8:    def timeout_after(seconds)
 9:      Timeout::timeout(seconds) do
10:        yield
11:      end
12:    end

Protected Class methods

[Source]

     # File lib/system_timer.rb, line 104
104:    def debug(message)    #:nodoc
105:      puts message if debug_enabled?
106:    end

[Validate]