Skip to content

Instantly share code, notes, and snippets.

@portertech
Created November 7, 2010 05:46
Show Gist options
  • Save portertech/665978 to your computer and use it in GitHub Desktop.
Save portertech/665978 to your computer and use it in GitHub Desktop.
tailable cursor (MongoDB)
#!/usr/bin/ruby -w
require 'rubygems'
require 'mongo'
require 'thread'
include Mongo
db = Connection.new('localhost',27017,:slave_ok => true).db('test')
coll = db['cappedCollection']
start_count = coll.count
insertion = Thread.new do
loop do
coll.save({:timestamp => Time.now.to_i})
sleep rand(0.5)
end
end
tail = Thread.new do
loop do
puts "Creating the cursor"
cursor = Cursor.new(coll, :tailable => true, :order => [['$natural', 1]]).skip(start_count - 1)
loop do
if not cursor.has_next?
puts "Cursor has nothing more!"
if cursor.closed?
puts "Cursor closed"
break
end
sleep 2
next
end
puts "#{Time.now} -> #{cursor.count} #{cursor.next_document['_id']}"
end
end
end
insertion.join
tail.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment