Example #1
0
    def is_healthy(self, ctx) -> bool:
        db_health = self.health_repository.ping(ctx)
        if not db_health:
            LOG.error("health_check_db_not_healthy", extra=log_extra(ctx))
            return False

        # TODO: add more health checks?

        LOG.info("health_check_success", extra=log_extra(ctx))
        return True
Example #2
0
    def wrapper(cls, ctx, *args, **kwds):
        """
        Wrap http_api function.
        """
        # TODO: Remove dep from SQLAlchemy?
        if ctx.get(CTX_SQL_SESSION):
            return f(cls, ctx, *args, **kwds)

        s = Db.get_db().get_session()
        ctx = build_context(session=s, ctx=ctx)

        try:
            result = f(cls, ctx, *args, **kwds)

            # It makes things clearer and less error-prone.
            assert isinstance(
                result,
                tuple), "Please always pass the result AND the HTTP code."
            assert len(
                result) > 1, "Please always pass the result AND the HTTP code."

            status_code = result[1]
            msg = result[0]
            if result[0] == NoContent:
                msg = None
            if status_code and 200 <= status_code <= 299:
                s.commit()
            else:
                LOG.info("rollback_sql_transaction_non_200_http_code",
                         extra=log_extra(ctx, code=status_code, message=msg))
                s.rollback()
            return result

        except Exception as e:
            LOG.error("rollback_sql_transaction_exception_caught",
                      extra=log_extra(ctx,
                                      exception=str(e),
                                      traceback=traceback.format_exc()))
            s.rollback()
            raise

        finally:
            # When running unit tests, we don't close the session so tests can actually perform some work on that
            # session.
            if not ctx.get(CTX_TESTING):
                s.close()