fix connection status message

This commit is contained in:
Jack Robison 2019-02-03 16:19:29 -05:00
parent 73f4e954e5
commit e5af041627
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 25 additions and 8 deletions

View file

@ -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()),

View file

@ -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)])