|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# Copyright (c) 2026 Maurice Garcia |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +BACKGROUND_CHILD_ENV = "PYPNM_BACKGROUND_CHILD" |
| 13 | +RUN_BACKGROUND_FLAG = "--run-background" |
| 14 | +BACKGROUND_LOG_FILE_FLAG = "--background-log-file" |
| 15 | +BACKGROUND_PID_FILE_FLAG = "--background-pidfile" |
| 16 | + |
| 17 | + |
| 18 | +def background_log_path(runtime_dir: str, app_slug: str) -> Path: |
| 19 | + """Return the default detached-serve log file path.""" |
| 20 | + return Path(runtime_dir) / f"{app_slug}.serve.log" |
| 21 | + |
| 22 | + |
| 23 | +def background_pidfile_path(runtime_dir: str, app_slug: str) -> Path: |
| 24 | + """Return the default detached-serve pidfile path.""" |
| 25 | + return Path(runtime_dir) / f"{app_slug}.serve.pid" |
| 26 | + |
| 27 | + |
| 28 | +def launch_background_serve( |
| 29 | + *, |
| 30 | + module_name: str, |
| 31 | + app_slug: str, |
| 32 | + runtime_dir: str, |
| 33 | + argv: list[str], |
| 34 | + log_file: str | None, |
| 35 | + pidfile: str | None, |
| 36 | +) -> int: |
| 37 | + """Detach a serve command into the background and return success.""" |
| 38 | + runtime_path = Path(runtime_dir) |
| 39 | + runtime_path.mkdir(parents=True, exist_ok=True) |
| 40 | + |
| 41 | + resolved_log_file = Path(log_file) if log_file is not None and log_file.strip() != "" else background_log_path(runtime_dir, app_slug) |
| 42 | + resolved_pidfile = Path(pidfile) if pidfile is not None and pidfile.strip() != "" else background_pidfile_path(runtime_dir, app_slug) |
| 43 | + resolved_log_file.parent.mkdir(parents=True, exist_ok=True) |
| 44 | + resolved_pidfile.parent.mkdir(parents=True, exist_ok=True) |
| 45 | + |
| 46 | + child_argv = _strip_background_flags(argv) |
| 47 | + command = [sys.executable, "-m", module_name, *child_argv] |
| 48 | + child_env = os.environ.copy() |
| 49 | + child_env[BACKGROUND_CHILD_ENV] = "1" |
| 50 | + |
| 51 | + with resolved_log_file.open("ab") as log_handle: |
| 52 | + process = subprocess.Popen( |
| 53 | + command, |
| 54 | + stdin=subprocess.DEVNULL, |
| 55 | + stdout=log_handle, |
| 56 | + stderr=subprocess.STDOUT, |
| 57 | + start_new_session=True, |
| 58 | + close_fds=True, |
| 59 | + env=child_env, |
| 60 | + ) |
| 61 | + |
| 62 | + resolved_pidfile.write_text(f"{int(process.pid)}\n", encoding="utf-8") |
| 63 | + print(f"[INFO] Background serve started: pid={int(process.pid)}") |
| 64 | + print(f"[INFO] Background serve log: {resolved_log_file}") |
| 65 | + print(f"[INFO] Background serve pidfile: {resolved_pidfile}") |
| 66 | + return 0 |
| 67 | + |
| 68 | + |
| 69 | +def _strip_background_flags(argv: list[str]) -> list[str]: |
| 70 | + """Return argv without background-launch flags and their values.""" |
| 71 | + stripped: list[str] = [] |
| 72 | + skip_next = False |
| 73 | + for token in argv: |
| 74 | + if skip_next: |
| 75 | + skip_next = False |
| 76 | + continue |
| 77 | + if token == RUN_BACKGROUND_FLAG: |
| 78 | + continue |
| 79 | + if token in (BACKGROUND_LOG_FILE_FLAG, BACKGROUND_PID_FILE_FLAG): |
| 80 | + skip_next = True |
| 81 | + continue |
| 82 | + if token.startswith(f"{BACKGROUND_LOG_FILE_FLAG}=") or token.startswith(f"{BACKGROUND_PID_FILE_FLAG}="): |
| 83 | + continue |
| 84 | + stripped.append(token) |
| 85 | + return stripped |
| 86 | + |
| 87 | + |
| 88 | +__all__ = [ |
| 89 | + "BACKGROUND_CHILD_ENV", |
| 90 | + "RUN_BACKGROUND_FLAG", |
| 91 | + "BACKGROUND_LOG_FILE_FLAG", |
| 92 | + "BACKGROUND_PID_FILE_FLAG", |
| 93 | + "background_log_path", |
| 94 | + "background_pidfile_path", |
| 95 | + "launch_background_serve", |
| 96 | +] |
0 commit comments