improved send command functionality with a generator and added progress to it

This commit is contained in:
UpstreamData
2022-05-13 14:28:51 -06:00
parent 5a0bafb964
commit 191f1d24b9
2 changed files with 38 additions and 5 deletions

View File

@@ -1,7 +1,10 @@
from miners.miner_factory import MinerFactory
from tools.cfg_util.layout import window
from tools.cfg_util.layout import window, update_prog_bar
from tools.cfg_util.tables import TableManager
from tools.cfg_util.decorators import disable_buttons
from settings import CFG_UTIL_CONFIG_THREADS as COMMAND_THREADS
import asyncio
@disable_buttons("Flashing Lights")
@@ -62,15 +65,44 @@ async def btn_backend(ip_idxs: list):
@disable_buttons("Sending Command")
async def btn_command(ip_idxs: list, command: str):
prog_bar_len = 0
await update_prog_bar(prog_bar_len, len(ip_idxs))
table_manager = TableManager()
_table = window["cmd_table"].Widget
miners = []
iids = _table.get_children()
for idx in ip_idxs:
item = _table.item(iids[idx])
ip = item["values"][0]
miner = await MinerFactory().get_miner(ip)
success = await miner.send_ssh_command(command)
if not isinstance(success, str):
miners.append(miner)
sent = send_command_generator(miners, command)
async for done in sent:
success = done["Status"]
if not isinstance(done["Status"], str):
success = f"Command {command} failed."
table_manager.data[ip]["Output"] = success
table_manager.update_tables()
table_manager.data[done["IP"]]["Output"] = success
prog_bar_len += 1
table_manager.update_tables()
await update_prog_bar(prog_bar_len, len(ip_idxs))
async def send_command_generator(miners: list, command: str):
loop = asyncio.get_event_loop()
command_tasks = []
for miner in miners:
if len(command_tasks) >= COMMAND_THREADS:
cmd_sent = asyncio.as_completed(command_tasks)
command_tasks = []
for done in cmd_sent:
yield await done
command_tasks.append(loop.create_task(_send_ssh_command(miner, command)))
cmd_sent = asyncio.as_completed(command_tasks)
for done in cmd_sent:
yield await done
async def _send_ssh_command(miner, command: str):
proc = await miner.send_ssh_command(command)
return {"IP": miner.ip, "Status": proc}