Skip to content

Instantly share code, notes, and snippets.

@lowang
Created November 3, 2014 10:23
Show Gist options
  • Save lowang/5fc24c6e40b03a613d2b to your computer and use it in GitHub Desktop.
Save lowang/5fc24c6e40b03a613d2b to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'mongo'
gem 'bson_ext'
GEM
remote: https://rubygems.org/
specs:
bson (1.11.1)
bson (1.11.1-java)
bson_ext (1.11.1)
bson (~> 1.11.1)
mongo (1.11.1)
bson (= 1.11.1)
PLATFORMS
java
ruby
DEPENDENCIES
bson_ext
mongo
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_preferred
#: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, Mongo::OperationFailure => 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.class}: #{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