Repository: ansible/awx
File: awx/main/utils/common.py (Python)

Code before:
        else:
            start = current_time.replace(microsecond=0, second=0, minute=0)
    if not end:
        if minutely:
            end = start.replace(microsecond=0, second=0) + timedelta(minutes=1)
        else:
            end = start.replace(microsecond=0, second=0, minute=0) + timedelta(hours=1)
    start_timestamp = str(start)
    end_timestamp = str(end)

    if not partition_label:
        if minutely:
            partition_label = start.strftime('%Y%m%d_%H%M')
        else:
            partition_label = start.strftime('%Y%m%d_%H')

    try:
        with transaction.atomic():
            with connection.cursor() as cursor:
                cursor.execute(
                    f'CREATE TABLE IF NOT EXISTS {tblname}_{partition_label} '
                    f'PARTITION OF {tblname} '
                    f'FOR VALUES FROM (\'{start_timestamp}\') to (\'{end_timestamp}\');'
                )
    except Exception as e:
        logger.warning(e)


def cleanup_new_process(func):
    """
    Cleanup django connection, cache connection, before executing new thread or processes entry point, func.
    """

    @wraps(func)
    def wrapper_cleanup_new_process(*args, **kwargs):
        from awx.conf.settings import SettingsWrapper  # noqa

        django_connection.close()
        django_cache.close()
        SettingsWrapper.initialize()
        return func(*args, **kwargs)

    return wrapper_cleanup_new_process


Review comment (bug):
not sure of the best log level here -- exception and error is going to show a bunch of scary red text, and we probably don't want users to panic or open issues if they see it.

I think warning is okay-ish, but I could be convinced of debug or even just passing with no logging.

Code after:
        else:
            start = current_time.replace(microsecond=0, second=0, minute=0)
    if not end:
        if minutely:
            end = start.replace(microsecond=0, second=0) + timedelta(minutes=1)
        else:
            end = start.replace(microsecond=0, second=0, minute=0) + timedelta(hours=1)
    start_timestamp = str(start)
    end_timestamp = str(end)

    if not partition_label:
        if minutely:
            partition_label = start.strftime('%Y%m%d_%H%M')
        else:
            partition_label = start.strftime('%Y%m%d_%H')

    try:
        with transaction.atomic():
            with connection.cursor() as cursor:
                cursor.execute(
                    f'CREATE TABLE IF NOT EXISTS {tblname}_{partition_label} '
                    f'PARTITION OF {tblname} '
                    f'FOR VALUES FROM (\'{start_timestamp}\') to (\'{end_timestamp}\');'
                )
    except ProgrammingError as e:
        logger.debug(f'Caught known error due to existing partition: {e}')


def cleanup_new_process(func):
    """
    Cleanup django connection, cache connection, before executing new thread or processes entry point, func.
    """

    @wraps(func)
    def wrapper_cleanup_new_process(*args, **kwargs):
        from awx.conf.settings import SettingsWrapper  # noqa

        django_connection.close()
        django_cache.close()
        SettingsWrapper.initialize()
        return func(*args, **kwargs)

    return wrapper_cleanup_new_process