Register a build process with the daemon for orphan cleanup. This function adds the current client PID and the build process PID to the daemon's registry. If the client dies, the daemon will automatically kill the build process and all its children. Args: root_pid: PID of t
(
root_pid: int,
example: str = "",
project_dir: str = "",
)
| 32 | |
| 33 | |
| 34 | def register_build_process( |
| 35 | root_pid: int, |
| 36 | example: str = "", |
| 37 | project_dir: str = "", |
| 38 | ) -> None: |
| 39 | """Register a build process with the daemon for orphan cleanup. |
| 40 | |
| 41 | This function adds the current client PID and the build process PID to the |
| 42 | daemon's registry. If the client dies, the daemon will automatically kill |
| 43 | the build process and all its children. |
| 44 | |
| 45 | Args: |
| 46 | root_pid: PID of the root build process (PlatformIO/scons) |
| 47 | example: Example name being built (for logging) |
| 48 | project_dir: Project directory (for logging) |
| 49 | """ |
| 50 | client_pid = os.getpid() |
| 51 | |
| 52 | try: |
| 53 | # Ensure daemon directory exists |
| 54 | DAEMON_DIR.mkdir(parents=True, exist_ok=True) |
| 55 | |
| 56 | # Read existing registry |
| 57 | registry = {} |
| 58 | if BUILD_REGISTRY_FILE.exists(): |
| 59 | try: |
| 60 | with open( |
| 61 | BUILD_REGISTRY_FILE, "r", encoding="utf-8", errors="replace" |
| 62 | ) as f: |
| 63 | registry = json.load(f) |
| 64 | except KeyboardInterrupt as ki: |
| 65 | handle_keyboard_interrupt(ki) |
| 66 | raise |
| 67 | except Exception: |
| 68 | pass # Start with empty registry if corrupted |
| 69 | |
| 70 | # Add this process |
| 71 | registry[str(client_pid)] = { |
| 72 | "client_pid": client_pid, |
| 73 | "root_pid": root_pid, |
| 74 | "child_pids": [], # Will be populated by daemon |
| 75 | "request_id": f"build_{root_pid}", |
| 76 | "project_dir": project_dir, |
| 77 | "started_at": 0, # Daemon will set this |
| 78 | "last_updated": 0, # Daemon will set this |
| 79 | } |
| 80 | |
| 81 | # Write atomically |
| 82 | temp_file = BUILD_REGISTRY_FILE.with_suffix(".tmp") |
| 83 | with open(temp_file, "w", encoding="utf-8", errors="replace") as f: |
| 84 | json.dump(registry, f, indent=2) |
| 85 | |
| 86 | temp_file.replace(BUILD_REGISTRY_FILE) |
| 87 | |
| 88 | logging.debug( |
| 89 | f"Registered build process: client={client_pid}, root={root_pid}, example={example}" |
| 90 | ) |
| 91 |
no test coverage detected