Sha256: 7e540276571544aede0f191802b2d0a92d007aa8ff8650521145a62749fa602d

Contents?: true

Size: 946 Bytes

Versions: 1

Compression:

Stored size: 946 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
      rec = Magent.database.command(BSON::OrderedHash[:findandmodify, @name,
        :sort, [{:priority => -1}, {:created_at => 1}],
        :remove, true
      ]) rescue {}

      rec["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

1 entries across 1 versions & 1 rubygems

Version Path
magent-0.4.2 lib/magent/generic_channel.rb