diff options
author | Devaev Maxim <[email protected]> | 2021-05-24 05:03:45 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2021-05-24 05:08:53 +0300 |
commit | 19a68887e4083755af9f3edcb59c69e89b34b6f7 (patch) | |
tree | eee102f2cf1312121d8af606e0bdeabb507fb236 /kvmd/aioproc.py | |
parent | 9cead6203295e55c25b9a011a6da93509d05e79f (diff) |
janus runner draft
Diffstat (limited to 'kvmd/aioproc.py')
-rw-r--r-- | kvmd/aioproc.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/kvmd/aioproc.py b/kvmd/aioproc.py index 19925f10..d7d33f05 100644 --- a/kvmd/aioproc.py +++ b/kvmd/aioproc.py @@ -72,7 +72,7 @@ async def log_process( if stdout: log = (logger.info if proc.returncode == 0 else logger.error) for line in stdout.split("\n"): - log("Console: %s", line) + log("=> %s", line) return proc @@ -81,12 +81,34 @@ async def log_stdout_infinite(proc: asyncio.subprocess.Process, logger: logging. async for line_bytes in proc.stdout: # type: ignore line = line_bytes.decode(errors="ignore").strip() if line: - logger.info("Console: %s", line) + logger.info("=> %s", line) empty = 0 else: empty += 1 if empty == 100: # asyncio bug - raise RuntimeError("asyncio process: too many empty lines") + raise RuntimeError("Asyncio process: too many empty lines") + + +async def kill_process(proc: asyncio.subprocess.Process, wait: float, logger: logging.Logger) -> None: # pylint: disable=no-member + if proc.returncode is None: + try: + proc.terminate() + await asyncio.sleep(wait) + if proc.returncode is None: + try: + proc.kill() + except Exception: + if proc.returncode is not None: + raise + await proc.wait() + logger.info("Process killed: pid=%d; retcode=%d", proc.pid, proc.returncode) + except asyncio.CancelledError: + pass + except Exception: + if proc.returncode is None: + logger.exception("Can't kill process pid=%d", proc.pid) + else: + logger.info("Process killed: pid=%d; retcode=%d", proc.pid, proc.returncode) def rename_process(suffix: str, prefix: str="kvmd") -> None: |