Module: Upperkut::Util

Instance Method Summary collapse

Instance Method Details

#decode_json_items(items) ⇒ Object



34
35
36
37
38
# File 'lib/upperkut/util.rb', line 34

def decode_json_items(items)
  items.each_with_object([]) do |item, memo|
    memo << Item.from_json(item) if item
  end
end

#normalize_items(items) ⇒ Object

Public:

Normalize hash and hash arrays into a hash of Items.
An Item object contains metadata, for example the timestamp from the moment it was enqueued,
 that we need to carry through multiple execution tries.

When the execution fails, we need to schedule the whole batch for retry, and scheduling
 an Item will make Upperkut understand that we're not dealing with a new batch,
 so metrics like latency will increase.


24
25
26
27
28
29
30
31
32
# File 'lib/upperkut/util.rb', line 24

def normalize_items(items)
  items = [items] unless items.is_a?(Array)

  items.map do |item|
    next item if item.is_a?(Item)

    Item.new(item)
  end
end

#retry_block(retries_limit = 3, base_sleep = 2) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/upperkut/util.rb', line 40

def retry_block(retries_limit = 3, base_sleep = 2)
  retries = 0

  begin
    yield
  rescue StandardError => err
    if retries < retries_limit
      retries += 1
      sleep_time = base_sleep**retries
      Kernel.sleep(sleep_time)
      retry
    end

    raise err
  end
end

#to_underscore(object) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/upperkut/util.rb', line 6

def to_underscore(object)
  klass_name = object
  klass_name.gsub!(/::/, '_')
  klass_name.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  klass_name.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  klass_name.tr!('-', '_')
  klass_name.downcase!
  klass_name
end