Module: ActiveRecord::Retry::ClassMethods

Defined in:
lib/active_record/retry.rb

Instance Method Summary collapse

Instance Method Details

#find_by_sql_with_retry(*args, &block) ⇒ Object



39
40
41
# File 'lib/active_record/retry.rb', line 39

def find_by_sql_with_retry(*args, &block)
  with_retry { find_by_sql_without_retry(*args, &block) }
end

#transaction_with_retry(*args, &block) ⇒ Object



43
44
45
# File 'lib/active_record/retry.rb', line 43

def transaction_with_retry(*args, &block)
  with_retry { transaction_without_retry(*args, &block) }
end

#with_retryObject



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