Sha256: c3ab85f5de8402decdb71f57bbd0f09f6fe709d0a8d8394ea4f0f1642325e682

Contents?: true

Size: 983 Bytes

Versions: 2

Compression:

Stored size: 983 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(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 & 1 rubygems

Version Path
magent-0.3 lib/magent/generic_channel.rb
magent-0.2 lib/magent/generic_channel.rb