Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require 'rubygems'
- require 'mongo'
- require 'bson'
- require 'fileutils'
- $cluster_conn = Mongo::ReplSetConnection.new(
- ["ip-10-126-45-113", 27017],
- ['localhost', 27017],
- :slave_ok => true, :read => :secondary)
- $local_conn = Mongo::Connection.new('localhost')
- def is_offline?
- File.exists? 'offline'
- end
- def get_conn
- is_offline? ? $local_conn : $cluster_conn
- end
- def get_db
- db = is_offline? ? 'local' : 'test'
- get_conn[db]
- end
- puts " off line? " + is_offline?.to_s
- while true do
- begin
- get_db['foo'].find().each {|row|
- puts row.inspect
- }
- rescue
- puts "read failed: " + $!
- end
- if ARGV[0] == 'w' then
- begin
- get_db['foo'].update({"a" => "b"}, {"$set" => {"t" => Time.new.to_s}}, :upsert => true);
- rescue
- puts "write failed: " + $!
- puts "switching to offline"
- FileUtils.touch('offline')
- end
- end
- puts Time.new
- sleep 3;
- end
Add Comment
Please, Sign In to add comment