class Toolshed::Timeout

github.com/ruby/ruby/blob/trunk/lib/timeout.rb This code had to be modified so the timeout could be extended instead of just being a fixnum. This is import for the SSH client as we want it to keep running for an unlimited time period as long as we are getting output from the client. When that stops then the timeout needs to kick in just in case the server is no longer responding or a connection got lost. Too bad ruby core doesn't already support something like this.

Attributes

start_time[RW]
timeout_period[R]

Public Class Methods

new(options = nil) click to toggle source
# File lib/toolshed/timeout.rb, line 18
def initialize(options = nil)
  options ||= {}
  @timeout_period = options[:timeout_period] || 30
  @start_time = options[:start_time] || Time.now.utc.to_i
end

Public Instance Methods

reset_start_time() click to toggle source
# File lib/toolshed/timeout.rb, line 24
def reset_start_time
  @start_time = Time.now.utc.to_i
end
start(klass = nil) { |sec| ... } click to toggle source
# File lib/toolshed/timeout.rb, line 28
def start(klass = nil)   #:yield: +sec+
  return yield(timeout_period) if timeout_period == nil or timeout_period.zero?
  message = "execution expired in #{timeout_period} seconds".freeze
  e = Error
  bl = proc do |exception|
    begin
      x = Thread.current
      y = Thread.start {
        begin
          sleep(1) until timed_out?
        rescue => e
          x.raise e
        else
          x.raise exception, message
        end
      }
      return yield(timeout_period)
    ensure
      if y
        y.kill
        y.join # make sure y is dead.
      end
    end
  end
  if klass
    begin
      bl.call(klass)
    rescue klass => e
      bt = e.backtrace
    end
  else
    bt = Error.catch(message, &bl)
  end
  level = -caller(CALLER_OFFSET).size-2
  while THIS_FILE =~ bt[level]
    bt.delete_at(level)
  end
  raise(e, message, bt)
end
timed_out?() click to toggle source
# File lib/toolshed/timeout.rb, line 68
def timed_out?
  Time.now.utc.to_i - start_time > timeout_period
end