Module: SimpleTimeout

Defined in:
lib/simple_timeout.rb,
lib/simple_timeout/version.rb

Defined Under Namespace

Classes: Control, Error

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.timeout(seconds, timeout_error_class = SimpleTimeout::Error, wait_for_block = false, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_timeout.rb', line 2

def self.timeout(seconds,timeout_error_class=SimpleTimeout::Error,wait_for_block=false,&block)
  if seconds == 0
    raise timeout_error_class.new, "execution expired"
  end

  if seconds.nil? || !seconds.is_a?(Numeric) || seconds < 0
    raise ArgumentError.new, "seconds must be an Integer number greater or equal to 0"
  end

  if block_given?
    control = SimpleTimeout::Control.new(seconds,timeout_error_class)

    block_thread = build_block_thread(control,&block)

    timeout_thread = build_timeout_thread(control)

    while control.status == :waiting;end
    
    timeout_thread.kill if timeout_thread.alive?
    block_thread.kill if block_thread.alive?

    if wait_for_block
      block_thread.join
    end

    handle_control_status control

  end
end