Skip to content

Instantly share code, notes, and snippets.

@lowang
Created October 31, 2014 15:24
Show Gist options
  • Save lowang/1e764aa491c54a9d9bda to your computer and use it in GitHub Desktop.
Save lowang/1e764aa491c54a9d9bda to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
class MongoClient
def initialize
@mongo_client = Mongo::MongoClient.new 'localhost',30999, pool_size: 3, pool_timeout:1, op_timeout:1, connect_timeout:1, read: :secondary
#:secondary_preferred
@mongo_database = @mongo_client['mongodb_outage_test']
end
def fetch_data
attempts = 1
begin
@mongo_database['data'].find(_id: rand(100_000)).first
rescue Mongo::OperationTimeout => e
@mongo_client.ping
attempts+=1
sleep 0.25
retry if attempts < 3
puts "Surrender on attempt #{attempts}"
raise
end
end
def run
while true
begin
puts "#{Time.now} #{fetch_data['_id']}"
rescue Interrupt
puts "quit!"
break
rescue StandardError => e
puts "#{Time.now} EXCEPTION: #{e}"
end
sleep 0.5
end
end
end
MongoClient.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment