示例#1
0
    def handle(self, *args, **kwargs):
        """Wait till the database is ready."""
        self.stdout.write("Waiting for database...")

        connected = False

        while not connected:

            time.sleep(5)

            try:
                connection.ensure_connection()

                connected = True

            except OperationalError as e:
                self.stdout.write(f"Could not connect to database: {e}")
            except ImproperlyConfigured as e:
                self.stdout.write(f"Improperly configured: {e}")
            else:
                if not connection.is_usable():
                    self.stdout.write("Database configuration is not usable")

            if connected:
                self.stdout.write("Database connection sucessful!")
示例#2
0
 def _check(self):
     try:
         self.check()
         if settings.DATABASES:
             # once a connection has been established, this will be successful
             # (even if the connection is gone later on)
             connection.ensure_connection()
     except SystemCheckError as e:
         if settings.DEBUG:
             self.write("django check error: " + str(e))
         else:
             self.write("check error")
         self.set_status(500)
     except OperationalError as e:
         if settings.DEBUG:
             self.write("django database error: " + str(e))
         else:
             self.write("db error")
         self.set_status(500)
     else:
         if response_average_time := ResponseTimeAverageMetric.get():
             self.write(
                 f"Average response time: {response_average_time:.2f}ms Request "
                 f"queue size: {RequestQueueLengthMetric.get()} Rx")
         else:
示例#3
0
 def get(self, request):
     is_healthy = True
     # verify if blacklisted environment vars is not available
     blacklisted_envs = ENV_BLACKLIST.get(settings.LEVEL, [])
     # we loop through the blacklisted vars for the current env and
     # if there is anything, we unset the healthy flag.
     for env in blacklisted_envs:
         env_value = os.environ.get(env, None)
         if env_value:
             is_healthy = False
             break
     # verify if RDS is available
     try:
         db_connection.ensure_connection()
     except OperationalError:
         is_healthy = False
         logger.exception(
             'Could not get DB connection from instance {0} while doing health check'.format(
                 settings.AWS_INSTANCE_ID
             ))
     # verify if we can write to S3 bucket
     try:
         assert default_storage.connection is not None
         if self._should_verify_s3_write_access():
             if not self._has_s3_write_access():
                 is_healthy = False
     except Exception as e:
         is_healthy = False
         logger.exception(
             'Could not get S3 connection while doing health check; instance: {0}'.format(
                 settings.AWS_INSTANCE_ID
             ))
     if is_healthy:
         return response.Response(status=status.HTTP_200_OK)
     return response.Response(status=status.HTTP_400_BAD_REQUEST)
示例#4
0
def _build_mbox(query, params, msgid=None):
    connection.ensure_connection()

    # Rawmsg is not in the django model, so we have to query it separately
    curs = connection.connection.cursor(name='mbox', withhold=True)
    curs.itersize = 50
    curs.execute(query, params)

    firstmsg = curs.fetchone()
    if msgid and firstmsg[0] != msgid:
        # Always redirect to the first message in the thread when building
        # the mbox, to not generate potentially multiple copies in
        # the cache.
        return HttpResponsePermanentRedirect(firstmsg[0])

    def _one_message(raw):
        # Parse as a message to generate headers
        s = BytesIO(raw)
        parser = email.parser.BytesParser(policy=email.policy.compat32)
        msg = parser.parse(s)
        return msg.as_string(unixfrom=True)

    def _message_stream(first):
        yield _one_message(first[1])

        for mid, raw in curs:
            yield _one_message(raw)

        # Close must be done inside this function. If we close it in the
        # main function, it won't let the iterator run to completion.
        curs.close()

    r = StreamingHttpResponse(_message_stream(firstmsg))
    r['Content-type'] = 'application/mbox'
    return r
def write_read_orm():
    """
    Write a ResponseLog object to the database to see if it is up.

    Returns:
         bool: True if we can read and write using the ORM.
    """
    random_roundtrip = randint(1, 1000)
    random_available = random() > 0.5

    try:
        connection.ensure_connection()
    except:
        raven_client.captureException()
        return False

    try:
        response_log = ResponseLog.objects.create(
            platform=GCM_PLATFORM,
            roundtrip_time=random_roundtrip,
            available=random_available,
        )
    except DatabaseError:
        connection.close()

        raven_client.captureException()
        return False
    else:
        if response_log.available == random_available and response_log.roundtrip_time == random_roundtrip:
            response_log.delete()
            return True
        return False
示例#6
0
def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')

    n = 1  # attempt number
    h = 60  # last attempt
    b = 2  # growth factor
    s = 1  # time in seconds to wait if the connection to the database fails in attempt n
    sys.stdout.write('Waiting for database...\n')
    while n <= h:
        try:
            connection.ensure_connection()
            sys.stdout.write('Database available!\n\n')
            sys.stdout.flush()
            file = sys.argv[1]
            args = sys.argv[1:]  # first argument is the program name
            os.execvp(
                file,
                args)  # execute the command, replacing the current process
        except OperationalError:
            sys.stdout.write(
                f' [{n}/{h}] Database unavailable, waiting for {s} seconds ...\n'
            )
            sys.stdout.flush()
            time.sleep(s)
            n = n + 1  # increase attempt number
            s = s * b  # calculate wait time for next attempt
    sys.exit(1)
    def _check(self):
        if StartupTimeMetric.get():
            got_exception = None
            try:
                self.check()
                if settings.DATABASES:
                    # once a connection has been established, this will be successful
                    # (even if the connection is gone later on)
                    connection.ensure_connection()
            except SystemCheckError as e:
                got_exception = traceback.format_exc()
                if settings.DEBUG:
                    self.write("django check error: " + str(e))
                else:
                    self.write("check error")
            except OperationalError as e:
                got_exception = traceback.format_exc()
                if settings.DEBUG:
                    self.write("django database error: " + str(e))
                else:
                    self.write("db error")

            else:
                if response_average_time := ResponseTimeAverageMetric.get():
                    self.write(
                        f"Average response time: {response_average_time:.2f}ms Request "
                        f"queue size: {RequestQueueLengthMetric.get()} Rx"
                    )
                else:
                    self.write("alive")
                if HealthMetric.get() is not True:
                    HealthMetric.set(True)
                    if self.liveness_webhook:
                        logger.info("Health metric changed to True. Liveness webhook with status succeeded triggered")
                        LivenessWebhook().run(url=self.liveness_webhook, status=WebhookStatus.SUCCEEDED)
def get_live_learners():
    """Get learners that are active based on Kolibri idle session timeout
    Args:
        None
    Returns:
        A list of user_ids of the learners currently logged in
    """
    # get kolibri idle session timeout as an integer
    sess_timeout = int(os.environ["KOLIBRI_SESSION_TIMEOUT"])
    # initalize cut-off time using kolibri idle session timeout to decide which learners qualify as live learners
    live_learners_cutoff = timezone.now() - timedelta(seconds=sess_timeout)
    try:
        # ensure that there is a conenction to the database
        connection.ensure_connection()

        # live learners are learners that have been active in the last 10 minutes
        # get all usersessionlogs
        # where the last interaction timestamp is greater or equal to current time minus kolibri idle seesion timeout
        live_sessions = (
            UserSessionLog.objects.filter(
                last_interaction_timestamp__gte=live_learners_cutoff
            )
            .values("user_id")
            .distinct()
        )

        # get array of user_ids of live learners
        live_learners = [user.get("user_id") for user in live_sessions]

    except OperationalError:
        # catch operational errors and inform the user
        print("Database unavailable, not able to retrieve users and sessions info")

    # return an array of user_ids of live learners
    return live_learners
示例#9
0
def check_database_connected(app_configs, **kwargs):
    """
    A Django check to see if connecting to the configured default
    database backend succeeds.
    """
    errors = []

    try:
        connection.ensure_connection()
    except OperationalError as e:
        msg = "Could not connect to database: {!s}".format(e)
        errors.append(
            checks.Error(msg, id=health.ERROR_CANNOT_CONNECT_DATABASE))
    except ImproperlyConfigured as e:
        msg = 'Datbase misconfigured: "{!s}"'.format(e)
        errors.append(checks.Error(msg,
                                   id=health.ERROR_MISCONFIGURED_DATABASE))
    else:
        if not connection.is_usable():
            errors.append(
                checks.Error(
                    "Database connection is not usable",
                    id=health.ERROR_UNUSABLE_DATABASE,
                ))

    return errors
示例#10
0
def get_db_info():
    """
    Returns information about the sessions and users the current
    Kolibri server has in use

    """
    # Users information
    active_sessions = "unknown"
    active_users = active_users_minute = None
    try:
        connection.ensure_connection()
        # Sessions active in the last 10 minutes (includes guest accesses):
        active_sessions = str(
            Session.objects.filter(expire_date__gte=timezone.now()).count())
        last_ten_minutes = timezone.now() - timedelta(minutes=10)
        last_minute = timezone.now() - timedelta(minutes=1)
        # Active logged users:
        active_users = str(
            UserSessionLog.objects.filter(
                last_interaction_timestamp__gte=last_ten_minutes).count())
        # Logged users with activity in the last minute:
        active_users_minute = str(
            UserSessionLog.objects.filter(
                last_interaction_timestamp__gte=last_minute).count())
    except OperationalError:
        print(
            "Database unavailable, impossible to retrieve users and sessions info"
        )

    return (active_sessions, active_users, active_users_minute)
    def get(self, request):
        try:
            connection.ensure_connection()
        except OperationalError as err:
            return JsonResponse({"message": str(err)}, status=503)

        return JsonResponse({"message": "ok"})
示例#12
0
def connected():
    """Context manager that ensures we're connected to the database.

    If there is not yet a connection to the database, this will connect on
    entry and disconnect on exit. Preexisting connections will be left alone.

    If the preexisting connection is not usable it is closed and a new
    connection is made.
    """
    if connection.connection is None:
        connection.close_if_unusable_or_obsolete()
        connection.ensure_connection()
        try:
            yield
        finally:
            connection.close()
    elif connection.is_usable():
        yield
    else:
        # Connection is not usable, so we disconnect and reconnect. Since
        # the connection was previously connected we do not disconnect this
        # new connection.
        connection.close_if_unusable_or_obsolete()
        connection.ensure_connection()
        yield
示例#13
0
    def handle(self, *args, **options):
        wait = options["seconds"]
        max_retries = options["max_retries"]
        num_loops = 1
        connection_made = False
        while True:
            self.stdout.write(
                self.style.SUCCESS(
                    f"({num_loops}/{max_retries}) Checking database connection..."
                ))
            try:
                connection.ensure_connection()
                connection_made = True
                break
            except OperationalError:
                plural_time = ngettext("second", "seconds", wait)
                self.stdout.write(
                    self.style.WARNING(
                        f"Database unavailable, retrying after {wait} {plural_time}!"
                    ))
                time.sleep(wait)
            if num_loops >= max_retries:
                self.stdout.write(
                    self.style.WARNING(
                        f"Maximum attempts reached: {max_retries}"))
                break
            num_loops += 1

        if connection_made:
            self.stdout.write(
                self.style.SUCCESS("Database connections successful"))
        else:
            self.stdout.write(
                self.style.WARNING("Failed to make Database connection"))
示例#14
0
    def run(self, result, debug=False):
        """
        overrides TestSuite's run() method, with few changes,
        except that this method swaps in the template db before
        each test is run!
        """
        if self.test_db_name is None or self.template_db_name is None:
            err_msg = 'test_db_name & template_db_name must be set before run() can be called!'
            raise self.SetupException(err_msg)

        topLevel = False
        if getattr(result, '_testRunEntered', False) is False:
            result._testRunEntered = topLevel = True

        for test in self:
            if result.shouldStop:
                break

            #
            # previous to the test execution,
            # copy our template test db into the test db
            # and then instantiate the test
            # TODO - db connection.close()
            PgUtil.clone_db(self.template_db_name, self.test_db_name,
                            self.requires_sudo)
            # TODO - settings.DATABASES[self.connection.alias]["NAME"] = self.test_db_name
            # TODO - self.connection.settings_dict["NAME"] = self.test_db_name
            # TODO - not sure if we HAVE to do this, but try it with it and without it
            connection.ensure_connection()
            countdown = 5
            while countdown >= 0:
                time.sleep(0.5)
                SSHELP.check_score_systems()
                countdown -= 0.5

            if _isnotsuite(test):
                self._tearDownPreviousClass(test, result)
                self._handleModuleFixture(test, result)
                self._handleClassSetUp(test, result)
                result._previousTestClass = test.__class__

                if (getattr(test.__class__, '_classSetupFailed', False)
                        or getattr(result, '_moduleSetUpFailed', False)):
                    continue

            if not debug:
                #
                # now make the test
                test(result)
            else:
                test.debug()

        if topLevel:
            self._tearDownPreviousClass(None, result)
            self._handleModuleTearDown(result)
            result._testRunEntered = False
        return result
示例#15
0
 def handle(self, *args, **options):
     self.stdout.write('Waiting for database...')
     db_conn = None
     while not db_conn:
         try:
             connection.ensure_connection()
             db_conn = True
         except OperationalError:
             time.sleep(1)
     self.stdout.write(self.style.SUCCESS('Database is available!'))
示例#16
0
def _extract_broker_objects(id_list):

    connection.ensure_connection()
    with connection.connection.cursor(cursor_factory=DictCursor) as cursor:
        sql = "SELECT {} from source_procurement_transaction where detached_award_procurement_id in %s".format(
            ",".join(all_broker_columns()))
        cursor.execute(sql, (tuple(id_list), ))

        results = cursor.fetchall()

    return results
示例#17
0
def adapt(text):
    connection.ensure_connection()
    a = psycopg2.extensions.adapt(force_text(text))
    c = connection.connection

    # This is a workaround for https://github.com/18F/calc/issues/1498.
    if hasattr(c, '__wrapped__'):
        c = getattr(c, '__wrapped__')

    a.prepare(c)
    return a
示例#18
0
def database_connected(app_configs, **kwargs):
    errors = []

    try:
        connection.ensure_connection()
    except OperationalError:
        errors.append(Error('Could not connect to database', id=ERROR_CANNOT_CONNECT_DATABASE))
    else:
        if not connection.is_usable():
            errors.append(Error('Database connection is not usable', id=ERROR_UNUSABLE_DATABASE))

    return errors
示例#19
0
 def handle(self, *args, **options):
     """Handle the command"""
     self.stdout.write("Waiting for database...")
     db_conn = None
     while not db_conn:
         try:
             connection.ensure_connection()
             db_conn = True
         except OperationalError:
             self.stdout.write("Database unavailable, waiting 1 second...")
             time.sleep(1)
     self.stdout.write(self.style.SUCCESS("Database available!"))
示例#20
0
    def handle(self, *args, **options):
        self.stdout.write("Waiting for database...")
        t = time.time()
        db_conn = None
        while not db_conn:
            try:
                connection.ensure_connection()
                db_conn = True
            except OperationalError:
                time.sleep(0.25)

        self.stdout.write(self.style.SUCCESS(f"Database available after {time.time()-t:.1f}s!"))
示例#21
0
def handler(event, context):
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "regcore.settings")
    import django
    django.setup()

    from django.db import connection
    connection.ensure_connection()
    if not connection.is_usable():
        raise Exception("database is unreachable")

    from django.core.management import call_command
    call_command('migrate')
    def handle(self, *args, **options):
        self.stdout.write('Waiting for database...')

        for _ in range(20):
            try:
                connection.ensure_connection()
                break
            except OperationalError:
                self.stdout.write('Database unavailable, waiting 5 second...')
                time.sleep(5)

        self.stdout.write(self.style.SUCCESS('Database available!'))
示例#23
0
 def handle(self, *args, **options):
     print("Checking the connection to the database.")
     database_connected = False
     while not database_connected:
         try:
             connection.ensure_connection()
             database_connected = True
         except OperationalError:
             print(self.style.ERROR("Database still unavailable."), end="")
             print(" Waiting 1 second.")
             time.sleep(1)
     print(self.style.SUCCESS("Database available!"))
示例#24
0
    def _generate_database_status(self):
        """Generates the database status message

        :return: JSON describing the database status
        :rtype: dict
        """
        try:
            connection.ensure_connection()
            status_dict = {'OK': True, 'detail': {'msg': 'Database alive and well'}, 'errors': [], 'warnings': []}
        except Exception as ex:
            status_dict = {'OK': False, 'detail': {'msg': 'Unable to connect to database'}, 'errors': [{'OPERATIONAL_ERROR': 'Database unavailable.'}], 'warnings': []}
        
        return status_dict
示例#25
0
    def handle(self, *args, **options):
        """Entrypoint for command."""
        self.stdout.write('Waiting for database...')
        db_conn = None
        while not db_conn:
            try:
                connection.ensure_connection()
                db_conn = True
            except (Psycopg2OpError, OperationalError):
                self.stdout.write('Database unavailable, waiting 1 second...')
                time.sleep(1)

        self.stdout.write(self.style.SUCCESS('Database available!'))
示例#26
0
 def test_posthook(self) -> None:
     cb = Mock(name='post_reconnect_hook')
     ddr.pre_reconnect.connect(fix_connection)
     ddr.post_reconnect.connect(cb)
     from django.db import connection
     connection.close()
     connection.s_connect = connection.connect
     connection.connect = Mock(side_effect=OperationalError('reconnect testing'))
     connection.ensure_connection()
     ReconnectTests.cls_atomics['default'] = transaction.atomic(using='default')
     ReconnectTests.cls_atomics['default'].__enter__()
     self.assertTrue(cb.called)
     self.assertTrue(connection.is_usable())
示例#27
0
    def test_request_finished_db_state(self):
        # Force closing connection on request end
        connection.settings_dict['CONN_MAX_AGE'] = 0

        # The GET below will not succeed, but it will give a response with
        # defined ._handler_class. That is needed for sending the
        # request_finished signal.
        response = self.client.get('/')
        # Make sure there is an open connection
        connection.ensure_connection()
        connection.enter_transaction_management()
        signals.request_finished.send(sender=response._handler_class)
        self.assertEqual(len(connection.transaction_state), 0)
示例#28
0
    def handle(self, *args, **options):
        """Handle the command"""
        self.stdout.write('Waiting for database...')
        for i in range(10):
            try:
                connection.ensure_connection()
            except OperationalError:
                if i == 9:
                    raise
                self.stdout.write('Database unavailable, waiting 1 second...')
                time.sleep(1)

        self.stdout.write(self.style.SUCCESS('Database available!'))
示例#29
0
    def handle(self, *args, **options):
        self.log_console('Waiting for database...')

        db_connected = None
        while not db_connected:
            try:
                connection.ensure_connection()
                db_connected = True
            except Error:
                self.log_console('Database unavailable, waiting 1 second...')
                time.sleep(1)

        self.log_console('Database available!')
示例#30
0
    def handle(self, *args, **options):
        """Handle the command"""
        print('Waiting for database...')
        db_conn = None
        while not db_conn:
            try:
                connection.ensure_connection()
                db_conn = True
            except OperationalError as e:
                print('Database unavailable, waiting 1 second...', e)
                time.sleep(1)

        print('Database available!')
示例#31
0
    def test_request_finished_db_state(self):
        # Force closing connection on request end
        connection.settings_dict['CONN_MAX_AGE'] = 0

        # The GET below will not succeed, but it will give a response with
        # defined ._handler_class. That is needed for sending the
        # request_finished signal.
        response = self.client.get('/')
        # Make sure there is an open connection
        connection.ensure_connection()
        connection.enter_transaction_management()
        signals.request_finished.send(sender=response._handler_class)
        self.assertEqual(len(connection.transaction_state), 0)
示例#32
0
文件: orm.py 项目: kcns008/maas
def connected():
    """Context manager that ensures we're connected to the database.

    If there is not yet a connection to the database, this will connect on
    entry and disconnect on exit. Preexisting connections will be left alone.
    """
    if connection.connection is None:
        connection.ensure_connection()
        try:
            yield
        finally:
            connection.close()
    else:
        yield
def database_connected(app_configs, **kwargs):
    errors = []

    try:
        connection.ensure_connection()
    except OperationalError as e:
        msg = 'Could not connect to database: {!s}'.format(e)
        errors.append(Error(msg, id=ERROR_CANNOT_CONNECT_DATABASE))
    except ImproperlyConfigured as e:
        msg = 'Datbase misconfigured: "{!s}"'.format(e)
        errors.append(Error(msg, id=ERROR_MISCONFIGURED_DATABASE))
    else:
        if not connection.is_usable():
            errors.append(Error('Database connection is not usable', id=ERROR_UNUSABLE_DATABASE))

    return errors
示例#34
0
# Getting the secrets
admin_username = os.getenv('ADMIN_USERNAME')
admin_password = os.getenv('ADMIN_PASSWORD')
admin_email = os.getenv('ADMIN_EMAIL')


#########################################################
# 1. Waiting for PostgreSQL
#########################################################

print("-----------------------------------------------------")
print("1. Waiting for PostgreSQL")
for _ in range(60):
    try:
        connection.ensure_connection()
        break
    except OperationalError:
        time.sleep(1)
else:
    connection.ensure_connection()
connection.close()

#########################################################
# 2. Running the migrations
#########################################################

print("-----------------------------------------------------")
print("2. Running the migrations")
call_command('migrate', '--noinput')
示例#35
0
文件: messages.py 项目: danshev/zulip
 def get_dj_conn():
     connection.ensure_connection()
     return connection.connection
示例#36
0
 def get_dj_conn():
     # type: () -> TimeTrackingConnection
     connection.ensure_connection()
     return connection.connection
示例#37
0
 def test_client_encoding(self):
     """Client encoding is set correctly."""
     connection.ensure_connection()
     self.assertEqual(connection.connection.encoding, 'UTF-8')
     self.assertEqual(connection.connection.nencoding, 'UTF-8')
示例#38
0
文件: tests.py 项目: zalmoxis/django
 def test_timezone_none_use_tz_false(self):
     connection.ensure_connection()
     with self.settings(TIME_ZONE=None, USE_TZ=False):
         connection.init_connection_state()
示例#39
0
 def test_client_encoding(self):
     # If the backend is Oracle, test that the client encoding is set
     # correctly.  This was broken under Cygwin prior to r14781.
     connection.ensure_connection()
     self.assertEqual(connection.connection.encoding, "UTF-8")
     self.assertEqual(connection.connection.nencoding, "UTF-8")
 def ping(self):
     connection.ensure_connection()