Class: Upperkut::Strategies::BufferedQueue

Inherits:
Base
  • Object
show all
Includes:
Util
Defined in:
lib/upperkut/strategies/buffered_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#decode_json_items, #normalize_items, #retry_block, #to_underscore

Constructor Details

#initialize(worker, options = {}) ⇒ BufferedQueue

Returns a new instance of BufferedQueue.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/upperkut/strategies/buffered_queue.rb', line 12

def initialize(worker, options = {})
  @options = options
  @redis_options = options.fetch(:redis, {})
  @worker     = worker
  @max_wait   = options.fetch(
    :max_wait,
    Integer(ENV['UPPERKUT_MAX_WAIT'] || 20)
  )

  @batch_size = options.fetch(
    :batch_size,
    Integer(ENV['UPPERKUT_BATCH_SIZE'] || 1000)
  )

  @waiting_time = 0
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/upperkut/strategies/buffered_queue.rb', line 10

def options
  @options
end

Instance Method Details

#clearObject



52
53
54
# File 'lib/upperkut/strategies/buffered_queue.rb', line 52

def clear
  redis { |conn| conn.del(key) }
end

#fetch_itemsObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/upperkut/strategies/buffered_queue.rb', line 40

def fetch_items
  stop = [@batch_size, size].min

  items = redis do |conn|
    conn.multi do
      stop.times { conn.lpop(key) }
    end
  end

  decode_json_items(items)
end

#metricsObject



56
57
58
59
60
61
# File 'lib/upperkut/strategies/buffered_queue.rb', line 56

def metrics
  {
    'latency' => latency,
    'size' => size
  }
end

#process?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/upperkut/strategies/buffered_queue.rb', line 63

def process?
  buff_size = size

  if fulfill_condition?(buff_size)
    @waiting_time = 0
    return true
  else
    @waiting_time += @worker.setup.polling_interval
    return false
  end
end

#push_items(items = []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/upperkut/strategies/buffered_queue.rb', line 29

def push_items(items = [])
  items = normalize_items(items)
  return false if items.empty?

  redis do |conn|
    conn.rpush(key, items.map(&:to_json))
  end

  true
end