2011-09-28 14 views
5

Próbuję utworzyć obserwatora oplog w ruby. Jak na razie wymyślę mały skrypt poniżej.ruchomy kursor w mongo db timing out

require 'rubygems' 
require 'mongo' 
db = Mongo::Connection.new("localhost", 5151).db("local") 
coll = db.collection('oplog.$main') 

loop do 
cursor = Mongo::Cursor.new(coll, :tailable => true) 
    while not cursor.closed? 
     if doc = cursor.next_document 
      puts doc 
     else 
      sleep 1 
     end 
    end 
end 

Problem z tym jest, po 5 lub 6 sekund, kiedy to wypluć dużo danych to limit czasu i pojawia się błąd

C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/connection.rb 
:807:in `check_response_flags': Query response returned CURSOR_NOT_FOUND. Either an invalid c 
ursor was specified, or the cursor may have timed out on the server. (Mongo::OperationFailure 
) 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
connection.rb:800:in `receive_response_header' 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
connection.rb:768:in `receive' 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
connection.rb:493:in `receive_message' 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
connection.rb:491:in `synchronize' 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
connection.rb:491:in `receive_message' 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
cursor.rb:494:in `send_get_more' 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
cursor.rb:456:in `refresh' 
     from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/ 
cursor.rb:124:in `next_document' 
     from n.rb:7 
     from n.rb:6:in `loop' 
     from n.rb:6 

Co ja nie rozumiem, kiedy jestem w stanie aby zobaczyć rzeczywiste dane, jak może nagle stwierdzić, że kursor nie został znaleziony. Jestem całkiem nowy w rubinach i wszelkie pomysły na temat tego, w którym kierunku muszę iść, będą dla mnie użyteczne.

Odpowiedz

5

Rozwiązaniem jest to, że muszę mieć mechanizm obsługi wyjątków, aby wychwycić wyjątek, który jest generowany, kiedy kursor odczytuje ostatni dokument w relatywnie małym oploku z większą liczbą zapisów na sekundę. Ponieważ kursor dociera do końca oploga, wyrzuciłby wyjątek, że nie ma już rekordów.

require 'rubygems' 
require 'mongo' 
db = Mongo::Connection.new("localhost",5151).db("local") 
coll = db.collection('oplog.$main') 
loop do 
cursor = Mongo::Cursor.new(coll, :timeout => false, :tailable => true) 
    while not cursor.closed? 
    begin 
     if doc = cursor.next_document 
      puts "Timestamp" 
      puts doc["ts"] 
      puts "Record" 
      puts doc["o"] 
      puts "Affected Collection" 
      puts doc["ns"] 
     end 
    rescue 
     puts "" 
     break 
    end 
    end 
end 

Działa to teraz, ponieważ został potraktowany wyjątek. Dzięki grupie google mongodb-user za wskazanie mi tego.