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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 71e451da7545c291e59d8f60e217b79e079012b9
4
- data.tar.gz: e1b74d9e7e5c7a099c74cd0c53e919a4c7519858
2
+ SHA256:
3
+ metadata.gz: b22787c59f28400f81aa1dc6647ecd14ceca54ecb648285ddee05e96c652c90f
4
+ data.tar.gz: 0f97dc3481ca776b51390fb01ed799df7043be4e20299534726f51e75b584c88
5
5
  SHA512:
6
- metadata.gz: 66a5760999823b8d478147048333654dc3c2decbbb9d4649b49712f5f36701fa02a9fa5e634da909ac8f1632c4ce04c177f18b9f851bdbb669af32b2f127945a
7
- data.tar.gz: 04ed93663318b7ef51c7e3a8f71da99f46b0e286db93555b1bc6384a558d582b6840b5d8519ce7dff436db7a372ce083d89523a7e1721a258618659c98519231
6
+ metadata.gz: d7da6b4b5ff50f35254f9bdc9f14d040cb8930d483d35b574edf1d7ec8c024ee9cc4455fdeb85589ec2c920e299bbf6d0c5cf6f896f36efb70d2fc69ec90e8e1
7
+ data.tar.gz: cd45c8a2b11f9db45830da921a755dab9db9793910323c2b36f05da3c27ebdab51577e3de6d7566d9fb1a8b60b38a22ea817bf21c70b0f4a8f21a68c8559bb6d
@@ -2,10 +2,9 @@ dist: trusty
2
2
  sudo: required
3
3
  language: ruby
4
4
  rvm:
5
- - 2.1.9
6
- - 2.2.6
7
- - 2.3.4
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
@@ -3,5 +3,6 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.2.0"
6
+ gem "mysql2", "< 0.5"
6
7
 
7
8
  gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.2.0"
6
+
7
+ gemspec path: "../"
@@ -29,25 +29,24 @@ module Activerecord::Mysql::Reconnect
29
29
  Mysql2::Error,
30
30
  ]
31
31
 
32
- HANDLE_R_ERROR_MESSAGES = [
33
- 'Lost connection to MySQL server during query',
34
- ]
35
-
36
- HANDLE_RW_ERROR_MESSAGES = [
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(HANDLE_ERROR_MESSAGES) =~ e.message
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(HANDLE_R_ERROR_MESSAGES) =~ e.message
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
@@ -1,7 +1,7 @@
1
1
  module Activerecord
2
2
  module Mysql
3
3
  module Reconnect
4
- VERSION = '0.4.1'
4
+ VERSION = '0.4.2'
5
5
  end
6
6
  end
7
7
  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(Mysql2::Error).to receive(:message).and_return('Lost connection to MySQL server during query')
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
- "Mysql2::Error: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]",
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 [Mysql2::Error]",
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 [Mysql2::Error]",
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(Mysql2::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
- "Mysql2::Error: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]",
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.1
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: 2017-07-12 00:00:00.000000000 Z
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.12
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