Class: AsyncCall

Inherits:
Object
  • Object
show all
Defined in:
lib/capybarbecue/async_call.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, method, *args, &block) ⇒ AsyncCall

Returns a new instance of AsyncCall.



9
10
11
12
13
14
# File 'lib/capybarbecue/async_call.rb', line 9

def initialize(obj, method, *args, &block)
  @obj, @method, @args, @block = obj, method, args, block
  @ready = false
  @response = nil
  @exception = nil
end

Instance Attribute Details

#readyObject (readonly) Also known as: ready?

Returns the value of attribute ready.



5
6
7
# File 'lib/capybarbecue/async_call.rb', line 5

def ready
  @ready
end

#to_sObject (readonly)

Returns the value of attribute to_s.



6
7
8
# File 'lib/capybarbecue/async_call.rb', line 6

def to_s
  @to_s
end

Instance Method Details

#runObject

Should be called from the executer



16
17
18
19
20
21
22
# File 'lib/capybarbecue/async_call.rb', line 16

def run  # Should be called from the executer
  begin
    respond_with @obj.send(@method, *@args, &@block)
  rescue Exception => e
    respond_with_exception e
  end
end

#wait_for_response(timeout = DEFAULT_TIMEOUT) ⇒ Object

If a block is passed, it will be called repeatedly until a response comes in

Raises:

  • (Timeout::Error)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capybarbecue/async_call.rb', line 25

def wait_for_response(timeout=DEFAULT_TIMEOUT)
  started_at = Time.now
  while Time.now - started_at < timeout.seconds && !ready?
    yield if block_given?
    # It feels dangerous not to sleep here... keep a pulse on this (sleep causes performance problems)
    Thread.pass
  end
  raise Timeout::Error.new('Timeout expired before response received') unless ready?
  if @exception.present?
    # Add the backtrace from this thread to make it useful
    backtrace = @exception.backtrace + Kernel.caller
    @exception.set_backtrace(backtrace)
    raise @exception
  end
  @response
end