Module: Garcon::Timeout

Extended by:
Timeout
Included in:
Provider, Timeout
Defined in:
lib/garcon/utility/timeout.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#timeout(seconds) ⇒ Object

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.

Parameters:

  • seconds (Integer)

    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.

Returns:

  • (Object)

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

Raises:

  • (Garcon::TimeoutError)

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

See Also:

  • Timeout::timeout


48
49
50
51
52
53
# 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