activerecord_postgresql_retry 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +9 -0
- data/lib/activerecord_postgresql_retry.rb +64 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0d50bbbd0c1517f923bb91585645912c3208c2f4
|
4
|
+
data.tar.gz: f16c0ff3169aba4d039bef9b32c1c98335459adf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4eb8de3a754203bf69860d44d7ebe935832214a52291edc941031673253fd3de60c9acf3959191649e868c98ebbfbcb5009649b220b1bf716bc7ac9b4845fd67
|
7
|
+
data.tar.gz: 3959e8cfb2f35d0ba189418eeeb82d12c1bdda1c0accb9929788966889150e7300869a41bfe6a93dc86162426557befe0b02dfef739acd70b30ff0e12599a679
|
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,9 @@
|
|
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. require 'activerecord_postgresql_retry' (after requiring activerecord)
|
6
|
+
3. declare the following environment variables
|
7
|
+
- PG_SLEEP_RETRY, default 0.33 seconds
|
8
|
+
- PG_QUERY_RETRY, default 0
|
9
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
class PostgreSQLAdapter < AbstractAdapter
|
6
|
+
alias_method :old_exec_cache, :exec_cache
|
7
|
+
alias_method :old_exec_no_cache, :exec_no_cache
|
8
|
+
alias_method :old_execute, :execute
|
9
|
+
alias_method :old_reconnect!, :reconnect!
|
10
|
+
alias_method :old_connect, :connect
|
11
|
+
alias_method :old_transaction, :transaction
|
12
|
+
|
13
|
+
PG_SLEEP_RETRY = ENV['PG_SLEEP_RETRY'].to_f || 0.33
|
14
|
+
PG_QUERY_RETRY = ENV['PG_QUERY_RETRY'].to_i || 0
|
15
|
+
|
16
|
+
def exec_no_cache(sql, name, binds)
|
17
|
+
with_retry { old_exec_no_cache(sql, name, binds) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def exec_cache(sql, name, binds)
|
21
|
+
with_retry { old_exec_cache(sql, name, binds) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def execute(sql, name = nil)
|
25
|
+
with_retry { old_execute(sql, name) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def reconnect!
|
29
|
+
with_retry { old_reconnect! }
|
30
|
+
end
|
31
|
+
|
32
|
+
def connect
|
33
|
+
with_retry { old_connect }
|
34
|
+
end
|
35
|
+
|
36
|
+
def transaction(options = {}, &block)
|
37
|
+
with_retry { old_transaction(options = {}, &block) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def with_retry
|
41
|
+
retry_count = 0
|
42
|
+
|
43
|
+
begin
|
44
|
+
yield
|
45
|
+
rescue PG::ConnectionBad, PG::UnableToSend, ActiveRecord::StatementInvalid => ex
|
46
|
+
raise if open_transactions != 0
|
47
|
+
raise if retry_count >= PG_QUERY_RETRY
|
48
|
+
raise if ex.is_a?(ActiveRecord::StatementInvalid) unless (
|
49
|
+
ex.original_exception.is_a?(PG::ConnectionBad) ||
|
50
|
+
ex.original_exception.is_a?(PG::UnableToSend)
|
51
|
+
)
|
52
|
+
|
53
|
+
sleep PG_SLEEP_RETRY
|
54
|
+
retry_count += 1
|
55
|
+
$log.warn "PostgreSQL retry ##{retry_count} '#{defined?(sql) ? sql : ''}' because #{ex}"
|
56
|
+
reconnect!
|
57
|
+
retry
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_postgresql_retry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
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/activerecord_postgresql_retry.rb
|
37
|
+
homepage: https://github.com/alexxed/activerecord_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.4.8
|
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: []
|