2011-09-28 3 views
5

ruby에서 oplog watcher를 만들려고합니다. 지금까지 아래의 작은 스크립트를 작성하십시오.mongo db의 시간 제한에서 커서를 사용할 수 없음

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 

이의 문제는, 5 ~ 6 초 후가 시간이 초과 많은 데이터를 뱉어 한 때와 내가 이해 해달라고 어떤 오류

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 

를 얻을 수는있을 때 할 수 메신저 실제 데이터를보고 갑자기 커서를 찾을 수 없다는 것을 어떻게 알 수 있습니까? 나는 루비에 대해 꽤 새로운 사람이고 어떤 방향으로 나아갈 지에 대한 아이디어는 나에게 유용 할 것이다.

답변

5

해결책은 커서가 비교적 작은 oplog의 마지막 문서를 초당 더 많은 수의 쓰기로 읽을 때 throw되는 예외를 캡처하는 예외 처리 메커니즘이 필요하다는 것입니다. 커서가 oplog의 끝에 도달하기 때문에 더 이상 레코드가 없다는 예외가 발생합니다.

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 

이제 예외가 처리되었으므로 작동합니다. Mongodb-user google 그룹에게 감사의 말을 전합니다.

관련 문제