Exemple #1
0
    def __connect(self):
        mongo_url = self.__get_mongo_url(self.settings_dict)
        logger.debug("Connecting to '%s'..." % self.displayed_mongo_url)
        conn_cnt = 0
        while conn_cnt < settings.DB_CONN_MAX_TIMES:
            try:
                # More details about `MongoClient` API, see:
                # https://api.mongodb.com/python/2.8/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient
                client = MongoClient(mongo_url)
                # the constructor returns immediately and launches the
                # connection process on background threads.
                # Checks if the server is available like this:
                client.admin.command('ismaster')
                logger.debug("Connection to MongoDB is established.")
                return client
            # If auto-reconnection will be performed, AutoReconnect will be raised.
            # Application code should handle this exception (recognizing that the
            # operation failed) and then continue to execute.
            except errors.AutoReconnect as e:
                conn_cnt += 1
                msg = ("Connection to MongoDB is lost and an attempt to "
                       "auto-connect will be made ...")
                stdout.warn(msg)
                logger.warning(msg)

        logger.error("Unable to establish the connection to MongoDB.")
        raise ImproperlyConfigured(
            "Unable to establish the connection to MongoDB.")
Exemple #2
0
    def get_connection(self, settings_dict):
        if settings_dict.get('HOST') and settings_dict.get('PORT'):
            stdout.debug(
                "Trying to connect to {} servered on '{}:{}'...".format(
                    self.db_name, settings_dict['HOST'],
                    settings_dict['PORT']))

        conn_cnt = 0
        while conn_cnt < settings.DB_CONN_MAX_TIMES:
            try:
                conn = self._connect(settings_dict)
                stdout.success("Connection to {} established.".format(
                    self.db_name))
                return conn
            except ConnectionTimeout as e:
                conn_cnt += 1
                interval = random.randint(0, settings.DB_CONN_MAX_INTERVAL)
                stdout.warn(
                    "Connection timeout to {}: {}\nWill retry to connect in {} \
                    seconds.".format(self.db_name, str(e), interval))
                time.sleep(interval)
            except KeyError as e:
                # May raise when resolving the settings dict
                stdout.error("Fields missing, check '{}' was set.".format(
                    str(e)))
                raise ImproperlyConfigured("Fields missing: {}".format(str(e)))

        stdout.error("Unable to establish connection to '{}'".format(
            self.db_name))
        raise ImproperlyConfigured()
Exemple #3
0
 def _connect(self, settings_dict):
     try:
         conn = mysqldb.connect(host=settings_dict['HOST'],
                                port=settings_dict['PORT'],
                                user=settings_dict['USER'],
                                passwd=settings_dict['PASSWORD'],
                                db=settings_dict['DATABASE'],
                                charset=settings_dict['CHARSET'],
                                connect_timeout=settings.DB_CONN_TIMEOUT)
     except (mysqldb.InternalError, mysqldb.OperationalError) as e:
         stdout.warn(str(e))
         raise ConnectionTimeout
     return conn
Exemple #4
0
    def _connect(self, settings_dict):
        try:
            conn = _mssql.connect(server=settings_dict['HOST'],
                                  port=settings_dict['PORT'],
                                  user=settings_dict['USER'],
                                  password=settings_dict['PASSWORD'],
                                  database=settings_dict['DATABASE'],
                                  charset=settings_dict['CHARSET'])
            conn.query_timeout = settings.DB_CONN_TIMEOUT
        # _mssql is not written based on the PEP-249, `exceptions` on _mssql see:
        # http://pymssql.org/en/stable/ref/_mssql.html#module-level-exceptions
        except _mssql.MssqlDatabaseException as e:
            stdout.warn(str(e))
            raise ConnectionTimeout

        return conn
Exemple #5
0
 def _connect(self, settings_dict):
     try:
         conn = pymssql.connect(
             host=settings_dict['HOST'],
             port=settings_dict['PORT'],
             user=settings_dict['USER'],
             password=settings_dict['PASSWORD'],
             database=settings_dict['DATABASE'],
             charset=settings_dict['CHARSET'],
             timeout=settings.DB_CONN_TIMEOUT,
         )
     # More details on error of python database api, see ref:
     # https://www.python.org/dev/peps/pep-0249/
     except (pymssql.InternalError, pymssql.OperationalError) as e:
         stdout.warn(str(e))
         raise ConnectionTimeout
     return conn