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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/umbrellio_utils/control.rb', line 44

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
# File 'lib/umbrellio_utils/control.rb', line 21

def retry_on_unique_violation(
  times: Float::INFINITY, retry_on_all_constraints: false, checked_constraints: [], &block
)
  retry_on(Sequel::UniqueConstraintViolation, times: 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



37
38
39
40
41
42
# File 'lib/umbrellio_utils/control.rb', line 37

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