Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/executor/manager.py (Python)

Code before:

    @classmethod
    def on_node_executor_start(cls):
        configure_logging()

        cls.loop = asyncio.new_event_loop()
        cls.pid = os.getpid()

        redis.connect()
        cls.loop.run_until_complete(db.connect())
        cls.agent_server_client = get_agent_server_client()

        # Set up shutdown handlers
        cls.shutdown_lock = threading.Lock()
        atexit.register(cls.on_node_executor_stop)  # handle regular shutdown
        signal.signal(  # handle termination
            signal.SIGTERM, lambda _, __: cls.on_node_executor_sigterm()
        )

    @classmethod
    def on_node_executor_stop(cls):
        if not cls.shutdown_lock.acquire(blocking=False):
            return  # already shutting down

        logger.info(f"[on_node_executor_stop {cls.pid}] ⏳ Disconnecting DB...")
        redis.disconnect()
        cls.loop.run_until_complete(db.disconnect())
        logger.info(f"[on_node_executor_stop {cls.pid}] ✅ Finished cleanup")

    @classmethod
    def on_node_executor_sigterm(cls):
        llprint(f"[on_node_executor_sigterm {cls.pid}] ⚠️ SIGTERM received")
        if not cls.shutdown_lock.acquire(blocking=False):
            return  # already shutting down, no need to self-terminate

        llprint(f"[on_node_executor_sigterm {cls.pid}] ⏳ Disconnecting DB...")
        cls.loop.run_until_complete(db.disconnect())
        llprint(f"[on_node_executor_sigterm {cls.pid}] ✅ Finished cleanup")
        sys.exit(0)

    @classmethod
    @error_logged
    def on_node_execution(
        cls,
        q: ExecutionQueue[NodeExecution],
        node_exec: NodeExecution,
        input_credentials: Credentials | None,
    ):
        log_metadata = LogMetadata(
            user_id=node_exec.user_id,
            graph_eid=node_exec.graph_exec_id,

Review comment (refactor):
please move this below the `db.disconnect()`, for 2 reasons:
- it's now directly after a log statement that says "Disconnecting DB"
- startup/shutdown sequences should be each other's reverse

Code after:

    @classmethod
    def on_node_executor_start(cls):
        configure_logging()

        cls.loop = asyncio.new_event_loop()
        cls.pid = os.getpid()

        redis.connect()
        cls.loop.run_until_complete(db.connect())
        cls.agent_server_client = get_agent_server_client()

        # Set up shutdown handlers
        cls.shutdown_lock = threading.Lock()
        atexit.register(cls.on_node_executor_stop)  # handle regular shutdown
        signal.signal(  # handle termination
            signal.SIGTERM, lambda _, __: cls.on_node_executor_sigterm()
        )

    @classmethod
    def on_node_executor_stop(cls):
        if not cls.shutdown_lock.acquire(blocking=False):
            return  # already shutting down

        logger.info(f"[on_node_executor_stop {cls.pid}] ⏳ Disconnecting DB...")
        cls.loop.run_until_complete(db.disconnect())
        logger.info(f"[on_node_executor_stop {cls.pid}] ⏳ Disconnecting Redis...")
        redis.disconnect()
        logger.info(f"[on_node_executor_stop {cls.pid}] ✅ Finished cleanup")

    @classmethod
    def on_node_executor_sigterm(cls):
        llprint(f"[on_node_executor_sigterm {cls.pid}] ⚠️ SIGTERM received")
        if not cls.shutdown_lock.acquire(blocking=False):
            return  # already shutting down, no need to self-terminate

        llprint(f"[on_node_executor_sigterm {cls.pid}] ⏳ Disconnecting DB...")
        cls.loop.run_until_complete(db.disconnect())
        llprint(f"[on_node_executor_sigterm {cls.pid}] ✅ Finished cleanup")
        sys.exit(0)

    @classmethod
    @error_logged
    def on_node_execution(
        cls,
        q: ExecutionQueue[NodeExecution],
        node_exec: NodeExecution,
        input_credentials: Credentials | None,
    ):
        log_metadata = LogMetadata(
            user_id=node_exec.user_id,