47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/active_record/retry.rb', line 47
def with_retry
tries = 0
begin
yield
rescue ActiveRecord::StatementInvalid => error
raise if connection.open_transactions != 0
raise if tries >= retries.count
found, actions = retry_errors.detect { |regex, action| regex =~ error.message }
raise unless found
actions = Array(actions)
delay = retries[tries]
tries += 1
if logger
message = "Query failed: '#{error}'. "
message << actions.map do |action|
case action
when :sleep
"sleeping for #{delay}s"
when :reconnect
"reconnecting"
when :retry
"retrying"
end
end.join(", ").capitalize
message << " for the #{tries.ordinalize} time."
logger.warn(message)
end
sleep(delay) if actions.include?(:sleep)
if actions.include?(:reconnect)
clear_active_connections!
establish_connection
end
retry if actions.include?(:retry)
end
end
|