Module: Activerecord::Mysql::Reconnect
- Defined in:
- lib/activerecord/mysql/reconnect/version.rb,
lib/activerecord/mysql/reconnect.rb
Defined Under Namespace
Modules: ExecuteWithReconnect, NewConnectionWithRetry, NullTransactionExt, ReconnectWithRetry
Constant Summary
collapse
- VERSION =
'0.6.0'
- DEFAULT_EXECUTION_TRIES =
3
- DEFAULT_EXECUTION_RETRY_WAIT =
0.5
- WITHOUT_RETRY_KEY =
'activerecord-mysql-reconnect-without-retry'
- HANDLE_ERROR =
[
ActiveRecord::StatementInvalid,
Mysql2::Error,
ActiveRecord::ConnectionNotEstablished
]
- READ_SQL_REGEXP =
/\A\s*(?:SELECT|SHOW|SET)\b/i
- RETRY_MODES =
[:r, :rw, :force]
- DEFAULT_RETRY_MODE =
:r
- @@handle_r_error_messages =
{
lost_connection: 'Lost connection to MySQL server during query',
}
- @@handle_rw_error_messages =
{
gone_away: 'MySQL server has gone away',
server_shutdown: 'Server shutdown in progress',
closed_connection: 'closed MySQL connection',
cannot_connect: "Can't connect to MySQL server",
interrupted: 'Query execution was interrupted',
access_denied: 'Access denied for user',
read_only: 'The MySQL server is running with the --read-only option',
cannot_connect_to_local: "Can't connect to local MySQL server", unknown_host: 'Unknown MySQL server host', lost_connection: "Lost connection to MySQL server at 'reading initial communication packet'",
not_connected: "MySQL client is not connected",
killed: 'Connection was killed',
}
Class Method Summary
collapse
Class Method Details
.enable_retry ⇒ Object
75
76
77
|
# File 'lib/activerecord/mysql/reconnect.rb', line 75
def enable_retry
!!ActiveRecord::Base.enable_retry
end
|
.execution_retry_wait ⇒ Object
70
71
72
73
|
# File 'lib/activerecord/mysql/reconnect.rb', line 70
def execution_retry_wait
wait = ActiveRecord::Base.execution_retry_wait || DEFAULT_EXECUTION_RETRY_WAIT
wait.kind_of?(BigDecimal) ? wait : BigDecimal(wait.to_s)
end
|
.execution_tries ⇒ Object
66
67
68
|
# File 'lib/activerecord/mysql/reconnect.rb', line 66
def execution_tries
ActiveRecord::Base.execution_tries || DEFAULT_EXECUTION_TRIES
end
|
.handle_r_error_messages ⇒ Object
58
59
60
|
# File 'lib/activerecord/mysql/reconnect.rb', line 58
def handle_r_error_messages
@@handle_r_error_messages
end
|
.handle_rw_error_messages ⇒ Object
62
63
64
|
# File 'lib/activerecord/mysql/reconnect.rb', line 62
def handle_rw_error_messages
@@handle_rw_error_messages
end
|
.logger ⇒ Object
152
153
154
155
156
157
158
|
# File 'lib/activerecord/mysql/reconnect.rb', line 152
def logger
if defined?(Rails)
Rails.logger || ActiveRecord::Base.logger || Logger.new($stderr)
else
ActiveRecord::Base.logger || Logger.new($stderr)
end
end
|
.retry_databases ⇒ Object
115
116
117
|
# File 'lib/activerecord/mysql/reconnect.rb', line 115
def retry_databases
@activerecord_mysql_reconnect_retry_databases || []
end
|
.retry_databases=(v) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/activerecord/mysql/reconnect.rb', line 91
def retry_databases=(v)
v ||= []
unless v.kind_of?(Array)
v = [v]
end
@activerecord_mysql_reconnect_retry_databases = v.map do |database|
if database.instance_of?(Symbol)
database = Regexp.escape(database.to_s)
[/.*/, /\A#{database}\z/]
else
host = '%'
database = database.to_s
if database =~ /:/
host, database = database.split(':', 2)
end
[create_pattern_match_regex(host), create_pattern_match_regex(database)]
end
end
end
|
.retry_mode ⇒ Object
87
88
89
|
# File 'lib/activerecord/mysql/reconnect.rb', line 87
def retry_mode
@activerecord_mysql_reconnect_retry_mode || DEFAULT_RETRY_MODE
end
|
.retry_mode=(v) ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/activerecord/mysql/reconnect.rb', line 79
def retry_mode=(v)
unless RETRY_MODES.include?(v)
raise "Invalid retry_mode. Please set one of the following: #{RETRY_MODES.map {|i| i.inspect }.join(', ')}"
end
@activerecord_mysql_reconnect_retry_mode = v
end
|
.retryable(opts) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/activerecord/mysql/reconnect.rb', line 119
def retryable(opts)
block = opts.fetch(:proc)
on_error = opts[:on_error]
conn = opts[:connection]
sql = opts[:sql]
tries = self.execution_tries
retval = nil
retryable_loop(tries) do |n|
begin
retval = block.call
break
rescue => e
if enable_retry and (tries.zero? or n < tries) and should_handle?(e, opts)
on_error.call if on_error
wait = self.execution_retry_wait * n
logger.warn("MySQL server has gone away. Trying to reconnect in #{wait.to_f} seconds. (#{build_error_message(e, sql, conn)})")
sleep(wait)
next
else
if enable_retry and n > 1
logger.warn("Query retry failed. (#{build_error_message(e, sql, conn)})")
end
raise e
end
end
end
return retval
end
|
.without_retry ⇒ Object
160
161
162
163
164
165
166
167
|
# File 'lib/activerecord/mysql/reconnect.rb', line 160
def without_retry
begin
Thread.current[WITHOUT_RETRY_KEY] = true
yield
ensure
Thread.current[WITHOUT_RETRY_KEY] = nil
end
end
|
.without_retry? ⇒ Boolean
169
170
171
|
# File 'lib/activerecord/mysql/reconnect.rb', line 169
def without_retry?
!!Thread.current[WITHOUT_RETRY_KEY]
end
|