Module: UmbrellioUtils::Control

Extended by:
Control
Included in:
Control
Defined in:
lib/umbrellio_utils/control.rb

Defined Under Namespace

Classes: UniqueConstraintViolation

Instance Method Summary collapse

Instance Method Details

#retry_on(exception, times: Float::INFINITY, wait: 0) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/umbrellio_utils/control.rb', line 55

def retry_on(exception, times: Float::INFINITY, wait: 0)
  retries = 0

  begin
    yield
  rescue exception
    retries += 1
    raise if retries > times
    sleep(wait)
    retry
  end
end

#retry_on_unique_violation(times: Float::INFINITY, retry_on_all_constraints: false, checked_constraints: [], &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/umbrellio_utils/control.rb', line 21

def retry_on_unique_violation(
  times: Float::INFINITY,
  retry_on_all_constraints: false,
  checked_constraints: [],
  &block
)
  if !retry_on_all_constraints && checked_constraints.empty?
    begin
      return yield
    rescue Sequel::UniqueConstraintViolation => e
      raise UniqueConstraintViolation, e.message
    end
  end

  retry_on(Sequel::UniqueConstraintViolation, times:) do
    DB.transaction(savepoint: true, &block)
  rescue Sequel::UniqueConstraintViolation => e
    constraint_name = Database.get_violated_constraint_name(e)

    if retry_on_all_constraints || checked_constraints.include?(constraint_name)
      raise e
    else
      raise UniqueConstraintViolation, e.message
    end
  end
end

#run_in_interval(interval, key:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/umbrellio_utils/control.rb', line 9

def run_in_interval(interval, key:)
  previous_string = Store[key]
  previous = previous_string ? Time.zone.parse(previous_string) : Time.utc(0)

  return if previous + interval > Time.current
  Store[key] = Time.current

  yield
ensure
  Store.delete(key) rescue nil
end

#run_non_critical(rescue_all: false, in_transaction: false, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/umbrellio_utils/control.rb', line 48

def run_non_critical(rescue_all: false, in_transaction: false, &block)
  in_transaction ? DB.transaction(savepoint: true, &block) : yield
rescue (rescue_all ? Exception : StandardError) => e
  Exceptions.notify!(e)
  nil
end