sidekiq-pg_helpers 0.1.0 → 0.2.0

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: d1eaac68cf5c30be590862792b0705d8ab2b28a1
4
- data.tar.gz: 7852938737b6d12159cd2525ab831400696bddd7
3
+ metadata.gz: 3e90d0e136330ed77dd238bc06b42e755be2a8d5
4
+ data.tar.gz: 5b91a433916c0452ad1d7163e4ec5173c5a9f5c8
5
5
  SHA512:
6
- metadata.gz: cf647b1a83fbf45cbb17bba3715516da88e0c36c8d8092199ba16601ef29ac222e3be89771c42b8a4d7260863e7961add49f1475e362b2bf16ad7e814af80cde
7
- data.tar.gz: 4dea9cf55cd9d47203fa2f3424883923bf108364ec15ac9dc909cddfbe0e2c42f874e40fed1ffd1f4f927f40b1b435178a3e80b596f8e2137c40c68cfb3a8070
6
+ metadata.gz: 456e0c81e83ef896f80d13dc0abeecc25f0bc06a322321b000b196d5c9fc267b5e083d710a5b7fd68fce10de47b480ee928dce599dec467c19c917475975716a
7
+ data.tar.gz: 8707c1869c2e43671e6debc6dee1410b230e131f055657c032b361b8d94beeaf633b461f18087cb54373c3831273e4d26bafc0700aa356f26569ed32a9f9897c
data/README.md CHANGED
@@ -1,4 +1,16 @@
1
- sidekiq-pg_helpers
2
- ==================
1
+ # sidekiq-pg_helpers
3
2
 
4
3
  Helper code for using Sidekiq with Postgres. Extracted from our production code.
4
+
5
+ ### ConnectionRecovery
6
+
7
+ This middleware helps Sidekiq bad Postgres connections on the fly, whenever they fail:
8
+ ```ruby
9
+ require "sidekiq/pg_helpers/middleware/connection_recovery"
10
+
11
+ Sidekiq.configure_server do |config|
12
+ config.server_middleware do |chain|
13
+ chain.insert_after Sidekiq::Middleware::Server::ActiveRecord, Sidekiq::PgHelpers::Middleware::ConnectionRecovery
14
+ end
15
+ end
16
+ ```
@@ -4,15 +4,29 @@ require "sidekiq"
4
4
  require "pg"
5
5
 
6
6
  module Sidekiq::PgHelpers::Middleware
7
+ # Helps keep ActiveRecord's connection pool healthy by detecting common Postgres errors
7
8
  class ConnectionRecovery
8
9
  def initialize
9
10
  @reconnection_attempts = 0
10
11
  end
11
12
 
12
- def call(*args)
13
+ # Detects commons problems with Postgres connections and forces ActiveRecord to close and re-open
14
+ # the offending connection, then automatically retries the error
15
+ def call(*)
13
16
  yield
14
- rescue PG::ConnectionBad, PG::UnableToSend => e
15
- if @reconnection_attempts >= 4
17
+ rescue *PG_CONNECTION_ERRORS => e
18
+ clean_up_connection(e)
19
+ retry
20
+ rescue ActiveRecord::StatementInvalid => e
21
+ raise unless PG_CONNECTION_ERRORS.include?(e.original_exception.class)
22
+ clean_up_connection(e)
23
+ retry
24
+ end
25
+
26
+ private
27
+
28
+ def clean_up_connection(e)
29
+ if reconnection_attempts >= 4
16
30
  Sidekiq.logger.error "Unable to re-establish Postgres connection after five attempts, giving up"
17
31
  raise
18
32
  end
@@ -28,8 +42,11 @@ module Sidekiq::PgHelpers::Middleware
28
42
  # Ensure we get a fresh connection the next time we access an activerecord object
29
43
  ActiveRecord::Base.clear_active_connections!
30
44
 
31
- @reconnection_attempts += 1
32
- retry
45
+ self.reconnection_attempts = reconnection_attempts + 1
33
46
  end
47
+
48
+ attr_accessor :reconnection_attempts
49
+
50
+ PG_CONNECTION_ERRORS = [PG::ConnectionBad, PG::UnableToSend].freeze
34
51
  end
35
52
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module PgHelpers
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -57,6 +57,13 @@ describe Sidekiq::PgHelpers::Middleware::ConnectionRecovery do
57
57
  it_behaves_like "helpful middleware"
58
58
  end
59
59
 
60
+ context "with ActiveRecord::StatementInvalid wrapping PG::ConnectionBad" do
61
+ let(:exception) do
62
+ ActiveRecord::StatementInvalid.new("ugh",PG::ConnectionBad.new)
63
+ end
64
+ it_behaves_like "helpful middleware"
65
+ end
66
+
60
67
  context "with an unrecoverable problem" do
61
68
  let(:exception) { PG::ConnectionBad.new("server closed the connection unexpectedly") }
62
69
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-pg_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Subelsky
@@ -14,98 +14,98 @@ dependencies:
14
14
  name: pg
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sidekiq
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.5'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.5'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: pry
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: ''
@@ -115,8 +115,8 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
- - .gitignore
119
- - .rspec
118
+ - ".gitignore"
119
+ - ".rspec"
120
120
  - Gemfile
121
121
  - LICENSE
122
122
  - LICENSE.txt
@@ -138,17 +138,17 @@ require_paths:
138
138
  - lib
139
139
  required_ruby_version: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - '>='
141
+ - - ">="
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  requirements:
146
- - - '>='
146
+ - - ">="
147
147
  - !ruby/object:Gem::Version
148
148
  version: '0'
149
149
  requirements: []
150
150
  rubyforge_project:
151
- rubygems_version: 2.2.2
151
+ rubygems_version: 2.4.2
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Helper code for using Sidekiq with Postgres. Extracted from our production
@@ -156,3 +156,4 @@ summary: Helper code for using Sidekiq with Postgres. Extracted from our product
156
156
  test_files:
157
157
  - spec/lib/middleware/connection_recovery_spec.rb
158
158
  - spec/spec_helper.rb
159
+ has_rdoc: