Sha256: 665d6f0e34023298eda445bc51e708803515d2202146a325b84603263278a384

Contents?: true

Size: 983 Bytes

Versions: 6

Compression:

Stored size: 983 Bytes

Contents

module Magent
  class GenericChannel
    include Magent::Failure

    attr_reader :name

    def initialize(name)
      @name = "magent.#{name}"
    end

    def enqueue(message, priority = 3)
      collection.save({:_id => generate_uid, :message => message, :priority => priority, :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
      collection.find_and_modify(:sort => [[:priority, Mongo::ASCENDING], [:created_at, Mongo::DESCENDING]],
                                 :remove => true) rescue {}
    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

6 entries across 6 versions & 1 rubygems

Version Path
magent-0.6.0 lib/magent/generic_channel.rb
magent-0.5.4 lib/magent/generic_channel.rb
magent-0.5.3 lib/magent/generic_channel.rb
magent-0.5.2 lib/magent/generic_channel.rb
magent-0.5.1 lib/magent/generic_channel.rb
magent-0.5.0 lib/magent/generic_channel.rb