2011-08-12 4 views
1

Redis 라이브러리에서 발생한 Timeout::Error을 구해야합니다. 문제가 발생하여 특정 클래스가 작동하지 않는 문제를 해결하고 있습니다.Rescue Timeout :: Error from Redis Gem (Ruby)

begin 
    Redis.new({ :host => "127.0.0.X" }) 
rescue Timeout::Error => ex 
end 

=> Timeout::Error: Timeout::Error from /Users/me/.rvm/gems/[email protected]/gems/redis-2.2.0/lib/redis/connection/hiredis.rb:23:in `connect' 

내가 Exception를 구출하려고 할 때 수동으로 예외를 발생하려고하면 아직도 내가 그것을 구출 할 수

begin 
    Redis.new({ :host => "127.0.0.X" }) 
rescue Exception => ex 
end 

=> Timeout::Error: Timeout::Error from /Users/me/.rvm/gems/[email protected]/gems/redis-2.2.0/lib/redis/connection/hiredis.rb:23:in `connect' 

작동하지 않습니다하지만 난 구할 수없는 이유를 모른다 그것이 Redis 보석 (2.2.0) 내에서 호출되었을 때.

begin 
    raise Timeout::Error 
rescue Timeout::Error => ex 
    puts ex 
end 

Timeout::Error 
=> nil 

단서를 어떻게 구해야합니까?

답변

5

이 코드를 irb에서 실행 했습니까? 예외는 실제로 Redis.new에 의해 제기되지 않습니다. inspect 메서드에 의해 발생되며 irb는 방금 입력 한 식의 값을 표시합니다.

그냥 스택 트레이스를 보면 (나는 그것을 읽을 수 있도록 경로를 단축) : 위에서 볼 수 있듯이

ruby-1.8.7-p330 :009 > Redis.new(:host => "google.com") 
Timeout::Error: time's up! 
    from /.../SystemTimer-1.2.3/lib/system_timer/concurrent_timer_pool.rb:63:in `trigger_next_expired_timer_at' 
    from /.../SystemTimer-1.2.3/lib/system_timer/concurrent_timer_pool.rb:68:in `trigger_next_expired_timer' 
    from /.../SystemTimer-1.2.3/lib/system_timer.rb:85:in `install_ruby_sigalrm_handler' 
    from /..../lib/ruby/1.8/monitor.rb:242:in `synchronize' 
    from /.../SystemTimer-1.2.3/lib/system_timer.rb:83:in `install_ruby_sigalrm_handler' 
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `call' 
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `initialize' 
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `new' 
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `connect' 
    from /.../SystemTimer-1.2.3/lib/system_timer.rb:60:in `timeout_after' 
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:115:in `with_timeout' 
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:25:in `connect' 
    from /.../redis-2.2.2/lib/redis/client.rb:227:in `establish_connection' 
    from /.../redis-2.2.2/lib/redis/client.rb:23:in `connect' 
    from /.../redis-2.2.2/lib/redis/client.rb:247:in `ensure_connected' 
    from /.../redis-2.2.2/lib/redis/client.rb:137:in `process' 
... 2 levels... 
    from /.../redis-2.2.2/lib/redis/client.rb:46:in `call' 
    from /.../redis-2.2.2/lib/redis.rb:90:in `info' 
    from /..../lib/ruby/1.8/monitor.rb:242:in `synchronize' 
    from /.../redis-2.2.2/lib/redis.rb:89:in `info' 
    from /.../redis-2.2.2/lib/redis.rb:1075:in `inspect' 
    from /..../lib/ruby/1.8/monitor.rb:242:in `synchronize' 
    from /.../redis-2.2.2/lib/redis.rb:1074:in `inspect' 
    from /..../lib/ruby/1.8/irb.rb:310:in `output_value' 
    from /..../lib/ruby/1.8/irb.rb:159:in `eval_input' 
    from /..../lib/ruby/1.8/irb.rb:271:in `signal_status' 
    from /..../lib/ruby/1.8/irb.rb:155:in `eval_input' 
    from /..../lib/ruby/1.8/irb.rb:154:in `eval_input' 
    from /..../lib/ruby/1.8/irb.rb:71:in `start' 
    from /..../lib/ruby/1.8/irb.rb:70:in `catch' 
    from /..../lib/ruby/1.8/irb.rb:70:in `start' 
    from /..../bin/irb:17 

는, 예외가 내부 inspect하지 Redis.new 발생합니다. Redis 객체에서 inspect을 호출하면 상태를 인쇄하는 대신 실제로 많은 작업을 수행합니다. 이 경우 inspect은 서버에 연결을 시도하고 해당 시간이 초과되면 예외를 throw합니다. 이것은 나에게 매우 나쁜 디자인 인 것처럼 보이며 아마도 우리는 Redis 보석의 관리자에게 버그 보고서를 제출해야합니다.

이 IRB에 몇 가지 흥미로운 행동에 이르게 :

  • '=> "hello"'에 Redis.new(:host => "google.com"); 'hello' 결과를 입력하면 위의 그림과 같이 당신이 잡을하려면

    • 는 예외 Redis.new(:host => "google.com") 결과를 입력하면 예외가 발생하면 begin/rescue/end 블록 내에 ensure_connected을 호출 해보십시오.

    +0

    나의 경우 (위의 질문을 참조하십시오. 편집이 승인 된 경우)이 대답은 IRB의 '곤충'이 문제의 원인임을 알 수 있도록 도와줍니다. 감사합니다. Devid! –