2011-09-28 15 views
5

में उपलब्ध कर्सर रूबी में एक ओप्लॉग वॉचर बनाने की कोशिश कर रहा हूं। अभी तक ive नीचे एक छोटी लिपि के साथ आते हैं।मोंगो डीबी टाइमिंग

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 

मिलता है जब im सक्षम वास्तविक डेटा देखने के लिए अचानक यह कैसे कह सकता है कर्सर नहीं मिला। मैं रूबी के लिए बहुत नया हूं और मुझे लगता है कि मुझे किस दिशा में लेना चाहिए, मेरे लिए उपयोगी होगा।

उत्तर

5

समाधान यह है कि मुझे अपवाद को पकड़ने के लिए एक अपवाद हैंडलिंग तंत्र की आवश्यकता होती है जिसे कर्सर प्रति सेकंड लिखने की उच्च संख्या वाले अपेक्षाकृत छोटे ओप्लोग में अंतिम दस्तावेज़ पढ़ता है। चूंकि कर्सर ओप्लॉग के अंत तक पहुंचता है, इसलिए यह एक अपवाद फेंक देगा कि कोई और रिकॉर्ड नहीं है।

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 समूह के लिए धन्यवाद।

संबंधित मुद्दे