vastly improved scanning in web monitor
This commit is contained in:
@@ -133,7 +133,7 @@ class MinerNetwork:
|
|||||||
yield await miner
|
yield await miner
|
||||||
|
|
||||||
# add the ping to the list of tasks if we dont scan
|
# 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
|
# do one last scan at the end to close out the list
|
||||||
scanned = asyncio.as_completed(scan_tasks)
|
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:
|
async def ping_miner(ip: ipaddress.ip_address) -> None or ipaddress.ip_address:
|
||||||
return await ping_miner(ip)
|
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(
|
async def ping_miner(
|
||||||
ip: ipaddress.ip_address, port=4028
|
ip: ipaddress.ip_address, port=4028
|
||||||
@@ -172,3 +178,32 @@ async def ping_miner(
|
|||||||
logging.warning(f"{str(ip)}: {e}")
|
logging.warning(f"{str(ip)}: {e}")
|
||||||
continue
|
continue
|
||||||
return
|
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
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from fastapi import WebSocket
|
|||||||
|
|
||||||
from network import MinerNetwork
|
from network import MinerNetwork
|
||||||
from tools.web_monitor.func import get_current_miner_list
|
from tools.web_monitor.func import get_current_miner_list
|
||||||
|
import ipaddress
|
||||||
from miners.miner_factory import MinerFactory
|
from miners.miner_factory import MinerFactory
|
||||||
|
|
||||||
|
|
||||||
@@ -16,24 +17,23 @@ async def do_websocket_scan(websocket: WebSocket, network_ip: str):
|
|||||||
else:
|
else:
|
||||||
network = MinerNetwork(network_ip)
|
network = MinerNetwork(network_ip)
|
||||||
miner_generator = network.scan_network_generator()
|
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 = []
|
all_miners = []
|
||||||
async for found_miner in get_miner_generator:
|
async for found_miner in miner_generator:
|
||||||
all_miners.append(
|
if found_miner and str(found_miner.ip) not in cur_miners:
|
||||||
{"ip": found_miner.ip, "model": await found_miner.get_model()}
|
# all_miners.append(miner_ip)
|
||||||
)
|
|
||||||
all_miners.sort(key=lambda x: x["ip"])
|
# get_miner_generator = MinerFactory().get_miner_generator(miners)
|
||||||
send_miners = []
|
# async for found_miner in get_miner_generator:
|
||||||
for miner_ip in all_miners:
|
all_miners.append(
|
||||||
send_miners.append(
|
{"ip": found_miner.ip, "model": await found_miner.get_model()}
|
||||||
{"ip": str(miner_ip["ip"]), "model": miner_ip["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")
|
await websocket.send_text("Done")
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
raise
|
raise
|
||||||
|
|||||||
Reference in New Issue
Block a user