class Ruote::Synchronize::Broker

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

Public Class Methods

new(context) click to toggle source

@param [Ruote::Context] context (needed by Ruote)

# File lib/ruote/synchronize/broker.rb, line 13
def initialize(context)
  @context = context
end

Public Instance Methods

publish(key, workitem) click to toggle source

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. @param [#to_s] key The unique key to do the lookup with @param [Ruote::Workitem] workitem The workitem to store @return [true, false] True if there was a previous workitem

with the same key, false otherwise
# 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) click to toggle source

Deletes a previously stored key from storage @param [#to_s] key The key to delere @return [void]

# File lib/ruote/synchronize/broker.rb, line 43
def unpublish(key)
  doc = stored_doc_from_key(key)
  @context.storage.delete(doc) if doc
end

Private Instance Methods

continue_with(doc) click to toggle source
# File lib/ruote/synchronize/broker.rb, line 54
def continue_with(doc)

  stored_workitem = Ruote::Workitem.new(doc['workitem'])
  receive(stored_workitem)
  @context.storage.delete(doc)

end
stored_doc_from_key(key) click to toggle source
# File lib/ruote/synchronize/broker.rb, line 50
def stored_doc_from_key(key)
  @context.storage.get('synchronize', key)
end
wait_for(key, workitem) click to toggle source
# File lib/ruote/synchronize/broker.rb, line 62
def wait_for(key, workitem)

  doc = {
    'type' => 'synchronize',
    '_id' => key,
    'workitem' => workitem.to_h
  }
  @context.storage.put(doc)

end