Пример #1
0
def db_monitor(uri, func, sleep=5, max_retries=None):
    """
    Check status of MongoDB connection. Invoke provided function upon
    successfull connection.
    """
    conn = None
    retries = 0
    while True:
        # db is dead
        if not (conn and is_db_alive(uri)):
            try:
                conn = db_connection(uri)
            except Exception as exc:
                print exc

        if conn and is_db_alive(uri):
            print "### established connection %s" % conn
            func()

        # limit the number of retries if needed
        retries += 1
        if max_retries is not None and retries > max_retries:
            break

        time.sleep(sleep)
Пример #2
0
 def init(self):
     "Initialize connection to MongoDB"
     conn = db_connection(self.dburi)
     if  conn:
         database = conn[self.dbname]
         if  self.dbcoll not in database.collection_names():
             database.create_collection(self.dbcoll, \
                         capped=True, size=self.dbsize)
         self.col = database[self.dbcoll]                
     if  not is_db_alive(self.dburi):
         self.col = None
         return
Пример #3
0
 def init(self):
     """
     Init db connection and check that it is alive
     """
     try:
         conn = db_connection(self.dburi)
         self.col = conn[self.dbname][self.dbcoll]
         indexes = [('dataset', ASCENDING), ('ts', ASCENDING)]
         create_indexes(self.col, indexes)
         self.col.remove()
     except Exception as _exp:
         self.col = None
     if  not is_db_alive(self.dburi):
         self.col = None
Пример #4
0
def db_monitor(uri, func, sleep=5):
    """
    Check status of MongoDB connection. Invoke provided function upon 
    successfull connection.
    """
    conn = db_connection(uri)
    while True:
        if  not conn or not is_db_alive(uri):
            try:
                conn = db_connection(uri)
                func()
                print "\n### re-established connection %s" % conn
            except:
                pass
        time.sleep(sleep)
Пример #5
0
def db_monitor(uri, func, sleep, reload_map, reload_time, check_maps, reload_time_bad_maps):
    """
    Check status of MongoDB connection and reload DAS maps once in a while.
    """
    time0 = time.time()
    valid_maps = False
    try:
        valid_maps = check_maps()
    except Exception as err:
        print_exc(err)
    while True:
        conn = db_connection(uri)
        if not conn or not is_db_alive(uri):
            try:
                conn = db_connection(uri, verbose=False)
                func()
                if conn:
                    print "### db_monitor re-established connection %s" % conn
                    valid_maps = check_maps()
                else:
                    print "### db_monitor, lost connection"
            except Exception as err:
                print_exc(err)
        if conn:
            # reload invalid more quickly
            reload_intervl = reload_time if valid_maps else reload_time_bad_maps
            if time.time() - time0 > reload_intervl:
                map_state = "INVALID" if not valid_maps else ""
                msg = "reload %s DAS maps %s" % (map_state, reload_map)
                print dastimestamp(), msg
                try:
                    reload_map()
                    valid_maps = check_maps()
                except Exception as err:
                    print_exc(err)
                time0 = time.time()

        time.sleep(sleep)