2011-09-28 43 views
5

Tôi đang cố tạo một trình theo dõi oplog bằng ruby. Cho đến nay, ive đã đưa ra một kịch bản nhỏ bên dưới.con trỏ có sẵn trong thời gian mongo db ra

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 

Vấn đề ở đây là, sau 5 hoặc 6 giây khi nó đã nhổ ra rất nhiều dữ liệu mà nó lần ra và tôi nhận được một lỗi

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 

gì tôi không hiểu là khi im thể để xem dữ liệu thực tế như thế nào nó đột nhiên có thể nói con trỏ không tìm thấy. Im khá mới để ruby ​​và bất kỳ ý tưởng về những gì tôi phải hướng sẽ có ích cho tôi.

Trả lời

5

Giải pháp là tôi cần phải có một cơ chế xử lý ngoại lệ để nắm bắt ngoại lệ được ném khi con trỏ đọc tài liệu cuối cùng trong một oplog tương đối nhỏ với số lượng ghi cao hơn mỗi giây. Kể từ khi con trỏ đến cuối của oplog nó sẽ ném một ngoại lệ mà không có hồ sơ nhiều hơn nữa.

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 

Điều này hiện đang hoạt động khi ngoại lệ được xử lý. Nhờ nhóm người dùng mongodb-người dùng google đã chỉ ra điều này cho tôi.

Các vấn đề liên quan