Module: OpenStaxTransactionRetry::ActiveRecord::Base::ClassMethods
- Defined in:
- lib/open_stax_transaction_retry/active_record/base.rb
Instance Method Summary collapse
-
#transaction_with_retry(**objects, &block) ⇒ Object
rubocop:todo Metrics/PerceivedComplexity.
Instance Method Details
#transaction_with_retry(**objects, &block) ⇒ Object
rubocop:todo Metrics/PerceivedComplexity
20 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 47 |
# File 'lib/open_stax_transaction_retry/active_record/base.rb', line 20 def transaction_with_retry(**objects, &block) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity retry_count = 0 opts = if objects.is_a? Hash objects elsif objects.is_a?(Array) && objects.last.is_a?(Hash) objects.last else {} end retry_on = opts.delete(:retry_on) || OpenStaxTransactionRetry.retry_on max_retries = opts.delete(:max_retries) || OpenStaxTransactionRetry.max_retries before_retry = opts.delete(:before_retry) || OpenStaxTransactionRetry.before_retry begin transaction_without_retry(**objects, &block) rescue ::ActiveRecord::TransactionIsolationConflict, *retry_on => e raise if retry_count >= max_retries raise if tr_in_nested_transaction? retry_count += 1 logger&.warn "#{e.class.name} detected. Retry num #{retry_count}..." before_retry&.call(retry_count, e) tr_exponential_pause(retry_count) retry end end |