Class: Ruote::Synchronize::Broker

Inherits:
Object
  • Object
show all
Includes:
ReceiverMixin
Defined in:
lib/ruote/synchronize/broker.rb

Overview

A Ruote Receiver that act as a broker between two processes. It will store workitems under the synchronize storage type.

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Broker

Returns a new instance of Broker.

Parameters:

  • context (Ruote::Context)

    (needed by Ruote)



13
14
15
# File 'lib/ruote/synchronize/broker.rb', line 13

def initialize(context)
  @context = context
end

Instance Method Details

#publish(key, workitem) ⇒ true, false

Check if there was a previous workitem stored with the same key. If so, receives and deletes the stored workitem. Else, stores the workitem with the key.

Parameters:

  • key (#to_s)

    The unique key to do the lookup with

  • workitem (Ruote::Workitem)

    The workitem to store

Returns:

  • (true, false)

    True if there was a previous workitem with the same key, false otherwise



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruote/synchronize/broker.rb', line 24

def publish(key, workitem)

  if doc = stored_doc_from_key(key)
    # another process already registered the same key
    # allow both processes to continue
    continue_with doc
    true
  else
    # this process is the first to register
    # store the workitem for now
    wait_for key, workitem
    false
  end

end

#unpublish(key) ⇒ void

This method returns an undefined value.

Deletes a previously stored key from storage

Parameters:

  • key (#to_s)

    The key to delere



43
44
45
46
# File 'lib/ruote/synchronize/broker.rb', line 43

def unpublish(key)
  doc = stored_doc_from_key(key)
  @context.storage.delete(doc) if doc
end