module Garcon::Timeout

Class methods that are added when you include Garcon::Timeout

Public Instance Methods

timeout(seconds) { || ... } click to toggle source

Wait the given number of seconds for the block operation to complete. Intended to be a simpler and more reliable replacement to the Ruby standard library ‘Timeout::timeout` method.

@param [Integer] seconds

Number of seconds to wait for the block to terminate. Any number may
be used, including Floats to specify fractional seconds. A value of 0
or nil will execute the block without any timeout.

@return [Object]

Result of the block if the block completed before the timeout,
otherwise raises a TimeoutError exception.

@raise [Garcon::TimeoutError]

When the block operation does not complete in the alloted time.

@see Ruby Timeout::timeout

@api public

# File lib/garcon/utility/timeout.rb, line 48
def timeout(seconds)
  thread = Thread.new  { Thread.current[:result] = yield }
  thread.join(seconds) ? (return thread[:result]) : (raise TimeoutError)
ensure
  Thread.kill(thread) unless thread.nil?
end