2012-04-24 8 views
1

適切なRedis EM宝石(私の場合はem-hiredis)を使用してEventMachine原子炉ループ内でRedisセットを読み、一部のRedisセットにカスケードのメンバーが含まれているかどうかを確認する必要があります。私の目的は、空でない集合の名前を取得している:Ruby EventMachine&functions

require 'eventmachine' 
require 'em-hiredis' 

def fetch_queue 
    @redis.scard('todo').callback do |scard_todo| 
    if scard_todo.zero? 
     @redis.scard('failed_1').callback do |scard_failed_1| 
     if scard_failed_1.zero? 
      @redis.scard('failed_2').callback do |scard_failed_2| 
      if scard_failed_2.zero? 
       @redis.scard('failed_3').callback do |scard_failed_3| 
       if scard_failed_3.zero? 
        EM.stop 
       else 
        queue = 'failed_3' 
       end 
       end 
      else 
       queue = 'failed_2' 
      end 
      end 
     else 
      queue = 'failed_1' 
     end 
     end 
    else 
     queue = 'todo' 
    end 
    end 
end 

EM.run do 
    @redis = EM::Hiredis.connect "redis://#{HOST}:#{PORT}" 

    # How to get the value of fetch_queue? 
    foo = fetch_queue 
    puts foo 
end 

を私の質問は:私は原子炉のループでそれを使用する「fetch_queue」で「キュー」の値を返すためにEMを伝えることができる方法? fetch_queueの単純な "return queue = 'todo'"、 "return queue = 'failed_1'"などは、 "予期しない戻り値(LocalJumpError)"というエラーメッセージをもたらします。

答えて

9

もう少し多くの方法を使用してデバッグしてください、このような他のコードを考慮しないでください。

とにかく、これはあなたのコードを要因との両方をテストすることができますので、あなたはおそらく、何をしたいか、本質的である:

require 'eventmachine' 
require 'em-hiredis' 

# This is a simple class that represents an extremely simple, linear state 
# machine. It just walks the "from" parameter one by one, until it finds a 
# non-empty set by that name. When a non-empty set is found, the given callback 
# is called with the name of the set. 
class Finder 

    def initialize(redis, from, &callback) 
    @redis = redis 
    @from = from.dup 
    @callback = callback 
    end 

    def do_next 
    # If the from list is empty, we terminate, as we have no more steps 
    unless @current = @from.shift 
     EM.stop # or callback.call :error, whatever 
    end 

    @redis.scard(@current).callback do |scard| 
     if scard.zero? 
     do_next 
     else 
     @callback.call @current 
     end 
    end 
    end 

    alias go do_next 

end 

EM.run do 
    @redis = EM::Hiredis.connect "redis://#{HOST}:#{PORT}" 

    finder = Finder.new(redis, %w[todo failed_1 failed_2 failed_3]) do |name| 
    puts "Found non-empty set: #{name}" 
    end 

    finder.go 
end 
+0

うわー、何aswer!このようなクールで詳細なサンプルに感謝しています。 – ctp

関連する問題