-
-
Save siygle/814c4b4a06ab66297374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Written with pymongo-2.6 | |
# Modify for pymongo-2.1 | |
#!/usr/bin/env python | |
__author__ = 'mongolab' \ | |
import pymongo | |
import sys, datetime, time | |
import random | |
URI = "mongodb://testdbuser:[email protected]:53117,flop.mongolab.com:54117/testdb" | |
RETRY_WAIT_IN_SEC = 3 | |
############################################################################### \ | |
def main(args): | |
conn = pymongo.Connection(URI) | |
uri_parts = pymongo.uri_parser.parse_uri(URI) | |
db_name = uri_parts['database'] | |
db = conn[db_name] | |
collection_name = _get_unique_tmp_collection_name() | |
c = db[collection_name] | |
try: | |
while True: | |
now = datetime.datetime.now() | |
try: | |
if conn.host: | |
print ("%s: connected to primary server '%s'..." | |
% (now, conn.host + ":" + str(conn.port))) | |
print("...about to perform safe insert.") | |
c.insert({ "t" : now }) | |
print("...about to read.") | |
num_docs = c.find({}).count() | |
print ("...successfully inserted & read. There are %i " | |
"documents in your temporary test collection." | |
% num_docs) | |
retry_total_time = 0 | |
except Exception as e: | |
print ("*** EXCEPTION - %s" % e) | |
print ("*** ...will sleep for %s seconds before retrying " | |
"[total of %s seconds]" | |
% (RETRY_WAIT_IN_SEC, retry_total_time)) | |
retry_total_time += RETRY_WAIT_IN_SEC | |
print ("\n") | |
time.sleep(RETRY_WAIT_IN_SEC) | |
except: | |
print ("An unexpected exception occurred. Performing cleanup " | |
"if possible by dropping test collection '%s'..." | |
% collection_name) | |
c.drop() | |
############################################################################### \ | |
def _get_unique_tmp_collection_name(): | |
return "collection%s" % (random.randint(0, 99999999)) | |
############################################################################### \ | |
if __name__ == '__main__': | |
try: | |
main(sys.argv[1:]) | |
except SystemExit as e: | |
if e.code == 0: | |
pass | |
except Exception as e: | |
print ("*** EXCEPTION - %s" % str(e)) | |
print (" ... please try connecting again in 10 seconds, a " | |
"failover is probably in progress right now. If you still " | |
"can't connect, please email [email protected] for help. " | |
"Thank you!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment