Skip to content

Commit

Permalink
Added reconnect! to Flamingo to ensure close and reopen sockets and f…
Browse files Browse the repository at this point in the history
…ile descriptors after a fork. This is specifically intended to ensure we don't have issues with interleaved redis commands.
  • Loading branch information
Hayes Davis committed Nov 16, 2010
1 parent fe0652c commit 03de097
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/flamingo.rb
Expand Up @@ -99,7 +99,26 @@ def meta
@meta ||= Flamingo::Meta.new(redis)
end

# Intended to be called after a fork so that we don't have
# issues with shared file descriptors, sockets, etc
def reconnect!
reconnect_redis_client(@redis)
reconnect_redis_client(Resque.redis)
# Reload logger
logger.close
self.logger = new_logger
end

private
def reconnect_redis_client(client)
# Unfortunately older versions of the Redis client don't make these
# methods public so we have to use send. Later versions have made
# these public.
if client && (client.send(:connected?) rescue true)
client.send(:reconnect)
end
end

def root_dir
File.expand_path(File.dirname(__FILE__)+'/..')
end
Expand Down
33 changes: 33 additions & 0 deletions test/flamingo_test.rb
@@ -0,0 +1,33 @@
require File.join(File.dirname(__FILE__),"test_helper")

class FlamingoTest < Test::Unit::TestCase


include FlamingoTestCase

def setup
setup_flamingo
end

def test_reconnect_reconnects_redis_clients_when_connected
Flamingo.redis.expects(:connected?).returns(true)
Flamingo.redis.expects(:reconnect)
Resque.redis.expects(:connected?).returns(true)
Resque.redis.expects(:reconnect)
Flamingo.reconnect!
end

def test_reconnect_does_not_reconnect_redis_clients_when_not_connected
Flamingo.redis.expects(:connected?).returns(false)
Flamingo.redis.expects(:reconnect).never
Resque.redis.expects(:connected?).returns(false)
Resque.redis.expects(:reconnect).never
Flamingo.reconnect!
end

def teardown
teardown_flamingo
end


end

0 comments on commit 03de097

Please sign in to comment.