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.5.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,
]
- 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
74
75
76
|
# File 'lib/activerecord/mysql/reconnect.rb', line 74
def enable_retry
!!ActiveRecord::Base.enable_retry
end
|
.execution_retry_wait ⇒ Object
69
70
71
72
|
# File 'lib/activerecord/mysql/reconnect.rb', line 69
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
65
66
67
|
# File 'lib/activerecord/mysql/reconnect.rb', line 65
def execution_tries
ActiveRecord::Base.execution_tries || DEFAULT_EXECUTION_TRIES
end
|
.handle_r_error_messages ⇒ Object
57
58
59
|
# File 'lib/activerecord/mysql/reconnect.rb', line 57
def handle_r_error_messages
@@handle_r_error_messages
end
|
.handle_rw_error_messages ⇒ Object
61
62
63
|
# File 'lib/activerecord/mysql/reconnect.rb', line 61
def handle_rw_error_messages
@@handle_rw_error_messages
end
|
.logger ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/activerecord/mysql/reconnect.rb', line 151
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
114
115
116
|
# File 'lib/activerecord/mysql/reconnect.rb', line 114
def retry_databases
@activerecord_mysql_reconnect_retry_databases || []
end
|
.retry_databases=(v) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/activerecord/mysql/reconnect.rb', line 90
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
86
87
88
|
# File 'lib/activerecord/mysql/reconnect.rb', line 86
def retry_mode
@activerecord_mysql_reconnect_retry_mode || DEFAULT_RETRY_MODE
end
|
.retry_mode=(v) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/activerecord/mysql/reconnect.rb', line 78
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
118
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
|
# File 'lib/activerecord/mysql/reconnect.rb', line 118
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
159
160
161
162
163
164
165
166
|
# File 'lib/activerecord/mysql/reconnect.rb', line 159
def without_retry
begin
Thread.current[WITHOUT_RETRY_KEY] = true
yield
ensure
Thread.current[WITHOUT_RETRY_KEY] = nil
end
end
|
.without_retry? ⇒ Boolean
168
169
170
|
# File 'lib/activerecord/mysql/reconnect.rb', line 168
def without_retry?
!!Thread.current[WITHOUT_RETRY_KEY]
end
|