Sha256: a5875883ce128b9c1623128e4572a6fb59aa9d159f76ea516abc800214afd68a

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module UmbrellioUtils
  module Control
    extend self

    class UniqueConstraintViolation < StandardError; end

    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

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

    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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
umbrellio-utils-1.5.2 lib/umbrellio_utils/control.rb
umbrellio-utils-1.5.1 lib/umbrellio_utils/control.rb
umbrellio-utils-1.5.0 lib/umbrellio_utils/control.rb