active_record_postgresql_retry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f79de3930cd1788519e1bf452cb4be1d504fa918
4
+ data.tar.gz: 9ced0fbd898596e837d02f2d0092beaa4d78e601
5
+ SHA512:
6
+ metadata.gz: 3ff6d5014c101a483588d7d391af24c8cf6036382883d1e993c3261d5700bfcd7539b26a97b521523e51dd10df7c3e761ea1eb59f2d5094f82379c44f2ca9431
7
+ data.tar.gz: c99c357c216ace4ca55045ca1a4ed9cee7e5e00f1cc7d1da0021b4453303568c0376fc34c156e0b415d20a4ef1a4ef246f7e72b85c87f911a237b5bd74022763
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Alexandru Szasz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # active_record_postgresql_retry
2
+ This gem provides a patch that once included will retry the PostgreSQL operation in case the server has gone away
3
+
4
+ 1. add it to your Gemfile
5
+ 2. declare the following environment variables
6
+ - PG_SLEEP_RETRY, default 0.33 seconds
7
+ - PG_QUERY_RETRY, default 0
8
+ - PG_RECONNECT_RETRY, default 0
9
+ - PG_CONNECT_RETRY, default 0
10
+
@@ -0,0 +1,80 @@
1
+ require 'active_record/connection_adapters/postgresql_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class PostgreSQLAdapter < AbstractAdapter
6
+ alias_method :old_execute, :execute
7
+ alias_method :old_reconnect!, :reconnect!
8
+ alias_method :old_connect, :connect
9
+
10
+ PG_SLEEP_RETRY = ENV['PG_SLEEP_RETRY'].to_f || 0.33
11
+ PG_QUERY_RETRY = ENV['PG_QUERY_RETRY'].to_i || 0
12
+ PG_RECONNECT_RETRY = ENV['PG_RECONNECT_RETRY'].to_i || 0
13
+ PG_CONNECT_RETRY = ENV['PG_CONNECT_RETRY'].to_i || 0
14
+
15
+ def execute(sql, name=nil)
16
+ begin
17
+ result = old_execute(sql, name)
18
+ @retry = nil
19
+ result
20
+ rescue PG::ConnectionBad, PG::UnableToSend => ex
21
+ @retry ||= 1
22
+ $log.warn "PostgreSQL statement invalid, retrying #{sql}: #{ex}"
23
+ sleep PG_SLEEP_RETRY
24
+ @retry += 1
25
+ raise if @retry > PG_QUERY_RETRY
26
+ end
27
+
28
+ if @retry.nil?
29
+ result
30
+ else
31
+ execute(sql, name)
32
+ end
33
+
34
+ end
35
+
36
+ def reconnect!
37
+ begin
38
+ result = old_reconnect!
39
+ @retry = nil
40
+ result
41
+ rescue PG::Error => ex
42
+ @retry ||= 1
43
+ $log.warn "PostgreSQL reconnect not possible, retry ##{@retry}: #{ex}"
44
+ sleep PG_SLEEP_RETRY
45
+ @retry += 1
46
+ raise if @retry > PG_RECONNECT_RETRY
47
+ end
48
+
49
+ if @retry.nil?
50
+ result
51
+ else
52
+ reconnect!
53
+ end
54
+
55
+ end
56
+
57
+ def connect
58
+
59
+ begin
60
+ result = old_connect
61
+ @retry = nil
62
+ result
63
+ rescue PG::Error => ex
64
+ @retry ||= 1
65
+ $log.warn "PostgreSQL connect not possible, retry ##{@retry}: #{ex}"
66
+ sleep PG_SLEEP_RETRY
67
+ @retry += 1
68
+ raise if @retry > PG_CONNECT_RETRY
69
+ end
70
+
71
+ if @retry.nil?
72
+ result
73
+ else
74
+ connect
75
+ end
76
+
77
+ end
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_postgresql_retry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexandru Szasz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-09 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This gem provides a patch that once included will retry the PostgreSQL
28
+ operation in case the server has gone away
29
+ email: alexxed@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/active_record_postgresql_retry.rb
37
+ homepage: https://github.com/alexxed/active_record_postgresql_retry
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.5.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: This gem provides a patch that once included will retry the PostgreSQL operation
61
+ in case the server has gone away
62
+ test_files: []