pg_reconnect 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_reconnect.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 'Konstantin Makarchev'
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,10 @@
1
+ # PgReconnect
2
+
3
+ ActiveRecord PostgreSql auto-reconnection, works with 2.3 and 3.2 rails.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pg_reconnect'
10
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,46 @@
1
+ class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
2
+
3
+ MAX_QUERY_RETRIES = 1
4
+
5
+ alias_method :execute_without_retry, :execute
6
+ alias_method :query_without_retry, :query
7
+
8
+ def query(*args)
9
+ res = query_without_retry(*args)
10
+ @retry_counter = nil
11
+ res
12
+ rescue ActiveRecord::StatementInvalid => ex
13
+ unless active?
14
+ @retry_counter ||= 0
15
+ @logger.warn "Reconnecting to database after PGError! Try ##{@retry_counter + 1}/#{MAX_QUERY_RETRIES} #{ex.message}, trace: #{ex.backtrace.inspect}"
16
+ reconnect! rescue nil
17
+
18
+ if @retry_counter < MAX_QUERY_RETRIES
19
+ @retry_counter += 1
20
+ retry
21
+ end
22
+ end
23
+
24
+ raise
25
+ end
26
+
27
+ def execute(*args)
28
+ res = execute_without_retry(*args)
29
+ @retry_counter = nil
30
+ res
31
+ rescue ActiveRecord::StatementInvalid => ex
32
+ unless active?
33
+ @retry_counter ||= 0
34
+ @logger.warn "Reconnecting to database after PGError! Try ##{@retry_counter + 1}/#{MAX_QUERY_RETRIES} #{ex.message}, trace: #{ex.backtrace.inspect}"
35
+ reconnect! rescue nil
36
+
37
+ if @retry_counter < MAX_QUERY_RETRIES
38
+ @retry_counter += 1
39
+ retry
40
+ end
41
+ end
42
+
43
+ raise
44
+ end
45
+
46
+ end
@@ -0,0 +1,27 @@
1
+ class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
2
+
3
+ MAX_QUERY_RETRIES = 1
4
+
5
+ alias_method :exec_query_without_retry, :exec_query
6
+
7
+ def exec_query(*args)
8
+ res = exec_query_without_retry(*args)
9
+ @retry_counter = nil
10
+ res
11
+ rescue ActiveRecord::StatementInvalid => ex
12
+ unless active?
13
+ reconnect! rescue nil
14
+
15
+ @retry_counter ||= 0
16
+ @logger.warn "Reconnecting to database after PGError! Try ##{@retry_counter + 1}/#{MAX_QUERY_RETRIES} #{ex.message}, trace: #{ex.backtrace.inspect}"
17
+
18
+ if @retry_counter < MAX_QUERY_RETRIES
19
+ @retry_counter += 1
20
+ retry
21
+ end
22
+ end
23
+
24
+ raise
25
+ end
26
+
27
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_record'
2
+ require 'active_record/connection_adapters/postgresql_adapter'
3
+
4
+ # Hackety monkey patch
5
+ if ActiveRecord::VERSION::STRING < '3'
6
+ require File.join(File.dirname(__FILE__), %w{ar23})
7
+ elsif ActiveRecord::VERSION::STRING >= '3'
8
+ require File.join(File.dirname(__FILE__), %w{ar3})
9
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), %w{pg_reconnect ar_extend})
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "pg_reconnect"
7
+ gem.version = "0.1"
8
+ gem.authors = ["'Konstantin Makarchev'"]
9
+ gem.email = ["'kostya27@gmail.com'"]
10
+ gem.description = %q{ActiveRecord PostgreSql auto-reconnection, works with 2.3 and 3.2 rails.}
11
+ gem.summary = %q{ActiveRecord PostgreSql auto-reconnection, works with 2.3 and 3.2 rails.}
12
+ gem.homepage = ""
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency 'activerecord', ">=2"
20
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_reconnect
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ! '''Konstantin Makarchev'''
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '2'
30
+ description: ActiveRecord PostgreSql auto-reconnection, works with 2.3 and 3.2 rails.
31
+ email:
32
+ - ! '''kostya27@gmail.com'''
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/pg_reconnect.rb
43
+ - lib/pg_reconnect/ar23.rb
44
+ - lib/pg_reconnect/ar3.rb
45
+ - lib/pg_reconnect/ar_extend.rb
46
+ - pg_reconnect.gemspec
47
+ homepage: ''
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.24
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: ActiveRecord PostgreSql auto-reconnection, works with 2.3 and 3.2 rails.
71
+ test_files: []
72
+ has_rdoc: