improved streamer killing

This commit is contained in:
Devaev Maxim 2018-06-29 23:59:43 +03:00
parent 93e28c62e5
commit 5589ecbac4

View File

@ -57,8 +57,8 @@ class Streamer: # pylint: disable=too-many-instance-attributes
async def __process(self) -> None: async def __process(self) -> None:
logger = get_logger(0) logger = get_logger(0)
proc: Optional[asyncio.subprocess.Process] = None # pylint: disable=no-member while True: # pylint: disable=too-many-nested-blocks
while True: proc: Optional[asyncio.subprocess.Process] = None # pylint: disable=no-member
try: try:
proc = await asyncio.create_subprocess_shell( proc = await asyncio.create_subprocess_shell(
self.__cmd, self.__cmd,
@ -78,27 +78,36 @@ class Streamer: # pylint: disable=too-many-instance-attributes
if empty == 100: # asyncio bug if empty == 100: # asyncio bug
break break
await self.__kill(proc)
raise RuntimeError("WTF") raise RuntimeError("WTF")
except asyncio.CancelledError: except asyncio.CancelledError:
break break
except Exception as err: except Exception as err:
if proc: if proc:
logger.exception("Unexpected finished streamer pid=%d with retcode=%d", proc.pid, proc.returncode) logger.exception("Unexpected streamer error: pid=%d", proc.pid)
else: else:
logger.exception("Can't start streamer: %s", err) logger.exception("Can't start streamer: %s", err)
await asyncio.sleep(1) await asyncio.sleep(1)
if proc: finally:
await self.__kill(proc) if proc and proc.returncode is None:
await self.__kill(proc)
async def __kill(self, proc: asyncio.subprocess.Process) -> None: # pylint: disable=no-member async def __kill(self, proc: asyncio.subprocess.Process) -> None: # pylint: disable=no-member
try: try:
proc.terminate() proc.terminate()
await asyncio.sleep(1) await asyncio.sleep(1)
if proc.returncode is None: if proc.returncode is None:
proc.kill() try:
proc.kill()
except Exception:
if proc.returncode is not None:
raise
await proc.wait() await proc.wait()
get_logger().info("Streamer killed: pid=%d; retcode=%d")
except Exception: except Exception:
pass if proc.returncode is None:
get_logger().exception("Can't kill streamer pid=%d", proc.pid)
else:
get_logger().info("Streamer killed: pid=%d; retcode=%d", proc.pid, proc.returncode)