Start the daemon as a background process. Returns the ``Popen`` object so callers can detect early process death (via ``proc.poll()``) instead of waiting for a full timeout.
()
| 412 | |
| 413 | |
| 414 | def start_daemon() -> subprocess.Popen[bytes]: |
| 415 | """Start the daemon as a background process. |
| 416 | |
| 417 | Returns the ``Popen`` object so callers can detect early process death |
| 418 | (via ``proc.poll()``) instead of waiting for a full timeout. |
| 419 | """ |
| 420 | daemon_runtime_dir().mkdir(parents=True, exist_ok=True) |
| 421 | log_path = daemon_log_path() |
| 422 | |
| 423 | ccc_path = _find_ccc_executable() |
| 424 | if ccc_path: |
| 425 | cmd = [ccc_path, "run-daemon"] |
| 426 | else: |
| 427 | cmd = [sys.executable, "-m", "cocoindex_code.cli", "run-daemon"] |
| 428 | |
| 429 | log_fd = open(log_path, "w") |
| 430 | if sys.platform == "win32": |
| 431 | _create_no_window = 0x08000000 |
| 432 | proc = subprocess.Popen( |
| 433 | cmd, |
| 434 | stdout=log_fd, |
| 435 | stderr=log_fd, |
| 436 | stdin=subprocess.DEVNULL, |
| 437 | creationflags=_create_no_window, |
| 438 | ) |
| 439 | else: |
| 440 | proc = subprocess.Popen( |
| 441 | cmd, |
| 442 | start_new_session=True, |
| 443 | stdout=log_fd, |
| 444 | stderr=log_fd, |
| 445 | stdin=subprocess.DEVNULL, |
| 446 | ) |
| 447 | log_fd.close() |
| 448 | return proc |
| 449 | |
| 450 | |
| 451 | def _find_ccc_executable() -> str | None: |