Sha256: 02f44a871de0f5abb0129200d5474d79ddd4821ab39e4cfe31e51e5bf9122a3f

Contents?: true

Size: 989 Bytes

Versions: 2

Compression:

Stored size: 989 Bytes

Contents

module Magent
  class GenericChannel
    attr_reader :name

    def initialize(name)
      @name = name
    end

    def enqueue(message)
      collection.save({:_id => generate_uid, :message => message, :priority => 3, :created_at => Time.now.to_i})
    end

    def message_count
      collection.count # TODO: number of processed messages (create a collection for stats)
    end

    def queue_count
      collection.count
    end

    def dequeue
      if m = self.next_message
        m["message"]
      end
    end

    def next_message
      Magent.database.command(BSON::OrderedHash[:findandmodify, @name,
                               :sort, [{:priority => -1}, {:created_at => 1}],
                               :remove, true
                              ])["value"]
    end

    def collection
      @collection ||= Magent.database.collection(@name)
    end

    protected
    def generate_uid
      UUIDTools::UUID.random_create.hexdigest
    end
  end # GenericChannel
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
thirtysixthspan-magent-0.4.1 lib/magent/generic_channel.rb
magent-0.4 lib/magent/generic_channel.rb