Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mperham/sidekiq
Browse files Browse the repository at this point in the history
  • Loading branch information
mperham committed Sep 11, 2015
2 parents e60386f + adb8e1e commit 50fc8ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Changes.md
@@ -1,7 +1,8 @@
Next
HEAD
-----------

- Add middleware stack to testing harness; see [wiki documentation](https://github.com/mperham/sidekiq/wiki/Testing#testing-server-middleware) [#2534, ryansch]
- Disconnect and retry Redis operations if we see a READONLY error [#2550]
- Add server middleware testing harness; see [wiki](https://github.com/mperham/sidekiq/wiki/Testing#testing-server-middleware) [#2534, ryansch]

3.5.0
-----------
Expand Down
16 changes: 13 additions & 3 deletions lib/sidekiq.rb
Expand Up @@ -76,9 +76,19 @@ def self.server?
defined?(Sidekiq::CLI)
end

def self.redis(&block)
raise ArgumentError, "requires a block" unless block
redis_pool.with(&block)
def self.redis
raise ArgumentError, "requires a block" unless block_given?
redis_pool.with do |conn|
retryable = true
begin
yield conn
rescue Redis::CommandError => ex
#2550 Failover can cause the server to become a slave, need
# to disconnect and reopen the socket to get back to the master.
(conn.disconnect!; retryable = false; retry) if retryable && ex.message =~ /READONLY/
raise
end
end
end

def self.redis_pool
Expand Down
20 changes: 20 additions & 0 deletions test/test_sidekiq.rb
Expand Up @@ -84,4 +84,24 @@ class TestSidekiq < Sidekiq::Test
assert_includes output, "ERROR"
end
end

describe 'redis connection' do
it 'does not continually retry' do
assert_raises Redis::CommandError do
Sidekiq.redis do |c|
raise Redis::CommandError, "READONLY You can't write against a read only slave."
end
end
end

it 'reconnects if connection is flagged as readonly' do
counts = []
Sidekiq.redis do |c|
counts << c.info['total_connections_received'].to_i
raise Redis::CommandError, "READONLY You can't write against a read only slave." if counts.size == 1
end
assert_equal 2, counts.size
assert_equal counts[0] + 1, counts[1]
end
end
end

0 comments on commit 50fc8ee

Please sign in to comment.