fix status
breaking (and loggly) when the internet connection is lost
This commit is contained in:
parent
d246c46227
commit
6b1f88df66
3 changed files with 16 additions and 14 deletions
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
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))
|
||||
|
|
|
@ -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",
|
||||
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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue