Module: DumpCleaner::Cleanup::Uniqueness

Included in:
Cleaning
Defined in:
lib/dump_cleaner/cleanup/uniqueness.rb

Defined Under Namespace

Classes: CaseInsensitiveCache, MaxRetriesReachedError

Instance Method Summary collapse

Instance Method Details

#repeat_until_unique(step_context:, max_retries: 1000, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dump_cleaner/cleanup/uniqueness.rb', line 8

def repeat_until_unique(step_context:, max_retries: 1000, &block)
  n = 0
  result = nil

  loop do
    result = block.call(n)

    break unless result

    if n.positive?
      Log.debug do
        msg = "Uniqueness run:  type=#{step_context.type}, id=#{step_context.record['id']}, "
        msg << "orig_value=#{step_context.orig_value}, current_value=#{result}, repetition=#{n}"
      end
    end

    unless CaseInsensitiveCache.instance.known?(type: step_context.type, value: result)
      CaseInsensitiveCache.instance.push(type: step_context.type, value: result)
      break
    end

    if n >= max_retries
      warning = "Max retry count #{n} reached for ID:#{step_context.record['id']}, type:#{step_context.type}, "
      warning << "orig:#{step_context.orig_value}, current:#{result}"
      Log.warn { warning }
      raise MaxRetriesReachedError
    end

    n += 1
  end

  result
end