From e5af041627cc22d904d3128068d15cc6033d2e74 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Sun, 3 Feb 2019 16:19:29 -0500 Subject: [PATCH] fix connection status message --- lbrynet/extras/daemon/Daemon.py | 16 +++++++++++++++- lbrynet/utils.py | 17 ++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lbrynet/extras/daemon/Daemon.py b/lbrynet/extras/daemon/Daemon.py index 9c9946be3..e275cbade 100644 --- a/lbrynet/extras/daemon/Daemon.py +++ b/lbrynet/extras/daemon/Daemon.py @@ -256,6 +256,7 @@ class Daemon(metaclass=JSONRPCServerType): skip_components=conf.components_to_skip or [] ) self.component_startup_task = None + self._connection_status: typing.Tuple[float, bool] = [self.component_manager.loop.time(), False] logging.getLogger('aiohttp.access').setLevel(logging.WARN) app = web.Application() @@ -368,6 +369,18 @@ class Daemon(metaclass=JSONRPCServerType): if not os.path.isdir(self.conf.download_dir): os.makedirs(self.conf.download_dir) + async def update_connection_status(self): + connected = await utils.async_check_connection() + self._connection_status = (self.component_manager.loop.time(), connected) + + async def get_connection_status(self) -> str: + if self._connection_status[0] + 300 > self.component_manager.loop.time(): + if not self._connection_status[1]: + await self.update_connection_status() + else: + await self.update_connection_status() + return CONNECTION_STATUS_CONNECTED if self._connection_status[1] else CONNECTION_STATUS_NETWORK + async def start(self): log.info("Starting LBRYNet Daemon") log.debug("Settings: %s", json.dumps(self.conf.settings_dict, indent=2)) @@ -665,7 +678,8 @@ class Daemon(metaclass=JSONRPCServerType): } """ - connection_code = CONNECTION_STATUS_NETWORK + connection_code = await self.get_connection_status() + response = { 'installation_id': self.installation_id, 'is_running': all(self.component_manager.get_components_status().values()), diff --git a/lbrynet/utils.py b/lbrynet/utils.py index 6b0265ff6..9b02c01bd 100644 --- a/lbrynet/utils.py +++ b/lbrynet/utils.py @@ -69,13 +69,12 @@ def obfuscate(plain): return rot13(base64.b64encode(plain).decode()) -def check_connection(server="lbry.io", port=80, timeout=5): +def check_connection(server="lbry.io", port=80, timeout=5) -> bool: """Attempts to open a socket to server:port and returns True if successful.""" log.debug('Checking connection to %s:%s', server, port) try: server = socket.gethostbyname(server) - conn = socket.create_connection((server, port), timeout) - conn.close() + socket.create_connection((server, port), timeout).close() log.debug('Connection successful') return True except (socket.gaierror, socket.herror) as ex: @@ -84,19 +83,23 @@ def check_connection(server="lbry.io", port=80, timeout=5): try: server = "8.8.8.8" port = 53 - socket.create_connection((server, port), timeout) + socket.create_connection((server, port), timeout).close() log.debug('Connection successful') return True - except Exception as ex: + except Exception: log.error("Failed to connect to %s:%s. Maybe the internet connection is not working", server, port) return False - except Exception as ex: + except Exception: log.error("Failed to connect to %s:%s. Maybe the internet connection is not working", - server, port) + server, port) return False +async def async_check_connection(server="lbry.io", port=80, timeout=5) -> bool: + return await asyncio.get_event_loop().run_in_executor(None, check_connection, server, port, timeout) + + def random_string(length=10, chars=string.ascii_lowercase): return ''.join([random.choice(chars) for _ in range(length)])