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

Code before:
    return max(1, max_forks_based_on_memory)


_inventory_updates = threading.local()
_task_manager = threading.local()
_dependency_manager = threading.local()
_workflow_manager = threading.local()


@contextlib.contextmanager
def task_manager_bulk_reschedule():
    managers = [ScheduleTaskManager(), ScheduleWorkflowManager(), ScheduleDependencyManager()]
    """Context manager to avoid submitting task multiple times."""
    try:
        for m in managers:
            previous_flag = getattr(m.manager_threading_local, 'bulk_reschedule', False)
            previous_value = getattr(m.manager_threading_local, 'needs_scheduling', False)
            m.manager_threading_local.bulk_reschedule = True
            m.manager_threading_local.needs_scheduling = False
        yield
    finally:
        for m in managers:
            m.manager_threading_local.bulk_reschedule = previous_flag
            if m.manager_threading_local.needs_scheduling:
                m.schedule()
            m.manager_threading_local.needs_scheduling = previous_value


class ScheduleManager:
    def __init__(self, manager, manager_threading_local):
        self.manager = manager
        self.manager_threading_local = manager_threading_local

    def schedule(self):
        if getattr(self.manager_threading_local, 'bulk_reschedule', False):
            self.manager_threading_local.needs_scheduling = True
            return
        from django.db import connection

        # runs right away if not in transaction
        connection.on_commit(lambda: self.manager.delay())


class ScheduleTaskManager(ScheduleManager):
    def __init__(self):
        from awx.main.scheduler.tasks import task_manager

        super().__init__(task_manager, _task_manager)


class ScheduleDependencyManager(ScheduleManager):

Review comment (suggestion):
The scope of `previous_value` and `previous_flag` doesn't make any sense to me.

```suggestion
            m.previous_flag = getattr(m.manager_threading_local, 'bulk_reschedule', False)
            m.previous_value = getattr(m.manager_threading_local, 'needs_scheduling', False)
            m.manager_threading_local.bulk_reschedule = True
            m.manager_threading_local.needs_scheduling = False
        yield
    finally:
        for m in managers:
            m.manager_threading_local.bulk_reschedule = m.previous_flag
            if m.manager_threading_local.needs_scheduling:
                m.schedule()
            m.manager_threading_local.needs_scheduling = m.previous_value
            del m.previous_value
            del m.previous_flag
```

Code after:
    return max(1, max_forks_based_on_memory)


_inventory_updates = threading.local()
_task_manager = threading.local()
_dependency_manager = threading.local()
_workflow_manager = threading.local()


@contextlib.contextmanager
def task_manager_bulk_reschedule():
    managers = [ScheduleTaskManager(), ScheduleWorkflowManager(), ScheduleDependencyManager()]
    """Context manager to avoid submitting task multiple times."""
    try:
        for m in managers:
            m.previous_flag = getattr(m.manager_threading_local, 'bulk_reschedule', False)
            m.previous_value = getattr(m.manager_threading_local, 'needs_scheduling', False)
            m.manager_threading_local.bulk_reschedule = True
            m.manager_threading_local.needs_scheduling = False
        yield
    finally:
        for m in managers:
            m.manager_threading_local.bulk_reschedule = m.previous_flag
            if m.manager_threading_local.needs_scheduling:
                m.schedule()
            m.manager_threading_local.needs_scheduling = m.previous_value


class ScheduleManager:
    def __init__(self, manager, manager_threading_local):
        self.manager = manager
        self.manager_threading_local = manager_threading_local

    def schedule(self):
        if getattr(self.manager_threading_local, 'bulk_reschedule', False):
            self.manager_threading_local.needs_scheduling = True
            return
        from django.db import connection

        # runs right away if not in transaction
        connection.on_commit(lambda: self.manager.delay())


class ScheduleTaskManager(ScheduleManager):
    def __init__(self):
        from awx.main.scheduler.tasks import task_manager

        super().__init__(task_manager, _task_manager)


class ScheduleDependencyManager(ScheduleManager):