Sha256: 8a515e0e57a9e262ed8ee7479be9dc26aa78f7bbd6ca05dbbbb886ec8a3093a4
Contents?: true
Size: 1.48 KB
Versions: 25
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: 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
25 entries across 25 versions & 1 rubygems