sidekiq-pg_helpers 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: 5d2ed31b9d675454a85214492b86ba39c0f1611e
4
+ data.tar.gz: 5f90b834f777c70a8ee6eb6496d5d82b9f6a4ab7
5
+ SHA512:
6
+ metadata.gz: 1fe4ad87cf9adff11778f7735a2eecadd84fc09148dec9b4f688fe31b78647cba34cd1467db5aefca875ac3f7f0f34b8114f9eecb9f702db28950a24e292349a
7
+ data.tar.gz: f87aceb8d9d592cbe7b262fd63dec9ce44859678a85639a18276a4f2ce0f295a14b1fc3bdc95a8e5d4395773110782c3fdcfbbb384863f9759c23d42fa7280dd
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ Gemfile.lock
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq-pg_helpers.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Staq
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mike Subelsky
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,4 @@
1
+ sidekiq-pg_helpers
2
+ ==================
3
+
4
+ Helper code for using Sidekiq with Postgres. Extracted from our production code.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ #http://erniemiller.org/2014/02/05/7-lines-every-gems-rakefile-should-have/
4
+ task :console do
5
+ require "irb"
6
+ require "irb/completion"
7
+ require "sidekiq/pg_helpers"
8
+ ARGV.clear
9
+ IRB.start
10
+ end
@@ -0,0 +1,35 @@
1
+ require "sidekiq/pg_helpers"
2
+ require "active_record"
3
+ require "sidekiq"
4
+ require "pg"
5
+
6
+ module Sidekiq::PgHelpers
7
+ class ConnectionRecovery
8
+ def initialize
9
+ @reconnection_attempts = 0
10
+ end
11
+
12
+ def call(worker_class,msg,queue)
13
+ yield
14
+ rescue PG::ConnectionBad, PG::UnableToSend => e
15
+ if @reconnection_attempts >= 4
16
+ Sidekiq.logger.error "Unable to re-establish Postgres connection after five attempts, giving up"
17
+ raise
18
+ end
19
+
20
+ Sidekiq.logger.warn "Received #{e.class}, disconnecting and cleaning up our Postgres connection before re-trying"
21
+
22
+ # Probably due to an abrupt disconnection: https://devcenter.heroku.com/articles/postgres-logs-errors#pgerror-ssl-syscall-error-eof-detected
23
+ # The next time we access an ActiveRecord connection, it should automatically check out a fresh connection to replace this one
24
+ connection = ActiveRecord::Base.connection
25
+ connection.disconnect!
26
+ ActiveRecord::Base.connection_pool.remove(connection)
27
+
28
+ # Ensure we get a fresh connection the next time we access an activerecord object
29
+ ActiveRecord::Base.clear_active_connections!
30
+
31
+ @reconnection_attempts += 1
32
+ retry
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module PgHelpers
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require "sidekiq/pg_helpers/version"
2
+
3
+ module Sidekiq
4
+ module PgHelpers
5
+ end
6
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/pg_helpers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-pg_helpers"
8
+ spec.version = Sidekiq::PgHelpers::VERSION
9
+ spec.authors = ["Mike Subelsky"]
10
+ spec.email = ["github@mikeshop.net"]
11
+ spec.summary = %q{Helper code for using Sidekiq with Postgres. Extracted from our production code.}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/staqapp/sidekiq-pg_helpers"
14
+ spec.license = "MIT"
15
+ spec.date = "2014-02-21"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "pg"
23
+ spec.add_runtime_dependency "activerecord"
24
+ spec.add_runtime_dependency "sidekiq"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.5"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "pry"
30
+ end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+ require "sidekiq/pg_helpers/middleware/connection_recovery"
3
+
4
+ shared_examples "helpful middleware" do
5
+ before do
6
+ subject.call(:worker_class,:msg,:queue,&block)
7
+ end
8
+
9
+ it "logs the issue" do
10
+ expect(logger).to have_received(:warn).with(an_instance_of(String))
11
+ end
12
+
13
+ it "disconnects the bad connection" do
14
+ expect(connection).to have_received(:disconnect!)
15
+ end
16
+
17
+ it "removes the bad connection from ActiveRecord's connection pool" do
18
+ expect(connection_pool).to have_received(:remove).with(connection)
19
+ end
20
+
21
+ it "clears active connections, just in case" do
22
+ expect(ActiveRecord::Base).to have_received(:clear_active_connections!)
23
+ end
24
+ end
25
+
26
+ describe Sidekiq::PgHelpers::ConnectionRecovery do
27
+ let(:connection) { double(:connection,disconnect!: nil) }
28
+ let(:connection_pool) { double(:connection_pool,remove: nil) }
29
+ let(:logger) { double(:logger,warn: nil,error: nil) }
30
+ let(:block) do
31
+ lambda do
32
+ next if @_times_called == 1
33
+ @_times_called += 1
34
+ raise exception
35
+ end
36
+ end
37
+
38
+ before do
39
+ @_times_called = 0
40
+
41
+ ActiveRecord::Base.stub(connection: connection,connection_pool: connection_pool)
42
+ ActiveRecord::Base.stub(:clear_active_connections!)
43
+ Sidekiq.stub(logger: logger)
44
+ end
45
+
46
+ it "yields to a block" do
47
+ expect { |b| subject.call(:worker_class,:msg,:queue,&b) }.to yield_control
48
+ end
49
+
50
+ context "with PG::ConnectionBad" do
51
+ let(:exception) { PG::ConnectionBad.new("could not connect to server: Connection refused") }
52
+ it_behaves_like "helpful middleware"
53
+ end
54
+
55
+ context "with PG::UnableToSend" do
56
+ let(:exception) { PG::ConnectionBad.new("server closed the connection unexpectedly") }
57
+ it_behaves_like "helpful middleware"
58
+ end
59
+
60
+ context "with an unrecoverable problem" do
61
+ let(:exception) { PG::ConnectionBad.new("server closed the connection unexpectedly") }
62
+
63
+ let(:block) do
64
+ lambda do
65
+ @_times_called += 1
66
+ next if @_times_called > 5 # prevent looping forever if the retry code breaks
67
+ raise exception
68
+ end
69
+ end
70
+
71
+ before do
72
+ @_times_called = 0
73
+ end
74
+
75
+ it "gives up after 5 attempts" do
76
+ subject.call(:worker_class,:msg,:queue,&block) rescue PG::ConnectionBad
77
+ expect(@_times_called).to eq(5)
78
+ end
79
+
80
+ it "re-raises the exception after 5 attempts" do
81
+ expect { subject.call(:worker_class,:msg,:queue,&block) }.to raise_error(exception)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,16 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+
4
+ Bundler.setup(:default,:test)
5
+ require "rspec"
6
+ require "pp"
7
+ require "pry"
8
+
9
+ $:.unshift("#{__dir__}/../lib/staq_rendering")
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+ config.order = "random"
16
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-pg_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Subelsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg
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
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: ''
112
+ email:
113
+ - github@mikeshop.net
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - Gemfile
121
+ - LICENSE
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/sidekiq/pg_helpers.rb
126
+ - lib/sidekiq/pg_helpers/middleware/connection_recovery.rb
127
+ - lib/sidekiq/pg_helpers/version.rb
128
+ - sidekiq-pg_helpers.gemspec
129
+ - spec/lib/middleware/connection_recovery_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/staqapp/sidekiq-pg_helpers
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.2.2
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Helper code for using Sidekiq with Postgres. Extracted from our production
155
+ code.
156
+ test_files:
157
+ - spec/lib/middleware/connection_recovery_spec.rb
158
+ - spec/spec_helper.rb
159
+ has_rdoc: