Skip to content

Instantly share code, notes, and snippets.

@repeatedly
Created November 16, 2011 19:44
Show Gist options
  • Select an option

  • Save repeatedly/1371114 to your computer and use it in GitHub Desktop.

Select an option

Save repeatedly/1371114 to your computer and use it in GitHub Desktop.
Check reconnection in Mongo-Ruby-Driver
require 'mongo'
c = Mongo::Connection.new.db('fluent').collection('a')
i = 0
loop {
begin
c.insert({'a' => i})
i += 1
p "alive! #{i}"
sleep(1)
rescue
p "Down!"
sleep(2)
end
}
__END__
Result:
% ruby reconnection_test.rb
"alive! 1"
"alive! 2"
"alive! 3"
"alive! 4"
"alive! 5"
"alive! 6"
"alive! 7"
"alive! 8"
"Down!"
"Down!"
"Down!"
"Down!"
"alive! 9"
"alive! 10"
"alive! 11"
"alive! 12"
"alive! 13"
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment