module UmbrellioUtils::Control

Public Instance Methods

retry_on(exception, times: Float::INFINITY, wait: 0) { || ... } click to toggle source
# 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 ) click to toggle source
# 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:) { || ... } click to toggle source
# 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) { || ... } click to toggle source
# 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