activerecord-mysql-reconnect 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.travis.yml +4 -10
- data/Appraisals +5 -0
- data/README.md +9 -0
- data/gemfiles/activerecord_4.2.gemfile +1 -0
- data/gemfiles/activerecord_5.2.gemfile +7 -0
- data/lib/activerecord/mysql/reconnect.rb +28 -21
- data/lib/activerecord/mysql/reconnect/version.rb +1 -1
- data/spec/activerecord-mysql-reconnect_spec.rb +37 -6
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b22787c59f28400f81aa1dc6647ecd14ceca54ecb648285ddee05e96c652c90f
|
4
|
+
data.tar.gz: 0f97dc3481ca776b51390fb01ed799df7043be4e20299534726f51e75b584c88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7da6b4b5ff50f35254f9bdc9f14d040cb8930d483d35b574edf1d7ec8c024ee9cc4455fdeb85589ec2c920e299bbf6d0c5cf6f896f36efb70d2fc69ec90e8e1
|
7
|
+
data.tar.gz: cd45c8a2b11f9db45830da921a755dab9db9793910323c2b36f05da3c27ebdab51577e3de6d7566d9fb1a8b60b38a22ea817bf21c70b0f4a8f21a68c8559bb6d
|
data/.travis.yml
CHANGED
@@ -2,10 +2,9 @@ dist: trusty
|
|
2
2
|
sudo: required
|
3
3
|
language: ruby
|
4
4
|
rvm:
|
5
|
-
- 2.
|
6
|
-
- 2.
|
7
|
-
- 2.
|
8
|
-
- 2.4.1
|
5
|
+
- 2.3.6
|
6
|
+
- 2.4.4
|
7
|
+
- 2.5.1
|
9
8
|
before_install:
|
10
9
|
- docker-compose up -d
|
11
10
|
- function mysql_ping { mysqladmin -u root -h 127.0.0.1 -ppassword ping > /dev/null 2> /dev/null; }
|
@@ -18,16 +17,11 @@ gemfile:
|
|
18
17
|
- gemfiles/activerecord_4.2.gemfile
|
19
18
|
- gemfiles/activerecord_5.0.gemfile
|
20
19
|
- gemfiles/activerecord_5.1.gemfile
|
20
|
+
- gemfiles/activerecord_5.2.gemfile
|
21
21
|
env:
|
22
22
|
matrix:
|
23
23
|
- ACTIVERECORD_MYSQL_RECONNECT_ENGINE=InnoDB
|
24
24
|
- ACTIVERECORD_MYSQL_RECONNECT_ENGINE=MyISAM
|
25
|
-
matrix:
|
26
|
-
exclude:
|
27
|
-
- rvm: 2.1.9
|
28
|
-
gemfile: gemfiles/activerecord_5.0.gemfile
|
29
|
-
- rvm: 2.1.9
|
30
|
-
gemfile: gemfiles/activerecord_5.1.gemfile
|
31
25
|
addons:
|
32
26
|
apt:
|
33
27
|
packages:
|
data/Appraisals
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
appraise "activerecord-4.2" do
|
2
2
|
gem "activerecord", "~> 4.2.0"
|
3
|
+
gem "mysql2", "< 0.5"
|
3
4
|
end
|
4
5
|
|
5
6
|
appraise "activerecord-5.0" do
|
@@ -9,3 +10,7 @@ end
|
|
9
10
|
appraise "activerecord-5.1" do
|
10
11
|
gem "activerecord", "~> 5.1.0"
|
11
12
|
end
|
13
|
+
|
14
|
+
appraise "activerecord-5.2" do
|
15
|
+
gem "activerecord", "~> 5.2.0"
|
16
|
+
end
|
data/README.md
CHANGED
@@ -66,6 +66,15 @@ ActiveRecord::Base.without_retry do
|
|
66
66
|
end
|
67
67
|
```
|
68
68
|
|
69
|
+
### Add a retry error message
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Activerecord::Mysql::Reconnect.handle_rw_error_messages.update(
|
73
|
+
zapzapzap: 'ZapZapZap'
|
74
|
+
)
|
75
|
+
# or `Activerecord::Mysql::Reconnect.handle_r_error_messages...`
|
76
|
+
```
|
77
|
+
|
69
78
|
## Use on rails
|
70
79
|
|
71
80
|
### Gemfile
|
@@ -29,25 +29,24 @@ module Activerecord::Mysql::Reconnect
|
|
29
29
|
Mysql2::Error,
|
30
30
|
]
|
31
31
|
|
32
|
-
|
33
|
-
'Lost connection to MySQL server during query',
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
'MySQL server has gone away',
|
38
|
-
'Server shutdown in progress',
|
39
|
-
'closed MySQL connection',
|
40
|
-
"Can't connect to MySQL server",
|
41
|
-
'Query execution was interrupted',
|
42
|
-
'Access denied for user',
|
43
|
-
'The MySQL server is running with the --read-only option',
|
44
|
-
"Can't connect to local MySQL server", # When running in local sandbox, or using a socket file
|
45
|
-
'Unknown MySQL server host', # For DNS blips
|
46
|
-
"Lost connection to MySQL server at 'reading initial communication packet'",
|
47
|
-
"MySQL client is not connected",
|
48
|
-
|
49
|
-
|
50
|
-
HANDLE_ERROR_MESSAGES = HANDLE_R_ERROR_MESSAGES + HANDLE_RW_ERROR_MESSAGES
|
32
|
+
@@handle_r_error_messages = {
|
33
|
+
lost_connection: 'Lost connection to MySQL server during query',
|
34
|
+
}
|
35
|
+
|
36
|
+
@@handle_rw_error_messages = {
|
37
|
+
gone_away: 'MySQL server has gone away',
|
38
|
+
server_shutdown: 'Server shutdown in progress',
|
39
|
+
closed_connection: 'closed MySQL connection',
|
40
|
+
cannot_connect: "Can't connect to MySQL server",
|
41
|
+
interrupted: 'Query execution was interrupted',
|
42
|
+
access_denied: 'Access denied for user',
|
43
|
+
read_only: 'The MySQL server is running with the --read-only option',
|
44
|
+
cannot_connect_to_local: "Can't connect to local MySQL server", # When running in local sandbox, or using a socket file
|
45
|
+
unknown_host: 'Unknown MySQL server host', # For DNS blips
|
46
|
+
lost_connection: "Lost connection to MySQL server at 'reading initial communication packet'",
|
47
|
+
not_connected: "MySQL client is not connected",
|
48
|
+
killed: 'Connection was killed',
|
49
|
+
}
|
51
50
|
|
52
51
|
READ_SQL_REGEXP = /\A\s*(?:SELECT|SHOW|SET)\b/i
|
53
52
|
|
@@ -55,6 +54,14 @@ module Activerecord::Mysql::Reconnect
|
|
55
54
|
DEFAULT_RETRY_MODE = :r
|
56
55
|
|
57
56
|
class << self
|
57
|
+
def handle_r_error_messages
|
58
|
+
@@handle_r_error_messages
|
59
|
+
end
|
60
|
+
|
61
|
+
def handle_rw_error_messages
|
62
|
+
@@handle_rw_error_messages
|
63
|
+
end
|
64
|
+
|
58
65
|
def execution_tries
|
59
66
|
ActiveRecord::Base.execution_tries || DEFAULT_EXECUTION_TRIES
|
60
67
|
end
|
@@ -195,7 +202,7 @@ module Activerecord::Mysql::Reconnect
|
|
195
202
|
return false
|
196
203
|
end
|
197
204
|
|
198
|
-
unless Regexp.union(
|
205
|
+
unless Regexp.union(@@handle_r_error_messages.values + @@handle_rw_error_messages.values) =~ e.message
|
199
206
|
return false
|
200
207
|
end
|
201
208
|
|
@@ -204,7 +211,7 @@ module Activerecord::Mysql::Reconnect
|
|
204
211
|
return false
|
205
212
|
end
|
206
213
|
|
207
|
-
if retry_mode != :force and Regexp.union(
|
214
|
+
if retry_mode != :force and Regexp.union(@@handle_r_error_messages.values) =~ e.message
|
208
215
|
return false
|
209
216
|
end
|
210
217
|
end
|
@@ -451,27 +451,31 @@ describe 'activerecord-mysql-reconnect' do
|
|
451
451
|
"%s (cause: %s, sql: SELECT `employees`.* FROM `employees`, connection: host=127.0.0.1;database=employees;username=root)"
|
452
452
|
end
|
453
453
|
|
454
|
+
let(:mysql_error) do
|
455
|
+
Mysql2::Error.const_defined?(:ConnectionError) ? Mysql2::Error::ConnectionError : Mysql2::Error
|
456
|
+
end
|
457
|
+
|
454
458
|
before do
|
455
|
-
allow_any_instance_of(
|
459
|
+
allow_any_instance_of(mysql_error).to receive(:message).and_return('Lost connection to MySQL server during query')
|
456
460
|
end
|
457
461
|
|
458
462
|
context "when retry failed " do
|
459
463
|
specify do
|
460
464
|
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
|
461
465
|
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
|
462
|
-
"
|
466
|
+
"#{mysql_error}: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]",
|
463
467
|
])
|
464
468
|
|
465
469
|
(1.0..4.5).step(0.5).each do |sec|
|
466
470
|
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
|
467
471
|
"MySQL server has gone away. Trying to reconnect in #{sec} seconds.",
|
468
|
-
"Lost connection to MySQL server during query [
|
472
|
+
"Lost connection to MySQL server during query [#{mysql_error}]",
|
469
473
|
])
|
470
474
|
end
|
471
475
|
|
472
476
|
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
|
473
477
|
"Query retry failed.",
|
474
|
-
"Lost connection to MySQL server during query [
|
478
|
+
"Lost connection to MySQL server during query [#{mysql_error}]",
|
475
479
|
])
|
476
480
|
|
477
481
|
expect(Employee.all.length).to eq 1000
|
@@ -479,7 +483,7 @@ describe 'activerecord-mysql-reconnect' do
|
|
479
483
|
|
480
484
|
expect {
|
481
485
|
Employee.all.length
|
482
|
-
}.to raise_error(
|
486
|
+
}.to raise_error(mysql_error)
|
483
487
|
end
|
484
488
|
end
|
485
489
|
|
@@ -487,7 +491,7 @@ describe 'activerecord-mysql-reconnect' do
|
|
487
491
|
specify do
|
488
492
|
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
|
489
493
|
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
|
490
|
-
"
|
494
|
+
"#{mysql_error}: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]",
|
491
495
|
])
|
492
496
|
|
493
497
|
expect(Employee.all.length).to eq 1000
|
@@ -496,4 +500,31 @@ describe 'activerecord-mysql-reconnect' do
|
|
496
500
|
end
|
497
501
|
end
|
498
502
|
end
|
503
|
+
|
504
|
+
# NOTE: The following test need to execute at the last
|
505
|
+
context "when the custom error is happened" do
|
506
|
+
before do
|
507
|
+
allow_any_instance_of(Mysql2::Error).to receive(:message).and_return('ZapZapZap')
|
508
|
+
Activerecord::Mysql::Reconnect.handle_rw_error_messages.update(zapzapzap: 'ZapZapZap')
|
509
|
+
end
|
510
|
+
|
511
|
+
specify do
|
512
|
+
th = thread_start {
|
513
|
+
emp = Employee.create(
|
514
|
+
:emp_no => 9999,
|
515
|
+
:birth_date => Time.now,
|
516
|
+
# wait 10 sec
|
517
|
+
:first_name => "' + sleep(10) + '",
|
518
|
+
:last_name => 'Tiger',
|
519
|
+
:hire_date => Time.now
|
520
|
+
)
|
521
|
+
|
522
|
+
expect(emp.id).to eq 1001
|
523
|
+
expect(emp.emp_no).to eq 9999
|
524
|
+
}
|
525
|
+
|
526
|
+
MysqlServer.restart
|
527
|
+
th.join
|
528
|
+
end
|
529
|
+
end
|
499
530
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-mysql-reconnect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Genki Sugawara
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- gemfiles/activerecord_4.2.gemfile
|
117
117
|
- gemfiles/activerecord_5.0.gemfile
|
118
118
|
- gemfiles/activerecord_5.1.gemfile
|
119
|
+
- gemfiles/activerecord_5.2.gemfile
|
119
120
|
- lib/activerecord-mysql-reconnect.rb
|
120
121
|
- lib/activerecord/mysql/reconnect.rb
|
121
122
|
- lib/activerecord/mysql/reconnect/abstract_mysql_adapter_ext.rb
|
@@ -150,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
151
|
version: '0'
|
151
152
|
requirements: []
|
152
153
|
rubyforge_project:
|
153
|
-
rubygems_version: 2.6
|
154
|
+
rubygems_version: 2.7.6
|
154
155
|
signing_key:
|
155
156
|
specification_version: 4
|
156
157
|
summary: It is the library to reconnect automatically when ActiveRecord is disconnected
|