Class: Polipus::QueueOverflow::MongoQueueCapped

Inherits:
MongoQueue
  • Object
show all
Defined in:
lib/polipus/queue_overflow/mongo_queue_capped.rb

Instance Method Summary collapse

Methods inherited from MongoQueue

#clear, #empty?, #length, #pop

Constructor Details

#initialize(mongo_db, queue_name, options = {}) ⇒ MongoQueueCapped

Returns a new instance of MongoQueueCapped.



6
7
8
9
# File 'lib/polipus/queue_overflow/mongo_queue_capped.rb', line 6

def initialize(mongo_db, queue_name, options = {})
  super
  @max = @options[:max]
end

Instance Method Details

#push(data) ⇒ Object Also known as: enc, <<



11
12
13
14
15
16
17
18
19
20
# File 'lib/polipus/queue_overflow/mongo_queue_capped.rb', line 11

def push(data)
  super
  @semaphore.synchronize do
    s = size
    if s > @max
      docs = @mongo_db[@collection_name].find.sort(_id: 1).projection(_id: 1).limit(s - @max).map { |e| e['_id'] }
      @mongo_db[@collection_name].find(_id: { '$in' => docs }).delete_many
    end
  end
end