lib/redis_failover/client.rb in nogara-redis_failover-0.9.1 vs lib/redis_failover/client.rb in nogara-redis_failover-0.9.7
- old
+ new
@@ -62,14 +62,22 @@
parse_options(options)
setup_zk
build_clients
end
-
- # Resque wants to use Resque.redis.reconnect to recreate all connections. So
- # we provide ourselves as a client to receive the "reconnect" message
- def client
+ # Stubs this method to return this RedisFailover::Client object.
+ #
+ # Some libraries (Resque) assume they can access the `client` via this method,
+ # but we don't want to actually ever expose the internal Redis connections.
+ #
+ # By returning `self` here, we can add stubs for functionality like #reconnect,
+ # and everything will Just Work.
+ #
+ # Takes an *args array for safety only.
+ #
+ # @return [RedisFailover::Client]
+ def client(*args)
self
end
# Sidekiq-web asks for a location in the redis client. Implements something here
# just to make sure it works
@@ -188,10 +196,12 @@
purge_clients
@zk.stat(@znode, :watch => true)
else
logger.error("Unknown ZK node event: #{event.inspect}")
end
+ ensure
+ @zk.stat(@znode, :watch => true)
end
# Determines if a method is a known redis operation.
#
# @param [Symbol] method the method to check
@@ -248,11 +258,11 @@
# @note If there are no slaves, the master is returned.
# @return [Redis] the Redis client for the slave or master
# @raise [NoMasterError] if no master fallback is available
def slave
# pick a slave, if none available fallback to master
- if slave = @lock.synchronize { @slaves.sample }
+ if slave = @lock.synchronize { @slaves.shuffle.first }
verify_role!(slave, :slave)
return slave
end
master
end
@@ -264,11 +274,11 @@
begin
nodes = fetch_nodes
return unless nodes_changed?(nodes)
purge_clients
- logger.info("Building new clients for nodes #{nodes}")
+ logger.info("Building new clients for nodes #{nodes.inspect}")
new_master = new_clients_for(nodes[:master]).first if nodes[:master]
new_slaves = new_clients_for(*nodes[:slaves])
@master = new_master
@slaves = new_slaves
rescue
@@ -298,10 +308,10 @@
#
# @return [Hash] the known master/slave redis servers
def fetch_nodes
data = @zk.get(@znode, :watch => true).first
nodes = symbolize_keys(decode(data))
- logger.debug("Fetched nodes: #{nodes}")
+ logger.debug("Fetched nodes: #{nodes.inspect}")
nodes
rescue Zookeeper::Exceptions::InheritedConnectionError => ex
logger.debug { "Caught #{ex.class} '#{ex.message}' - reopening ZK client" }
@zk.reopen