activerecord-mysql-reconnect 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/activerecord-mysql-reconnect.gemspec +25 -0
- data/lib/activerecord/mysql/reconnect/abstract_mysql_adapter.rb +35 -0
- data/lib/activerecord/mysql/reconnect/version.rb +7 -0
- data/lib/activerecord/mysql/reconnect.rb +2 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YjQ4MjM5ZDVlNmU3OWY3YTc4OGZjZGFhNjM4ZDgyYjM3YjZmM2I1Yg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZGJkZjJmZDJkNDA4OWVhY2EzOTZjOGEzYzVlMDkyNzJjZDY5MzZiOA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MTBkOTJmOGU1ZTU5YWNmNjk4NTU1ZTRlMjE5YjM3ZTkxZjZhNjY2MjEwZjU1
|
10
|
+
NzNlMGU2MGZjYjVlZDU1MmFhYjFiNTQ5ZmI1OGZlZmRhZjRmMjI4ZDg5M2Jm
|
11
|
+
YmZiYzMwMzcwNmEyZmY2Y2M0NTkwN2IxOWY5NWE1MGNiNjQ1Mzk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
M2ZmNjNmNjdlMTg2YzcwZTU0OWJiMWU2ZThkMWUwYThiNzNmMmI5ZGZkNzIy
|
14
|
+
MzNjNjc3NTZkOTg2MjY2M2RjZmFmZjAyYmE0Y2IxMDNjYzE2OWE3ZjYxNDZk
|
15
|
+
MzUzNWE3YzE3NDQyNmFmNjY1OWI1ODhmMTE2MWQzMThmOGQyYTA=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Genki Sugawara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Activerecord::Mysql::Reconnect
|
2
|
+
|
3
|
+
It is the library to reconnect automatically when ActiveRecord is disconnected from MySQL.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'activerecord-mysql-reconnect'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install activerecord-mysql-reconnect
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
#!/usr/bin/env ruby
|
23
|
+
require 'active_record'
|
24
|
+
require 'activerecord/mysql/reconnect'
|
25
|
+
require 'logger'
|
26
|
+
|
27
|
+
ActiveRecord::Base.establish_connection(
|
28
|
+
adapter: 'mysql2',
|
29
|
+
host: '127.0.0.1',
|
30
|
+
username: 'root',
|
31
|
+
database: 'test',
|
32
|
+
)
|
33
|
+
|
34
|
+
ActiveRecord::Base.logger = Logger.new($stdout)
|
35
|
+
ActiveRecord::Base.logger.formatter = proc {|_, _, _, message| "#{message}\n" }
|
36
|
+
ActiveRecord::Base.execution_tries = 3
|
37
|
+
|
38
|
+
class Employee < ActiveRecord::Base; end
|
39
|
+
|
40
|
+
p Employee.count
|
41
|
+
system('sudo /etc/init.d/mysqld restart')
|
42
|
+
p Employee.count
|
43
|
+
```
|
44
|
+
|
45
|
+
```
|
46
|
+
shell> ruby test.rb
|
47
|
+
(64.1ms) SELECT COUNT(*) FROM `employees`
|
48
|
+
300024
|
49
|
+
Stopping mysqld: [ OK ]
|
50
|
+
Starting mysqld: [ OK ]
|
51
|
+
(0.4ms) SELECT COUNT(*) FROM `employees`
|
52
|
+
Mysql2::Error: MySQL server has gone away: SELECT COUNT(*) FROM `employees`
|
53
|
+
MySQL server has gone away. Trying to reconnect in 0.5 seconds.
|
54
|
+
(101.5ms) SELECT COUNT(*) FROM `employees`
|
55
|
+
300024
|
56
|
+
```
|
57
|
+
|
58
|
+
## Link
|
59
|
+
|
60
|
+
* [RubyGems.org site](http://rubygems.org/gems/activerecord-mysql-reconnect)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord/mysql/reconnect/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-mysql-reconnect"
|
8
|
+
spec.version = Activerecord::Mysql::Reconnect::VERSION
|
9
|
+
spec.authors = ["Genki Sugawara"]
|
10
|
+
spec.email = ["sugawara@cookpad.com"]
|
11
|
+
spec.description = %q{It is the library to reconnect automatically when ActiveRecord is disconnected from MySQL.}
|
12
|
+
spec.summary = %q{It is the library to reconnect automatically when ActiveRecord is disconnected from MySQL.}
|
13
|
+
spec.homepage = "https://bitbucket.org/winebarrel/activerecord-mysql-reconnect"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", "~> 3.2.14"
|
22
|
+
spec.add_dependency "retryable", "~> 1.3.3"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/connection_adapters/abstract_mysql_adapter'
|
3
|
+
require 'active_support'
|
4
|
+
require 'retryable'
|
5
|
+
|
6
|
+
class ActiveRecord::Base
|
7
|
+
class_attribute :execution_tries
|
8
|
+
class_attribute :execution_retry_wait
|
9
|
+
end
|
10
|
+
|
11
|
+
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
|
12
|
+
MYSQL_SERVER_HAS_GONE_AWAY = 'MySQL server has gone away'
|
13
|
+
DEFAULT_EXECUTION_TRIES = 1
|
14
|
+
DEFAULT_EXECUTION_RETRY_WAIT = 0.5
|
15
|
+
|
16
|
+
def execute_with_reconnect(sql, name = nil)
|
17
|
+
retryable_options = {
|
18
|
+
:tries => (ActiveRecord::Base.execution_tries || DEFAULT_EXECUTION_TRIES),
|
19
|
+
:matching => /#{MYSQL_SERVER_HAS_GONE_AWAY}/,
|
20
|
+
:sleep => proc {|n|
|
21
|
+
wait = (ActiveRecord::Base.execution_retry_wait || DEFAULT_EXECUTION_RETRY_WAIT) * (n + 1)
|
22
|
+
logger.warn("#{MYSQL_SERVER_HAS_GONE_AWAY}. Trying to reconnect in #{wait} seconds.")
|
23
|
+
sleep(wait)
|
24
|
+
reconnect!
|
25
|
+
0
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
retryable(retryable_options) do
|
30
|
+
execute_without_reconnect(sql, name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
alias_method_chain :execute, :reconnect
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-mysql-reconnect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.14
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.14
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: retryable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.3.3
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.3.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: It is the library to reconnect automatically when ActiveRecord is disconnected
|
70
|
+
from MySQL.
|
71
|
+
email:
|
72
|
+
- sugawara@cookpad.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- activerecord-mysql-reconnect.gemspec
|
83
|
+
- lib/activerecord/mysql/reconnect.rb
|
84
|
+
- lib/activerecord/mysql/reconnect/abstract_mysql_adapter.rb
|
85
|
+
- lib/activerecord/mysql/reconnect/version.rb
|
86
|
+
homepage: https://bitbucket.org/winebarrel/activerecord-mysql-reconnect
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.1.5
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: It is the library to reconnect automatically when ActiveRecord is disconnected
|
110
|
+
from MySQL.
|
111
|
+
test_files: []
|