Own only newly started process groups; preserve bounded complete command logs.
| 55 | |
| 56 | |
| 57 | class Processes: |
| 58 | """Own only newly started process groups; preserve bounded complete command logs.""" |
| 59 | |
| 60 | def __init__(self, root): |
| 61 | self.root = Path(root) |
| 62 | self.root.mkdir() |
| 63 | self.records = [] |
| 64 | self.total_output_bytes = 0 |
| 65 | |
| 66 | @staticmethod |
| 67 | def group_alive(pgid): |
| 68 | try: |
| 69 | os.killpg(pgid, 0) |
| 70 | return True |
| 71 | except ProcessLookupError: |
| 72 | return False |
| 73 | |
| 74 | def run(self, label, argv, env=None, timeout=180): |
| 75 | if STOP.is_set(): |
| 76 | raise KeyboardInterrupt("driver stop requested") |
| 77 | directory = self.root / ("%03d-%s" % (len(self.records), label)) |
| 78 | directory.mkdir() |
| 79 | record = {"label": label, "argv": [str(x) for x in argv], "timeout": timeout, |
| 80 | "stdout": str(directory / "stdout.log"), |
| 81 | "stderr": str(directory / "stderr.log"), "cleanup": []} |
| 82 | self.records.append(record) |
| 83 | state = {"bytes": 0, "overflow": False, "errors": []} |
| 84 | lock = threading.Lock() |
| 85 | stop = threading.Event() |
| 86 | started = time.monotonic() |
| 87 | try: |
| 88 | process = subprocess.Popen(record["argv"], env=env, stdin=subprocess.DEVNULL, |
| 89 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 90 | start_new_session=True, bufsize=0) |
| 91 | except OSError as exc: |
| 92 | record["launch_error"] = str(exc) |
| 93 | write_json(directory / "process.json", record) |
| 94 | raise Infrastructure("command launch failed: " + label) from exc |
| 95 | record["pid"] = process.pid |
| 96 | |
| 97 | def drain(source, destination): |
| 98 | try: |
| 99 | with Path(destination).open("xb") as output: |
| 100 | while True: |
| 101 | block = source.read(65536) |
| 102 | if not block: |
| 103 | break |
| 104 | with lock: |
| 105 | remaining = max(0, min(64 * 1024 * 1024 - state["bytes"], |
| 106 | 512 * 1024 * 1024 - self.total_output_bytes)) |
| 107 | state["bytes"] += len(block) |
| 108 | self.total_output_bytes += len(block) |
| 109 | if len(block) > remaining: |
| 110 | state["overflow"] = True |
| 111 | stop.set() |
| 112 | output.write(block[:remaining]) |
| 113 | except Exception as exc: |
| 114 | with lock: |