summaryrefslogtreecommitdiff
path: root/kvmd/aioproc.py
diff options
context:
space:
mode:
Diffstat (limited to 'kvmd/aioproc.py')
-rw-r--r--kvmd/aioproc.py28
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: