janus runner draft

This commit is contained in:
Devaev Maxim
2021-05-24 05:03:45 +03:00
parent 9cead62032
commit 19a68887e4
17 changed files with 565 additions and 30 deletions

View File

@@ -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: