Guest User

Untitled

a guest
Jan 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.05 KB | None | 0 0
  1. #!/usr/bin/ruby                                                                                                                  
  2.  
  3. require 'rubygems'
  4. require 'mongo'
  5. require 'bson'
  6. require 'fileutils'
  7.  
  8. $cluster_conn = Mongo::ReplSetConnection.new(
  9.   ["ip-10-126-45-113", 27017],
  10.   ['localhost', 27017],
  11.   :slave_ok => true, :read => :secondary)
  12. $local_conn = Mongo::Connection.new('localhost')
  13.  
  14. def is_offline?
  15.   File.exists? 'offline'
  16. end
  17.  
  18. def get_conn
  19.   is_offline? ? $local_conn : $cluster_conn
  20. end
  21.  
  22. def get_db
  23.   db = is_offline? ? 'local' : 'test'
  24.   get_conn[db]
  25. end
  26.  
  27. puts " off line? " + is_offline?.to_s
  28. while true do
  29.   begin
  30.     get_db['foo'].find().each {|row|
  31.       puts row.inspect
  32.     }
  33.   rescue
  34.      puts "read failed: " + $!
  35.   end
  36.   if ARGV[0] == 'w' then
  37.     begin
  38.       get_db['foo'].update({"a" => "b"}, {"$set" => {"t" => Time.new.to_s}}, :upsert => true);
  39.     rescue
  40.       puts "write failed: " + $!
  41.       puts "switching to offline"
  42.       FileUtils.touch('offline')
  43.     end
  44.   end
  45.   puts Time.new
  46.   sleep 3;
  47. end
Add Comment
Please, Sign In to add comment