Class: S3DataPacker::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_data_packer/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Queue

Returns a new instance of Queue.



5
6
7
8
9
# File 'lib/s3_data_packer/queue.rb', line 5

def initialize opts = {}
  @max_items = opts[:max_items]
  @wait_time = opts[:wait_time]
  @total_items = 0
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



3
4
5
# File 'lib/s3_data_packer/queue.rb', line 3

def items
  @items
end

#total_itemsObject (readonly)

Returns the value of attribute total_items.



3
4
5
# File 'lib/s3_data_packer/queue.rb', line 3

def total_items
  @total_items
end

Instance Method Details

#add!(item) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/s3_data_packer/queue.rb', line 23

def add!(item)
  items << item
  @total_items += 1
  if size >= max_items
    S3DataPacker.logger.info "Queue full, pausing"
    sleep(wait_time)
    S3DataPacker.logger.info "Resuming queue"
  end
end

#fetch!Object



33
34
35
# File 'lib/s3_data_packer/queue.rb', line 33

def fetch!
  items.shift
end

#max_itemsObject



11
12
13
# File 'lib/s3_data_packer/queue.rb', line 11

def max_items
  @max_items ||= S3DataPacker.config.max_queue_size
end

#reset!Object



41
42
43
44
# File 'lib/s3_data_packer/queue.rb', line 41

def reset!
  @items = []
  @total_items = 0
end

#sizeObject



37
38
39
# File 'lib/s3_data_packer/queue.rb', line 37

def size
  items.size
end

#wait_timeObject



15
16
17
# File 'lib/s3_data_packer/queue.rb', line 15

def wait_time
  @wait_time ||= S3DataPacker.config.max_queue_wait
end