diff options
author | Devaev Maxim <[email protected]> | 2018-12-09 01:37:32 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2018-12-09 01:37:32 +0300 |
commit | 014884f9ace5364cf8ebe70d558d1ad6b82867b2 (patch) | |
tree | 1816af6a5c07e603004dff0f9e4046a40c18eca9 | |
parent | 197e7cad6a835ebd8bf899ec3abd714f9a0774fb (diff) |
cleanup
-rw-r--r-- | kvmd/apps/kvmd/hid.py | 14 | ||||
-rw-r--r-- | kvmd/apps/kvmd/streamer.py | 22 |
2 files changed, 26 insertions, 10 deletions
diff --git a/kvmd/apps/kvmd/hid.py b/kvmd/apps/kvmd/hid.py index d7eb47cf..26cfa898 100644 --- a/kvmd/apps/kvmd/hid.py +++ b/kvmd/apps/kvmd/hid.py @@ -1,3 +1,5 @@ +import os +import signal import asyncio import multiprocessing import multiprocessing.queues @@ -135,13 +137,15 @@ class Hid(multiprocessing.Process): # pylint: disable=too-many-instance-attribu self.__pressed_keys.clear() def __emergency_clear_events(self) -> None: - try: - with serial.Serial(self.__device_path, self.__speed) as tty: - self.__send_clear_hid(tty) - except Exception: - get_logger().exception("Can't execute emergency clear HID events") + if os.path.exists(self.__device_path): + try: + with serial.Serial(self.__device_path, self.__speed) as tty: + self.__send_clear_hid(tty) + except Exception: + get_logger().exception("Can't execute emergency clear HID events") def run(self) -> None: # pylint: disable=too-many-branches + signal.signal(signal.SIGINT, signal.SIG_IGN) setproctitle.setproctitle("[hid] " + setproctitle.getproctitle()) try: with serial.Serial(self.__device_path, self.__speed) as tty: diff --git a/kvmd/apps/kvmd/streamer.py b/kvmd/apps/kvmd/streamer.py index 2527d4e9..c3d9bbe8 100644 --- a/kvmd/apps/kvmd/streamer.py +++ b/kvmd/apps/kvmd/streamer.py @@ -1,4 +1,5 @@ import os +import signal import asyncio import asyncio.subprocess @@ -61,13 +62,11 @@ class Streamer: # pylint: disable=too-many-instance-attributes self.__cmd = cmd self.__loop = loop - if self.__unix_path: - self.__http_session = aiohttp.ClientSession(connector=aiohttp.UnixConnector(path=self.__unix_path)) - else: - self.__http_session = aiohttp.ClientSession() self.__proc_task: Optional[asyncio.Task] = None + self.__http_session: Optional[aiohttp.ClientSession] = None + async def start(self, params: Dict, no_init_restart: bool=False) -> None: logger = get_logger() logger.info("Starting streamer ...") @@ -94,10 +93,11 @@ class Streamer: # pylint: disable=too-many-instance-attributes return dict(self.__params) async def get_state(self) -> Dict: + self.__ensure_session() url = "http://%s:%d/state" % (self.__host, self.__port) state = None try: - async with self.__http_session.get(url, timeout=self.__timeout) as response: + async with self.__http_session.get(url, timeout=self.__timeout) as response: # type: ignore response.raise_for_status() state = (await response.json())["result"] except aiohttp.ClientConnectorError: @@ -122,6 +122,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes *[self.__cmd[0], "--version"], stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL, + preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)), ) (stdout, _) = await proc.communicate() return stdout.decode(errors="ignore").strip() @@ -129,6 +130,16 @@ class Streamer: # pylint: disable=too-many-instance-attributes async def cleanup(self) -> None: if self.is_running(): await self.stop() + if self.__http_session: + await self.__http_session.close() + self.__http_session = None + + def __ensure_session(self) -> None: + if not self.__http_session: + if self.__unix_path: + self.__http_session = aiohttp.ClientSession(connector=aiohttp.UnixConnector(path=self.__unix_path)) + else: + self.__http_session = aiohttp.ClientSession() async def __inner_start(self) -> None: assert not self.__proc_task @@ -172,6 +183,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes *cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT, + preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)), ) logger.info("Started streamer pid=%d: %s", proc.pid, cmd) |