jason810496 commented on code in PR #57741:
URL: https://github.com/apache/airflow/pull/57741#discussion_r2510198058


##########
airflow-core/src/airflow/cli/hot_reload.py:
##########
@@ -0,0 +1,197 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Hot reload utilities for development mode."""
+
+from __future__ import annotations
+
+import os
+import signal
+import sys
+from collections.abc import Callable, Sequence
+from pathlib import Path
+from typing import TYPE_CHECKING
+
+import structlog
+
+if TYPE_CHECKING:
+    import subprocess
+
+log = structlog.getLogger(__name__)
+
+
+def run_with_reloader(
+    callback: Callable,
+    process_name: str = "process",
+):
+    """
+    Run a callback function with automatic reloading on file changes.
+
+    This function monitors specified paths for changes and restarts the process
+    when changes are detected. Useful for development mode hot-reloading.
+
+    :param callback: The function to run. This should be the main entry point
+        of the command that needs hot-reload support.
+    :param process_name: Name of the process being run (for logging purposes)
+    """
+    # Default watch paths - watch the airflow source directory
+    import airflow
+
+    airflow_root = Path(airflow.__file__).parent
+    watch_paths = [airflow_root]
+
+    log.info("Starting %s in development mode with hot-reload enabled", 
process_name)
+    log.info("Watching paths: %s", watch_paths)
+
+    # Check if we're the main process or a reloaded child
+    reloader_pid = os.environ.get("AIRFLOW_DEV_RELOADER_PID")
+    if reloader_pid is None:
+        # We're the main process - set up the reloader
+        os.environ["AIRFLOW_DEV_RELOADER_PID"] = str(os.getpid())
+        _run_reloader(watch_paths)
+    else:
+        # We're a child process - just run the callback
+        callback()
+
+
+def _terminate_process_tree(
+    process: subprocess.Popen[bytes],
+    timeout: int = 5,
+    force_kill_remaining: bool = True,
+) -> None:
+    """
+    Terminate a process and all its children recursively.
+
+    Uses psutil to ensure all child processes are properly terminated,
+    which is important for cleaning up subprocesses like serve-log servers.
+
+    :param process: The subprocess.Popen process to terminate
+    :param timeout: Timeout in seconds to wait for graceful termination
+    :param force_kill_remaining: If True, force kill processes that don't 
terminate gracefully
+    """
+    import subprocess
+
+    try:
+        import psutil

Review Comment:
   Good catch, the `psutil` should be present. I just moved it out of `try` 
block.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to