vastly improved scanning in web monitor

This commit is contained in:
UpstreamData
2022-05-18 11:12:38 -06:00
parent c3e285a9ee
commit 8ebcbd3c33
2 changed files with 52 additions and 17 deletions

View File

@@ -133,7 +133,7 @@ class MinerNetwork:
yield await miner
# add the ping to the list of tasks if we dont scan
scan_tasks.append(loop.create_task(self.ping_miner(host)))
scan_tasks.append(loop.create_task(self.ping_and_get_miner(host)))
# do one last scan at the end to close out the list
scanned = asyncio.as_completed(scan_tasks)
@@ -144,6 +144,12 @@ class MinerNetwork:
async def ping_miner(ip: ipaddress.ip_address) -> None or ipaddress.ip_address:
return await ping_miner(ip)
@staticmethod
async def ping_and_get_miner(
ip: ipaddress.ip_address,
) -> None or ipaddress.ip_address:
return await ping_and_get_miner(ip)
async def ping_miner(
ip: ipaddress.ip_address, port=4028
@@ -172,3 +178,32 @@ async def ping_miner(
logging.warning(f"{str(ip)}: {e}")
continue
return
async def ping_and_get_miner(
ip: ipaddress.ip_address, port=4028
) -> None or ipaddress.ip_address:
for i in range(PING_RETRIES):
connection_fut = asyncio.open_connection(str(ip), port)
try:
# get the read and write streams from the connection
reader, writer = await asyncio.wait_for(
connection_fut, timeout=PING_TIMEOUT
)
# immediately close connection, we know connection happened
writer.close()
# make sure the writer is closed
await writer.wait_closed()
# ping was successful
return await MinerFactory().get_miner(ip)
except asyncio.exceptions.TimeoutError:
# ping failed if we time out
continue
except ConnectionRefusedError:
# handle for other connection errors
logging.debug(f"{str(ip)}: Connection Refused.")
# ping failed, likely with an exception
except Exception as e:
logging.warning(f"{str(ip)}: {e}")
continue
return

View File

@@ -4,6 +4,7 @@ from fastapi import WebSocket
from network import MinerNetwork
from tools.web_monitor.func import get_current_miner_list
import ipaddress
from miners.miner_factory import MinerFactory
@@ -16,24 +17,23 @@ async def do_websocket_scan(websocket: WebSocket, network_ip: str):
else:
network = MinerNetwork(network_ip)
miner_generator = network.scan_network_generator()
miners = []
async for miner_ip in miner_generator:
if miner_ip and str(miner_ip) not in cur_miners:
miners.append(miner_ip)
get_miner_generator = MinerFactory().get_miner_generator(miners)
all_miners = []
async for found_miner in get_miner_generator:
all_miners.append(
{"ip": found_miner.ip, "model": await found_miner.get_model()}
)
all_miners.sort(key=lambda x: x["ip"])
send_miners = []
for miner_ip in all_miners:
send_miners.append(
{"ip": str(miner_ip["ip"]), "model": miner_ip["model"]}
async for found_miner in miner_generator:
if found_miner and str(found_miner.ip) not in cur_miners:
# all_miners.append(miner_ip)
# get_miner_generator = MinerFactory().get_miner_generator(miners)
# async for found_miner in get_miner_generator:
all_miners.append(
{"ip": found_miner.ip, "model": await found_miner.get_model()}
)
await websocket.send_json(send_miners)
all_miners.sort(key=lambda x: ipaddress.ip_address(x["ip"]))
send_miners = []
for miner_ip in all_miners:
send_miners.append(
{"ip": str(miner_ip["ip"]), "model": miner_ip["model"]}
)
await websocket.send_json(send_miners)
await websocket.send_text("Done")
except asyncio.CancelledError:
raise