From 6b1f88df669883f640b8c75e9be4cd80c9d4e22b Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 21 Aug 2019 13:28:12 -0400 Subject: [PATCH] fix `status` breaking (and loggly) when the internet connection is lost --- lbry/lbry/extras/daemon/Daemon.py | 4 ++++ lbry/lbry/extras/daemon/loggly_handler.py | 10 +++++++--- lbry/lbry/utils.py | 16 +++++----------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lbry/lbry/extras/daemon/Daemon.py b/lbry/lbry/extras/daemon/Daemon.py index 5c12ddebf..ea4619eb0 100644 --- a/lbry/lbry/extras/daemon/Daemon.py +++ b/lbry/lbry/extras/daemon/Daemon.py @@ -392,6 +392,10 @@ class Daemon(metaclass=JSONRPCServerType): async def update_connection_status(self): connected = await utils.async_check_connection() + if connected and not self._connection_status[1]: + log.info("detected internet connection is working") + elif not connected and self._connection_status[1]: + log.warning("detected internet connection was lost") self._connection_status = (self.component_manager.loop.time(), connected) async def get_connection_status(self) -> str: diff --git a/lbry/lbry/extras/daemon/loggly_handler.py b/lbry/lbry/extras/daemon/loggly_handler.py index 121d5454a..91108bc73 100644 --- a/lbry/lbry/extras/daemon/loggly_handler.py +++ b/lbry/lbry/extras/daemon/loggly_handler.py @@ -1,4 +1,5 @@ import asyncio +from aiohttp.client_exceptions import ClientError import json import logging.handlers import traceback @@ -49,9 +50,12 @@ class HTTPSLogglyHandler(logging.Handler): return record.getMessage() async def _emit(self, record): - payload = self.format(record) - async with utils.aiohttp_request('post', self.url, data=payload.encode(), cookies=self.cookies) as response: - self.cookies.update(response.cookies) + try: + async with utils.aiohttp_request('post', self.url, data=self.format(record).encode(), + cookies=self.cookies) as response: + self.cookies.update(response.cookies) + except ClientError: + pass def emit(self, record): asyncio.ensure_future(self._emit(record)) diff --git a/lbry/lbry/utils.py b/lbry/lbry/utils.py index 3bd9f5c2e..a1a9ad6f1 100644 --- a/lbry/lbry/utils.py +++ b/lbry/lbry/utils.py @@ -81,28 +81,22 @@ def check_connection(server="lbry.com", port=80, timeout=5) -> bool: try: server = socket.gethostbyname(server) socket.create_connection((server, port), timeout).close() - log.debug('Connection successful') return True except (socket.gaierror, socket.herror) as ex: - log.warning("Failed to connect to %s:%s. Unable to resolve domain. Trying to bypass DNS", - server, port) + log.debug("Failed to connect to %s:%s. Unable to resolve domain. Trying to bypass DNS", + server, port) try: server = "8.8.8.8" port = 53 socket.create_connection((server, port), timeout).close() - log.debug('Connection successful') return True - except Exception: - log.error("Failed to connect to %s:%s. Maybe the internet connection is not working", - server, port) + except OSError: return False - except Exception: - log.error("Failed to connect to %s:%s. Maybe the internet connection is not working", - server, port) + except OSError: return False -async def async_check_connection(server="lbry.com", port=80, timeout=5) -> bool: +async def async_check_connection(server="lbry.com", port=80, timeout=1) -> bool: return await asyncio.get_event_loop().run_in_executor(None, check_connection, server, port, timeout)