I am trying to write a function that tries to connect to Redis using the default TCP settings, and if that fails, it tries to connect to Redis via a unix socket. I intend to have one script connection that works on all my systems, some of which use TCP and others that use sockets.
However, I cannot get rid of the failed TCP connection. Here is my test script.
require "redis" def r begin $redis ||= Redis.new rescue $redis = Redis.new(:path => "/tmp/redis.sock") end end puts "You have #{r.keys.count} redis keys"
The rescue block never starts, and an exception is thrown instead. Here is the output of this script.
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:236:in `rescue in establish_connection ': Connection refused - Unable to connect to Redis on 127.0.0.1:6379 (Errno :: ECONNREFUSED)
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:222:in `establish_connection '
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:23:in `connect '
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:247:in `ensure_connected '
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:137:in `block in process'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:206:in `logging '
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:136:in `process'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis/client.rb:46:in `call '
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis.rb:246:in `block in keys'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize '
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/redis-2.2.2/lib/redis.rb:245:in `keys'
from scripts / redis.rb: 11: in `<main> '
I checked that Redis.new(:path => "/tmp/redis.sock") works as expected. I tried to be more specific with my rescue unit using rescue Errno::ECONNREFUSED no avail. I am not sure why I cannot catch this exception.
Any ideas?
Carl Zulauf
source share