Class: MQRPC::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/mqrpc/operation.rb

Overview

A single message operation

  • Takes a callback to call when a message is received

  • Allows you to wait for the operation to complete.

  • An operation is ‘complete’ when the callback returns :finished

Instance Method Summary collapse

Constructor Details

#initialize(callback) ⇒ Operation

Returns a new instance of Operation.



11
12
13
14
15
16
# File 'lib/mqrpc/operation.rb', line 11

def initialize(callback)
  @mutex = Mutex.new
  @callback = callback
  @cv = ConditionVariable.new
  @finished = false
end

Instance Method Details

#call(*args) ⇒ Object

def initialize



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mqrpc/operation.rb', line 18

def call(*args)
  # TODO(sissel): Come up with a better way for the callback to declare
  # that it is not done than simply returning ':continue'
  @mutex.synchronize do
    ret = @callback.call(*args)
    if ret != :continue
      #MQRPC::logger.debug "operation #{self} finished"
      @finished = true
      @cv.signal
    end
    return ret
  end
end

#wait_until_finishedObject

Block until the operation has finished. If the operation has already finished, this method will return immediately.



35
36
37
38
39
40
41
# File 'lib/mqrpc/operation.rb', line 35

def wait_until_finished
  @mutex.synchronize do
    if !finished?
      @cv.wait(@mutex)
    end
  end
end