Python django.db.utils.InterfaceError() Examples

The following are 6 code examples of django.db.utils.InterfaceError(). 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 django.db.utils , or try the search function .
Example #1
Source File: renderers.py    From prospector with GNU General Public License v3.0 6 votes vote down vote up
def render_path(self, *args, **kwargs):
        # Retry after an InterfaceError
        max_attempts = 5
        for i in range(max_attempts):
            try:
                return super().render_path(*args, **kwargs)
            except InterfaceError as e:
                self.logger.warning("Caught InterfaceError, closing connection "
                                    "and trying again (attempt #%s)",
                                    i, exc_info=True)
                try:
                    connection.close()
                except:
                    pass

        self.logger.error("Failed to render page after %s attempts. "
                          "Re-raising last exception...", max_attempts)
        raise e 
Example #2
Source File: signals.py    From Wooey with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def task_completed(sender=None, **kwargs):
    task_kwargs = kwargs.get('kwargs')
    job_id = task_kwargs.get('wooey_job')
    # Just return if it is not a wooey_job!
    if not job_id:
        return

    from .models import WooeyJob
    from celery import states
    try:
        job = WooeyJob.objects.get(pk=job_id)
    except (InterfaceError, DatabaseError) as e:
        db.connection.close()
        job = WooeyJob.objects.get(pk=job_id)
    state = kwargs.get('state')
    if state:
        job.status = WooeyJob.COMPLETED if state == states.SUCCESS else state
    job.celery_id = kwargs.get('task_id')
    job.save() 
Example #3
Source File: middleware.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def process_exception(self, request, exception):
        """Raise 424 on InterfaceError."""
        if isinstance(exception, InterfaceError):
            DB_CONNECTION_ERRORS_COUNTER.inc()
            LOG.error("KokuTenantMiddleware InterfaceError exception: %s", exception)
            return HttpResponseFailedDependency({"source": "Database", "exception": exception}) 
Example #4
Source File: base_thread.py    From scale with Apache License 2.0 5 votes vote down vote up
def run(self):
        """The main run loop of the thread
        """

        logger.info('%s thread started', self._name)

        while self._running:

            started = now()

            try:
                self._execute()
            except InterfaceError as err:
                logger.exception('%s thread had a critical error interfacing with the database', self._name)
                if err.message == 'connection already closed':
                    msg = '%s thread has detected that the database connection is closed and cannot be recovered.'
                    msg += ' Shutting down the scheduler...'
                    logger.error(msg, self._name)
                    from scheduler.management.commands.scale_scheduler import GLOBAL_SHUTDOWN
                    GLOBAL_SHUTDOWN()
            except Exception:
                logger.exception('%s thread had a critical error', self._name)

            duration = now() - started

            msg = '%s thread loop took %.3f seconds'
            if duration > self._warning_threshold:
                logger.warning(msg, self._name, duration.total_seconds())
            else:
                logger.debug(msg, self._name, duration.total_seconds())

            # If time takes less than threshold, throttle
            if duration < self._throttle:
                # Delay until full throttle time reached
                delay = math.ceil(self._throttle.total_seconds() - duration.total_seconds())
                time.sleep(delay)

        logger.info('%s thread stopped', self._name) 
Example #5
Source File: signals.py    From django-djangui with GNU General Public License v3.0 5 votes vote down vote up
def task_completed(sender=None, **kwargs):
    task_kwargs = kwargs.get('kwargs')
    job_id = task_kwargs.get('djangui_job')
    from .models import DjanguiJob
    from celery import states
    try:
        job = DjanguiJob.objects.get(pk=job_id)
    except (InterfaceError, DatabaseError) as e:
        db.connection.close()
        job = DjanguiJob.objects.get(pk=job_id)
    state = kwargs.get('state')
    if state:
        job.status = DjanguiJob.COMPLETED if state == states.SUCCESS else state
    job.celery_id = kwargs.get('task_id')
    job.save() 
Example #6
Source File: managers.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 4 votes vote down vote up
def transaction_retry(max_retries=1):
    """Decorator to retry database operations.

    For functions doing database operations, adding
    retrying if the operation fails.

    Keyword Arguments:
        max_retries (int): Maximum number of retries.  Default one retry.
    """

    def _outer(fun):

        @wraps(fun)
        def _inner(*args, **kwargs):
            _max_retries = kwargs.pop('exception_retry_count', max_retries)
            for retries in count(0):
                try:
                    return fun(*args, **kwargs)
                except SchedulingError as e:
                    log_task_failure(e, *args, **kwargs)
                    TaskUtils.prepare_task_execution()
                    if retries >= _max_retries:
                        raise
                except InterfaceError as e:
                    log_task_failure(e, *args, **kwargs)
                    TaskUtils.prepare_task_execution()
                    if retries >= _max_retries:
                        raise
                except OperationalError as e:
                    log_task_failure(e, *args, **kwargs)
                    TaskUtils.prepare_task_execution()
                    if retries >= _max_retries:
                        raise
                except Exception as e:  # pragma: no cover
                    # Depending on the database backend used we can experience
                    # various exceptions. E.g. psycopg2 raises an exception
                    # if some operation breaks the transaction, so saving
                    # the task result won't be possible until we rollback
                    # the transaction.
                    log_task_failure(e, *args, **kwargs)
                    if retries >= _max_retries:
                        raise

        return _inner

    return _outer