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.

If your callback returns :continue, then we will not call finished. This allows you to have an operation that is invoked multiple times, such as for streaming blocks of data, and only finish when you know you are done.

Instance Method Summary collapse

Constructor Details

#initialize(&callback) ⇒ Operation

Returns a new instance of Operation.



16
17
18
19
20
21
# File 'lib/mqrpc/operation.rb', line 16

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

Instance Method Details

#call(*args) ⇒ Object

def initialize



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mqrpc/operation.rb', line 23

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
      _withlock_finished
    end
    return ret
  end
end

#finishedObject

Declare that the operation is finished.



54
55
56
57
58
# File 'lib/mqrpc/operation.rb', line 54

def finished
  @mutex.synchronize do
    return _withlock_finished
  end
end

#finished?Boolean

Is the operation finished yet?

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/mqrpc/operation.rb', line 47

def finished?
  @mutex.synchronize do
    return _withlock_finished?
  end
end

#wait_until_finishedObject

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



38
39
40
41
42
43
44
# File 'lib/mqrpc/operation.rb', line 38

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