activerecord-mysql-reconnect 0.4.1.beta → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c37f85305063d95208c4a30ace243a62709d0d25
4
- data.tar.gz: 55d1bf38531ede94d81d977e33f3def41719f09d
3
+ metadata.gz: 71e451da7545c291e59d8f60e217b79e079012b9
4
+ data.tar.gz: e1b74d9e7e5c7a099c74cd0c53e919a4c7519858
5
5
  SHA512:
6
- metadata.gz: a83fe523cc08085211d963711f06264d20728cf8d7e3d3c9458cbd57f77faee1a2e597bd8487bc804bc81d83a23cdec747732a4d28c1b670ebe12de6f9376e2d
7
- data.tar.gz: 17ce95bde54d2b35ef1a9f92dd9a6546dd1e6f02a85fd8bdc276c6d49133e95e960c1f14730e44d8c5f82e0e984772a0f12cd7763111a7c5ded6e6dac5d8c388
6
+ metadata.gz: 66a5760999823b8d478147048333654dc3c2decbbb9d4649b49712f5f36701fa02a9fa5e634da909ac8f1632c4ce04c177f18b9f851bdbb669af32b2f127945a
7
+ data.tar.gz: 04ed93663318b7ef51c7e3a8f71da99f46b0e286db93555b1bc6384a558d582b6840b5d8519ce7dff436db7a372ce083d89523a7e1721a258618659c98519231
data/.gitignore CHANGED
@@ -18,3 +18,4 @@ tmp
18
18
  test.rb
19
19
  *.bak
20
20
  *.swp
21
+ /gemfiles/*.lock
@@ -3,20 +3,21 @@ sudo: required
3
3
  language: ruby
4
4
  rvm:
5
5
  - 2.1.9
6
- - 2.2.5
7
- - 2.3.1
6
+ - 2.2.6
7
+ - 2.3.4
8
+ - 2.4.1
8
9
  before_install:
9
10
  - docker-compose up -d
10
11
  - function mysql_ping { mysqladmin -u root -h 127.0.0.1 -ppassword ping > /dev/null 2> /dev/null; }
11
12
  - for i in {1..60}; do mysql_ping && break; sleep 1; done
12
13
  - gem install bundler
13
14
  - docker-compose stop
14
- - sudo rm -rf /tmp/mysql_for_ar_mysql_reconn
15
15
  script:
16
16
  - bundle exec rake
17
17
  gemfile:
18
18
  - gemfiles/activerecord_4.2.gemfile
19
19
  - gemfiles/activerecord_5.0.gemfile
20
+ - gemfiles/activerecord_5.1.gemfile
20
21
  env:
21
22
  matrix:
22
23
  - ACTIVERECORD_MYSQL_RECONNECT_ENGINE=InnoDB
@@ -25,6 +26,8 @@ matrix:
25
26
  exclude:
26
27
  - rvm: 2.1.9
27
28
  gemfile: gemfiles/activerecord_5.0.gemfile
29
+ - rvm: 2.1.9
30
+ gemfile: gemfiles/activerecord_5.1.gemfile
28
31
  addons:
29
32
  apt:
30
33
  packages:
data/Appraisals CHANGED
@@ -5,3 +5,7 @@ end
5
5
  appraise "activerecord-5.0" do
6
6
  gem "activerecord", "~> 5.0.0"
7
7
  end
8
+
9
+ appraise "activerecord-5.1" do
10
+ gem "activerecord", "~> 5.1.0"
11
+ end
@@ -1,8 +1,6 @@
1
1
  mysql_for_ar_mysql_reconn:
2
2
  image: "mysql:5.6"
3
3
  ports:
4
- - "3306:3306"
4
+ - "14407:3306"
5
5
  environment:
6
- MYSQL_ROOT_PASSWORD: password
7
- volumes:
8
- - /tmp/mysql_for_ar_mysql_reconn:/var/lib/mysql
6
+ MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
@@ -4,4 +4,4 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.2.0"
6
6
 
7
- gemspec :path => "../"
7
+ gemspec path: "../"
@@ -4,4 +4,4 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 5.0.0"
6
6
 
7
- gemspec :path => "../"
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.1.0"
6
+
7
+ gemspec path: "../"
@@ -44,6 +44,7 @@ module Activerecord::Mysql::Reconnect
44
44
  "Can't connect to local MySQL server", # When running in local sandbox, or using a socket file
45
45
  'Unknown MySQL server host', # For DNS blips
46
46
  "Lost connection to MySQL server at 'reading initial communication packet'",
47
+ "MySQL client is not connected",
47
48
  ]
48
49
 
49
50
  HANDLE_ERROR_MESSAGES = HANDLE_R_ERROR_MESSAGES + HANDLE_RW_ERROR_MESSAGES
@@ -111,6 +112,7 @@ module Activerecord::Mysql::Reconnect
111
112
  block = opts.fetch(:proc)
112
113
  on_error = opts[:on_error]
113
114
  conn = opts[:connection]
115
+ sql = opts[:sql]
114
116
  tries = self.execution_tries
115
117
  retval = nil
116
118
 
@@ -123,17 +125,14 @@ module Activerecord::Mysql::Reconnect
123
125
  on_error.call if on_error
124
126
  wait = self.execution_retry_wait * n
125
127
 
126
- opt_msgs = ["cause: #{e} [#{e.class}]"]
127
-
128
- if conn
129
- conn_info = connection_info(conn)
130
- opt_msgs << 'connection: ' + [:host, :database, :username].map {|k| "#{k}=#{conn_info[k]}" }.join(";")
131
- end
132
-
133
- logger.warn("MySQL server has gone away. Trying to reconnect in #{wait.to_f} seconds. (#{opt_msgs.join(', ')})")
128
+ logger.warn("MySQL server has gone away. Trying to reconnect in #{wait.to_f} seconds. (#{build_error_message(e, sql, conn)})")
134
129
  sleep(wait)
135
130
  next
136
131
  else
132
+ if enable_retry and n > 1
133
+ logger.warn("Query retry failed. (#{build_error_message(e, sql, conn)})")
134
+ end
135
+
137
136
  raise e
138
137
  end
139
138
  end
@@ -225,8 +224,6 @@ module Activerecord::Mysql::Reconnect
225
224
  return conn_info
226
225
  end
227
226
 
228
- private
229
-
230
227
  def create_pattern_match_regex(str)
231
228
  ss = StringScanner.new(str)
232
229
  buf = []
@@ -247,5 +244,17 @@ module Activerecord::Mysql::Reconnect
247
244
 
248
245
  /\A#{buf.join}\z/
249
246
  end
247
+
248
+ def build_error_message(e, sql, conn)
249
+ msgs = {cause: "#{e.message} [#{e.class}]"}
250
+ msgs[:sql] = sql if sql
251
+
252
+ if conn
253
+ conn_info = connection_info(conn)
254
+ msgs[:connection] = [:host, :database, :username].map {|k| "#{k}=#{conn_info[k]}" }.join(";")
255
+ end
256
+
257
+ msgs.map {|k, v| "#{k}: #{v}" }.join(", ")
258
+ end
250
259
  end # end of class methods
251
260
  end
@@ -1,7 +1,7 @@
1
1
  module Activerecord
2
2
  module Mysql
3
3
  module Reconnect
4
- VERSION = '0.4.1.beta'
4
+ VERSION = '0.4.1'
5
5
  end
6
6
  end
7
7
  end
@@ -4,7 +4,8 @@ describe 'activerecord-mysql-reconnect' do
4
4
  :adapter => 'mysql2',
5
5
  :host => '127.0.0.1',
6
6
  :username => 'root',
7
- :database => 'employees'
7
+ :database => 'employees',
8
+ :port => 14407,
8
9
  )
9
10
 
10
11
  ActiveRecord::Base.logger = Logger.new($stdout)
@@ -444,4 +445,55 @@ describe 'activerecord-mysql-reconnect' do
444
445
  end
445
446
  end
446
447
  end
448
+
449
+ context "when retry with warning" do
450
+ let(:warning_template) do
451
+ "%s (cause: %s, sql: SELECT `employees`.* FROM `employees`, connection: host=127.0.0.1;database=employees;username=root)"
452
+ end
453
+
454
+ before do
455
+ allow_any_instance_of(Mysql2::Error).to receive(:message).and_return('Lost connection to MySQL server during query')
456
+ end
457
+
458
+ context "when retry failed " do
459
+ specify do
460
+ expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
461
+ "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]",
463
+ ])
464
+
465
+ (1.0..4.5).step(0.5).each do |sec|
466
+ expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
467
+ "MySQL server has gone away. Trying to reconnect in #{sec} seconds.",
468
+ "Lost connection to MySQL server during query [Mysql2::Error]",
469
+ ])
470
+ end
471
+
472
+ expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
473
+ "Query retry failed.",
474
+ "Lost connection to MySQL server during query [Mysql2::Error]",
475
+ ])
476
+
477
+ expect(Employee.all.length).to eq 1000
478
+ MysqlServer.stop
479
+
480
+ expect {
481
+ Employee.all.length
482
+ }.to raise_error(Mysql2::Error)
483
+ end
484
+ end
485
+
486
+ context "when retry succeeded" do
487
+ specify do
488
+ expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
489
+ "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]",
491
+ ])
492
+
493
+ expect(Employee.all.length).to eq 1000
494
+ MysqlServer.restart
495
+ expect(Employee.all.length).to eq 1000
496
+ end
497
+ end
498
+ end
447
499
  end
@@ -1,15 +1,10 @@
1
1
  require 'mysql2'
2
2
 
3
3
  class MysqlServer
4
- CLI_ARGS = '-u root -P 3306 -h 127.0.0.1'
4
+ CLI_ARGS = '-u root -P 14407 -h 127.0.0.1'
5
5
  REDIRECT_TO_DEV_NULL = ENV['DEBUG'] == '1' ? '' : '> /dev/null 2> /dev/null'
6
6
 
7
7
  class << self
8
- def setup
9
- clean
10
- system('mkdir -p /tmp/mysql_for_ar_mysql_reconn')
11
- end
12
-
13
8
  def start
14
9
  system("docker-compose up -d #{REDIRECT_TO_DEV_NULL}")
15
10
  wait_mysql_start
@@ -44,11 +39,6 @@ class MysqlServer
44
39
 
45
40
  def clean
46
41
  stop
47
-
48
- if ENV['SKIP_CLEAN_DATA'] != '1'
49
- system("docker-compose rm -f mysql_for_ar_mysql_reconn #{REDIRECT_TO_DEV_NULL}")
50
- system('rm -rf /tmp/mysql_for_ar_mysql_reconn')
51
- end
52
42
  end
53
43
 
54
44
  def reset
@@ -35,12 +35,11 @@ include SpecHelper
35
35
 
36
36
  RSpec.configure do |config|
37
37
  config.before(:all) do
38
- ENV['MYSQL_PWD'] = 'password'
39
- MysqlServer.setup
38
+ MysqlServer.stop
40
39
  end
41
40
 
42
41
  config.after(:all) do
43
- MysqlServer.clean
42
+ MysqlServer.stop
44
43
  end
45
44
 
46
45
  config.before(:each) do |context|
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.beta
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-01 00:00:00.000000000 Z
11
+ date: 2017-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -114,9 +114,8 @@ files:
114
114
  - activerecord-mysql-reconnect.gemspec
115
115
  - docker-compose.yml
116
116
  - gemfiles/activerecord_4.2.gemfile
117
- - gemfiles/activerecord_4.2.gemfile.lock
118
117
  - gemfiles/activerecord_5.0.gemfile
119
- - gemfiles/activerecord_5.0.gemfile.lock
118
+ - gemfiles/activerecord_5.1.gemfile
120
119
  - lib/activerecord-mysql-reconnect.rb
121
120
  - lib/activerecord/mysql/reconnect.rb
122
121
  - lib/activerecord/mysql/reconnect/abstract_mysql_adapter_ext.rb
@@ -146,12 +145,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
145
  version: '0'
147
146
  required_rubygems_version: !ruby/object:Gem::Requirement
148
147
  requirements:
149
- - - ">"
148
+ - - ">="
150
149
  - !ruby/object:Gem::Version
151
- version: 1.3.1
150
+ version: '0'
152
151
  requirements: []
153
152
  rubyforge_project:
154
- rubygems_version: 2.4.5.1
153
+ rubygems_version: 2.6.12
155
154
  signing_key:
156
155
  specification_version: 4
157
156
  summary: It is the library to reconnect automatically when ActiveRecord is disconnected
@@ -1,66 +0,0 @@
1
- PATH
2
- remote: ../
3
- specs:
4
- activerecord-mysql-reconnect (0.4.0)
5
- activerecord
6
- mysql2
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activemodel (4.2.6)
12
- activesupport (= 4.2.6)
13
- builder (~> 3.1)
14
- activerecord (4.2.6)
15
- activemodel (= 4.2.6)
16
- activesupport (= 4.2.6)
17
- arel (~> 6.0)
18
- activesupport (4.2.6)
19
- i18n (~> 0.7)
20
- json (~> 1.7, >= 1.7.7)
21
- minitest (~> 5.1)
22
- thread_safe (~> 0.3, >= 0.3.4)
23
- tzinfo (~> 1.1)
24
- appraisal (2.1.0)
25
- bundler
26
- rake
27
- thor (>= 0.14.0)
28
- arel (6.0.3)
29
- builder (3.2.2)
30
- diff-lcs (1.2.5)
31
- i18n (0.7.0)
32
- json (1.8.3)
33
- minitest (5.8.4)
34
- mysql2 (0.4.4)
35
- rake (11.1.2)
36
- rspec (3.4.0)
37
- rspec-core (~> 3.4.0)
38
- rspec-expectations (~> 3.4.0)
39
- rspec-mocks (~> 3.4.0)
40
- rspec-core (3.4.4)
41
- rspec-support (~> 3.4.0)
42
- rspec-expectations (3.4.0)
43
- diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.4.0)
45
- rspec-mocks (3.4.1)
46
- diff-lcs (>= 1.2.0, < 2.0)
47
- rspec-support (~> 3.4.0)
48
- rspec-support (3.4.1)
49
- thor (0.19.1)
50
- thread_safe (0.3.5)
51
- tzinfo (1.2.2)
52
- thread_safe (~> 0.1)
53
-
54
- PLATFORMS
55
- ruby
56
-
57
- DEPENDENCIES
58
- activerecord (~> 4.2.0)
59
- activerecord-mysql-reconnect!
60
- appraisal
61
- bundler
62
- rake
63
- rspec (>= 3.0.0)
64
-
65
- BUNDLED WITH
66
- 1.11.2
@@ -1,63 +0,0 @@
1
- PATH
2
- remote: ../
3
- specs:
4
- activerecord-mysql-reconnect (0.4.0)
5
- activerecord
6
- mysql2
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activemodel (5.0.0)
12
- activesupport (= 5.0.0)
13
- activerecord (5.0.0)
14
- activemodel (= 5.0.0)
15
- activesupport (= 5.0.0)
16
- arel (~> 7.0)
17
- activesupport (5.0.0)
18
- concurrent-ruby (~> 1.0, >= 1.0.2)
19
- i18n (~> 0.7)
20
- minitest (~> 5.1)
21
- tzinfo (~> 1.1)
22
- appraisal (2.1.0)
23
- bundler
24
- rake
25
- thor (>= 0.14.0)
26
- arel (7.1.1)
27
- concurrent-ruby (1.0.2)
28
- diff-lcs (1.2.5)
29
- i18n (0.7.0)
30
- minitest (5.9.0)
31
- mysql2 (0.4.4)
32
- rake (11.2.2)
33
- rspec (3.5.0)
34
- rspec-core (~> 3.5.0)
35
- rspec-expectations (~> 3.5.0)
36
- rspec-mocks (~> 3.5.0)
37
- rspec-core (3.5.2)
38
- rspec-support (~> 3.5.0)
39
- rspec-expectations (3.5.0)
40
- diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.5.0)
42
- rspec-mocks (3.5.0)
43
- diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.5.0)
45
- rspec-support (3.5.0)
46
- thor (0.19.1)
47
- thread_safe (0.3.5)
48
- tzinfo (1.2.2)
49
- thread_safe (~> 0.1)
50
-
51
- PLATFORMS
52
- ruby
53
-
54
- DEPENDENCIES
55
- activerecord (~> 5.0.0)
56
- activerecord-mysql-reconnect!
57
- appraisal
58
- bundler
59
- rake
60
- rspec (>= 3.0.0)
61
-
62
- BUNDLED WITH
63
- 1.11.2