Python pymongo.errors.PyMongoError() Examples

The following are 14 code examples of pymongo.errors.PyMongoError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pymongo.errors , or try the search function .
Example #1
Source File: zabbix-mongodb.py    From zabbix-mongodb with MIT License 6 votes vote down vote up
def connect(self):
        """Connect to MongoDB"""
        if self.__conn is None:
            if self.mongo_user is None:
                try:
                    self.__conn = MongoClient('mongodb://%s:%s' %
                                              (self.mongo_host,
                                               self.mongo_port))
                except errors.PyMongoError as py_mongo_error:
                    print('Error in MongoDB connection: %s' %
                          str(py_mongo_error))
            else:
                try:
                    self.__conn = MongoClient('mongodb://%s:%s@%s:%s' %
                                              (self.mongo_user,
                                               self.mongo_password,
                                               self.mongo_host,
                                               self.mongo_port))
                except errors.PyMongoError as py_mongo_error:
                    print('Error in MongoDB connection: %s' %
                          str(py_mongo_error)) 
Example #2
Source File: mongodb.py    From s3recon with MIT License 6 votes vote down vote up
def with_retry(tries):
    def outer_wrapper(f):
        @wraps(f)
        def inner_wrapper(*args, **kwargs):
            def _retry(t=tries):
                if t <= 0:
                    logger.error("unable to write hit to database")
                    # raise WriteError(f"unable to write to database")
                    return
                try:
                    f(*args, **kwargs)
                except PyMongoError:
                    t -= 1
                    _retry(t)

            return _retry()

        return inner_wrapper

    return outer_wrapper 
Example #3
Source File: database.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def deprovision_mongo(self, ignore_errors=False):
        """
        Drop Mongo databases.
        """
        self.logger.info('Deprovisioning Mongo started.')
        database_url = self._get_main_database_url()
        if database_url and self.mongo_provisioned:
            mongo = pymongo.MongoClient(database_url)
            for database in self.mongo_database_names:
                # Dropping a non-existing database is a no-op.  Users are dropped together with the DB.
                self.logger.info('Dropping mongo db: %s.', database)
                try:
                    mongo.drop_database(database)
                except PyMongoError as exc:
                    self.logger.exception('Cannot drop Mongo database: %s. %s', database, exc)
                    if not ignore_errors:
                        raise
            self.mongo_provisioned = False
            self.save()
        self.logger.info('Deprovisioning Mongo finished.') 
Example #4
Source File: db_interface_backend.py    From FACT_core with GNU General Public License v3.0 6 votes vote down vote up
def update_analysis_tags(self, uid, plugin_name, tag_name, tag):
        firmware_object = self.get_object(uid=uid, analysis_filter=[])
        try:
            tags = update_tags(firmware_object.analysis_tags, plugin_name, tag_name, tag)
        except ValueError as value_error:
            logging.error('Plugin {} tried setting a bad tag {}: {}'.format(plugin_name, tag_name, str(value_error)))
            return None
        except AttributeError:
            logging.error('Firmware not in database yet: {}'.format(uid))
            return None

        if isinstance(firmware_object, Firmware):
            try:
                self.firmwares.update_one({'_id': uid}, {'$set': {'analysis_tags': tags}})
            except (TypeError, ValueError, PyMongoError) as exception:
                logging.error('Could not update firmware: {} - {}'.format(type(exception), str(exception)))
        else:
            logging.warning('Propagating tag only allowed for firmware. Given: {}') 
Example #5
Source File: test_auth.py    From arctic with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_authenticate_fails_exception():
    db = create_autospec(Database)
    db.authenticate.side_effect = PyMongoError("error")
    with pytest.raises(PyMongoError):
        assert auth.authenticate(db, sentinel.user, sentinel.password) is False 
Example #6
Source File: zabbix-mongodb.py    From zabbix-mongodb with MIT License 5 votes vote down vote up
def get_maintenance(self):
        """get replica set maintenance info"""
        if self.__conn is None:
            self.connect()
        db_handler = self.__conn

        fsync_locked = int(db_handler.is_locked)
        self.add_metrics('mongodb.fsync-locked', fsync_locked)

        try:
            config = db_handler.admin.command("replSetGetConfig", 1)
            connstring = (self.mongo_host + ':' + str(self.mongo_port))
            connstrings = list()

            for i in range(0, len(config['config']['members'])):
                host = config['config']['members'][i]['host']
                connstrings.append(host)

                if connstring in host:
                    priority = config['config']['members'][i]['priority']
                    hidden = int(config['config']['members'][i]['hidden'])

            self.add_metrics('mongodb.priority', priority)
            self.add_metrics('mongodb.hidden', hidden)
        except errors.PyMongoError:
            print ('Error while fetching replica set configuration.'
                   'Not a member of replica set?')
        except UnboundLocalError:
            print ('Cannot use this mongo host: must be one of ' + ','.join(connstrings))
            exit(1) 
Example #7
Source File: permission.py    From aswan with GNU Lesser General Public License v2.1 5 votes vote down vote up
def mongodb_error_log(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except PyMongoError as err:
            tb_content = ' '.join(traceback.format_exception(*sys.exc_info()))
            msg = (
                '\nOperate Mongodb error! \n Func: {}, args: {}, kwargs: {} '
                '\n Error: {} \n {}'
            ).format(func.__name__, args, kwargs, err, tb_content)
            logger.error(msg)
            raise DBError(msg)
        except InvalidId as err:
            logger.error('Invalid BSON ObjectId: {}'.format(err))
    return wrapper 
Example #8
Source File: testtools.py    From flocker with Apache License 2.0 5 votes vote down vote up
def get_mongo_client(host, port=27017):
    """
    Returns a ``Deferred`` which fires with a ``MongoClient`` when one has been
    created.

    See http://api.mongodb.org/python/current/api/pymongo/mongo_client.html#
        pymongo.mongo_client.MongoClient
    for more parameter information.

    :param bytes host: Hostname or IP address of the instance to connect to.
    :param int port: Port number on which to connect.

    The tutorial says "If you get a connection refused error try again after a
    few seconds; the application might take some time to fully start up."
    and so here we wait until the client can be created.
    """
    def create_mongo_client():
        try:
            client = MongoClient(host=host, port=port)
            client.areyoualive.posts.insert({"ping": 1})
            return client
        except PyMongoError:
            return False

    d = loop_until(reactor, create_mongo_client)
    return d 
Example #9
Source File: mongolog.py    From baleen with MIT License 5 votes vote down vote up
def connect(self):
        """
        Connect to the Mongo database.
        """
        try:
            self.connection = MongoClient(host=self.host, port=self.port)
        except PyMongoError:
            if self.fail_silently:
                return
            else:
                raise

        self.database   = self.connection[self.database_name]
        self.collection = self.database[self.collection_name] 
Example #10
Source File: rest_file_object.py    From FACT_core with GNU General Public License v3.0 5 votes vote down vote up
def _get_without_uid(self):
        try:
            query = get_query(request.args)
            offset, limit = get_paging(request.args)
        except ValueError as value_error:
            request_data = {k: request.args.get(k) for k in ['query', 'limit', 'offset']}
            return error_message(str(value_error), self.URL, request_data=request_data)

        parameters = dict(offset=offset, limit=limit, query=query)
        try:
            with ConnectTo(FrontEndDbInterface, self.config) as connection:
                uids = connection.rest_get_file_object_uids(**parameters)
            return success_message(dict(uids=uids), self.URL, parameters)
        except PyMongoError:
            return error_message('Unknown exception on request', self.URL, parameters) 
Example #11
Source File: rest_firmware.py    From FACT_core with GNU General Public License v3.0 5 votes vote down vote up
def _get_without_uid(self):
        try:
            query, recursive, inverted, offset, limit = self._get_parameters_from_request(request.args)
        except ValueError as value_error:
            request_data = {k: request.args.get(k) for k in ['query', 'limit', 'offset', 'recursive', 'inverted']}
            return error_message(str(value_error), self.URL, request_data=request_data)

        parameters = dict(offset=offset, limit=limit, query=query, recursive=recursive, inverted=inverted)
        try:
            with ConnectTo(FrontEndDbInterface, self.config) as connection:
                uids = connection.rest_get_firmware_uids(**parameters)
            return success_message(dict(uids=uids), self.URL, parameters)
        except PyMongoError:
            return error_message('Unknown exception on request', self.URL, parameters) 
Example #12
Source File: db_interface_compare.py    From FACT_core with GNU General Public License v3.0 5 votes vote down vote up
def add_compare_result(self, compare_result):
        compare_result['_id'] = self._calculate_compare_result_id(compare_result)
        compare_result['submission_date'] = time()
        with suppress(PyMongoError):
            self.compare_results.delete_one({'_id': compare_result['_id']})
        self.compare_results.insert_one(compare_result)
        logging.info('compare result added to db: {}'.format(compare_result['_id'])) 
Example #13
Source File: MongoMgr.py    From FACT_core with GNU General Public License v3.0 5 votes vote down vote up
def init_users(self):
        logging.info('Creating users for MongoDB authentication')
        if self.auth_is_enabled():
            logging.error("The DB seems to be running with authentication. Try terminating the MongoDB process.")
        mongo_server = self.config['data_storage']['mongo_server']
        mongo_port = self.config['data_storage']['mongo_port']
        try:
            client = MongoClient('mongodb://{}:{}'.format(mongo_server, mongo_port), connect=False)
            client.admin.command(
                "createUser",
                self.config['data_storage']['db_admin_user'],
                pwd=self.config['data_storage']['db_admin_pw'],
                roles=[
                    {'role': 'dbOwner', 'db': 'admin'},
                    {'role': 'readWriteAnyDatabase', 'db': 'admin'},
                    {'role': 'root', 'db': "admin"}
                ]
            )
            client.admin.command(
                "createUser",
                self.config['data_storage']['db_readonly_user'],
                pwd=self.config['data_storage']['db_readonly_pw'],
                roles=[{'role': 'readAnyDatabase', 'db': 'admin'}]
            )
        except (AttributeError, ValueError, errors.PyMongoError) as error:
            logging.error('Could not create users:\n{}'.format(error)) 
Example #14
Source File: base_consumer.py    From distributed_framework with Apache License 2.0 4 votes vote down vote up
def _run_consuming_function_with_confirm_and_retry(self, kw: dict, current_retry_times,
                                                       function_result_status: FunctionResultStatus, ):
        function_only_params = _delete_keys_and_return_new_dict(kw['body'])
        if current_retry_times < self.__get_priority_conf(kw, 'max_retry_times'):
            function_result_status.run_times += 1
            # noinspection PyBroadException
            t_start = time.time()
            try:
                function_run = self.consuming_function if self._function_timeout == 0 else self._concurrent_mode_dispatcher.timeout_deco(
                    self.__get_priority_conf(kw, 'function_timeout'))(self.consuming_function)
                if self._is_consuming_function_use_multi_params:  # 消费函数使用传统的多参数形式
                    function_result_status.result = function_run(**function_only_params)
                else:
                    function_result_status.result = function_run(
                        function_only_params)  # 消费函数使用单个参数,参数自身是一个字典,由键值对表示各个参数。
                function_result_status.success = True
                self._confirm_consume(kw)
                if self.__get_priority_conf(kw, 'do_task_filtering'):
                    self._redis_filter.add_a_value(function_only_params)  # 函数执行成功后,添加函数的参数排序后的键值对字符串到set中。
                self.logger.debug(f' 函数 {self.consuming_function.__name__}  '
                                  f'第{current_retry_times + 1}次 运行, 正确了,函数运行时间是 {round(time.time() - t_start, 4)} 秒,入参是 【 {function_only_params} 】。  {ConsumersManager.get_concurrent_info()}')
            except Exception as e:
                if isinstance(e, (PyMongoError,
                                  ExceptionForRequeue)):  # mongo经常维护备份时候插入不了或挂了,或者自己主动抛出一个ExceptionForRequeue类型的错误会重新入队,不受指定重试次数逇约束。
                    self.logger.critical(f'函数 [{self.consuming_function.__name__}] 中发生错误 {type(e)}  {e},消息重新入队')
                    time.sleep(1)  # 防止快速无限出错入队出队,导致cpu和中间件忙
                    return self._requeue(kw)
                self.logger.error(f'函数 {self.consuming_function.__name__}  第{current_retry_times + 1}次发生错误,'
                                  f'函数运行时间是 {round(time.time() - t_start, 4)} 秒,\n  入参是 【 {function_only_params} 】   \n 原因是 {type(e)} {e} ',
                                  exc_info=self.__get_priority_conf(kw, 'is_print_detail_exception'))
                function_result_status.exception = f'{e.__class__.__name__}    {str(e)}'
                return self._run_consuming_function_with_confirm_and_retry(kw, current_retry_times + 1, function_result_status, )
        else:
            self.logger.critical(
                f'函数 {self.consuming_function.__name__} 达到最大重试次数 {self.__get_priority_conf(kw, "max_retry_times")} 后,仍然失败, 入参是 【 {function_only_params} 】')
            self._confirm_consume(kw)  # 错得超过指定的次数了,就确认消费了。
        if self.__get_priority_conf(kw, 'is_using_rpc_mode'):
            # print(function_result_status.get_status_dict(without_datetime_obj=True))
            with RedisMixin().redis_db_frame.pipeline() as p:
                # RedisMixin().redis_db_frame.lpush(kw['body']['extra']['task_id'], json.dumps(function_result_status.get_status_dict(without_datetime_obj=True)))
                # RedisMixin().redis_db_frame.expire(kw['body']['extra']['task_id'], 600)
                p.lpush(kw['body']['extra']['task_id'],
                        json.dumps(function_result_status.get_status_dict(without_datetime_obj=True)))
                p.expire(kw['body']['extra']['task_id'], 600)
                p.execute()
        self._result_persistence_helper.save_function_result_to_mongo(function_result_status)