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 ProgrammingError as e:
        logger.debug(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 (suggestion):
A little more context would be appreciated, because you don't get line numbers or anything. If a log statement looks absurdly redundant, you're doing it right. Like `f'Caught known error due to existing partition: {e}'`

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