Sha256: e99d99821730e9e3aab5d350e99f8bdc225d2ba4458b13ec9d711d3252f7ca14

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Refinery #:nodoc:
  # Base class for publishers to be implemented by subclasses.
  class Publisher
    include Refinery::Loggable
    include Refinery::Queueable
    
    # Initialize the publisher with the queue to publish messages to.
    def initialize(waiting_queue_name)
      @waiting_queue_name = waiting_queue_name
    end
    
    protected
    # Get the publish queue name
    def waiting_queue_name
      @waiting_queue_name
    end
    
    # Publish the message. The message will be converted to JSON and pushed
    # into the queue associated with the publisher.
    def publish(message)
      with_queue(waiting_queue_name) do |waiting_queue|
        publish_to_queue(waiting_queue, message)
      end
    end
    
    # Publish the given message if the waiting queue is empty. The message will
    # be converted to JSON and pushed into the queue associated with the
    # publisher.
    def publish_if_empty(message)
      with_queue(waiting_queue_name) do |waiting_queue|
        publish_to_queue(waiting_queue, message) if waiting_queue.size == 0
      end
    end
    
    # Publish the given message to the queue. The message will be converted 
    # to JSON and pushed into the queue associated with the publisher.
    def publish_to_queue(queue, message)
      logger.debug "Publisher #{self.class.name} sending message: #{message.to_json} to queue #{queue.url}"
      queue.send_message(Base64.encode64(message.to_json))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
refinery-0.10.9 lib/refinery/publisher.rb