format: improve warning locations to remove warnings when connections are refused.

This commit is contained in:
UpstreamData
2023-02-16 13:35:20 -07:00
parent 1cd2566d0a
commit 71e9af1b91

View File

@@ -108,10 +108,6 @@ class MinerNetwork:
# clear cached miners # clear cached miners
MinerFactory().clear_cached_miners() MinerFactory().clear_cached_miners()
# create a list of tasks and miner IPs
scan_tasks = []
miners = []
limit = asyncio.Semaphore(PyasicSettings().network_scan_threads) limit = asyncio.Semaphore(PyasicSettings().network_scan_threads)
miners = await asyncio.gather( miners = await asyncio.gather(
*[self.ping_and_get_miner(host, limit) for host in local_network.hosts()] *[self.ping_and_get_miner(host, limit) for host in local_network.hosts()]
@@ -140,8 +136,6 @@ class MinerNetwork:
local_network = self.get_network() local_network = self.get_network()
# create a list of scan tasks # create a list of scan tasks
scan_tasks = []
limit = asyncio.Semaphore(PyasicSettings().network_scan_threads) limit = asyncio.Semaphore(PyasicSettings().network_scan_threads)
miners = asyncio.as_completed( miners = asyncio.as_completed(
[ [
@@ -183,14 +177,14 @@ class MinerNetwork:
miner = await ping_and_get_miner(ip) miner = await ping_and_get_miner(ip)
if miner: if miner:
return miner return miner
except (ConnectionRefusedError, OSError): except ConnectionRefusedError:
tasks = [ping_and_get_miner(ip, port=port) for port in [4029, 8889]] tasks = [ping_and_get_miner(ip, port=port) for port in [4029, 8889]]
for miner in asyncio.as_completed(tasks): for miner in asyncio.as_completed(tasks):
try: try:
miner = await miner miner = await miner
if miner: if miner:
return miner return miner
except (ConnectionRefusedError, OSError): except ConnectionRefusedError:
pass pass
@@ -213,14 +207,13 @@ async def ping_miner(
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
# ping failed if we time out # ping failed if we time out
continue continue
except ConnectionRefusedError: except (ConnectionRefusedError, OSError):
# handle for other connection errors # handle for other connection errors
logging.debug(f"{str(ip)}: Connection Refused.") logging.debug(f"{str(ip)}: Connection Refused.")
raise ConnectionRefusedError raise ConnectionRefusedError
# ping failed, likely with an exception
except Exception as e: except Exception as e:
logging.warning(f"{str(ip)}: {e}") logging.warning(f"{str(ip)}: Ping And Get Miner Exception: {e}")
continue raise ConnectionRefusedError
return return
@@ -228,8 +221,8 @@ async def ping_and_get_miner(
ip: ipaddress.ip_address, port=4028 ip: ipaddress.ip_address, port=4028
) -> Union[None, AnyMiner]: ) -> Union[None, AnyMiner]:
for i in range(PyasicSettings().network_ping_retries): for i in range(PyasicSettings().network_ping_retries):
connection_fut = asyncio.open_connection(str(ip), port)
try: try:
connection_fut = asyncio.open_connection(str(ip), port)
# get the read and write streams from the connection # get the read and write streams from the connection
reader, writer = await asyncio.wait_for( reader, writer = await asyncio.wait_for(
connection_fut, timeout=PyasicSettings().network_ping_timeout connection_fut, timeout=PyasicSettings().network_ping_timeout
@@ -243,11 +236,10 @@ async def ping_and_get_miner(
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
# ping failed if we time out # ping failed if we time out
continue continue
except ConnectionRefusedError as e: except (ConnectionRefusedError, OSError):
# handle for other connection errors # handle for other connection errors
logging.debug(f"{str(ip)}: Connection Refused.") logging.debug(f"{str(ip)}: Connection Refused.")
raise e raise ConnectionRefusedError
# ping failed, likely with an exception
except Exception as e: except Exception as e:
logging.warning(f"{str(ip)}: Ping And Get Miner Exception: {e}") logging.warning(f"{str(ip)}: Ping And Get Miner Exception: {e}")
raise ConnectionRefusedError raise ConnectionRefusedError