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
48
49
50
51
52
|
# File 'lib/transaction_retry/active_record/base.rb', line 20
def transaction_with_retry(**objects, &block)
retry_count = 0
opts = if objects.is_a? Hash
objects
else
{}
end
retry_on = opts.delete(:retry_on)
max_retries = opts.delete(:max_retries) || TransactionRetry.max_retries
begin
transaction_without_retry(**objects, &block)
rescue ::ActiveRecord::TransactionIsolationConflict, *retry_on
raise if retry_count >= max_retries
raise if tr_in_nested_transaction?
retry_count += 1
postfix = { 1 => 'st', 2 => 'nd', 3 => 'rd' }[retry_count] || 'th'
type_s = case $ERROR_INFO
when ::ActiveRecord::TransactionIsolationConflict
'Transaction isolation conflict'
else
$ERROR_INFO.class.name
end
logger&.warn "#{type_s} detected. Retrying for the #{retry_count}-#{postfix} time..."
tr_exponential_pause(retry_count)
retry
end
end
|