Module: Timeout

Defined in:
lib/polyphony/extensions/timeout.rb

Overview

Timeout extensions

Class Method Summary collapse

Class Method Details

.timeout(sec, klass = Timeout::Error, message = 'execution expired', &block) ⇒ any

Sets a timeout for the given block. This method provides an equivalent API to the stock Timeout API provided by Ruby. In case of a timeout, the block will be interrupted and an exception will be raised according to the given arguments.

Parameters:

  • sec (Number)

    timeout period in seconds

  • klass (Class) (defaults to: Timeout::Error)

    exception class

  • message (String) (defaults to: 'execution expired')

    exception message

Returns:

  • (any)

    block's return value



16
17
18
19
20
21
22
# File 'lib/polyphony/extensions/timeout.rb', line 16

def self.timeout(sec, klass = Timeout::Error, message = 'execution expired', &block)
  if sec.nil? || sec == 0
    block.call
  else
    cancel_after(sec, with_exception: [klass, message], &block)
  end
end