Rework task output bar operation

Allows sending constant updates including changes to the message within
the same task.
This commit is contained in:
Joshua Boniface 2024-09-28 10:48:39 -04:00
parent 792d135950
commit 0d533f3658
1 changed files with 28 additions and 17 deletions

View File

@ -19,6 +19,8 @@
# #
############################################################################### ###############################################################################
import sys
from click import progressbar from click import progressbar
from time import sleep, time from time import sleep, time
@ -115,30 +117,39 @@ def wait_for_celery_task(CLI_CONFIG, task_detail, start_late=False):
) )
while True: while True:
sleep(0.5) sleep(0.5)
task_status = pvc.lib.common.task_status(
CLI_CONFIG, task_id=task_id, is_watching=True
)
if isinstance(task_status, tuple): if isinstance(task_status, tuple):
continue continue
if task_status.get("state") != "RUNNING": if task_status.get("state") != "RUNNING":
break break
if task_status.get("current") > last_task: if task_status.get("current") == 0:
continue
current_task = int(task_status.get("current")) current_task = int(task_status.get("current"))
total_task = int(task_status.get("total")) total_task = int(task_status.get("total"))
bar.length = total_task bar.length = total_task
if current_task > last_task:
bar.update(current_task - last_task) bar.update(current_task - last_task)
last_task = current_task last_task = current_task
# The extensive spaces at the end cause this to overwrite longer previous messages
curlen = len(str(task_status.get("status"))) curlen = len(str(task_status.get("status")))
if curlen > maxlen: if curlen > maxlen:
maxlen = curlen maxlen = curlen
lendiff = maxlen - curlen lendiff = maxlen - curlen
overwrite_whitespace = " " * lendiff overwrite_whitespace = " " * lendiff
echo(
CLI_CONFIG, percent_complete = (current_task / total_task) * 100
" " + task_status.get("status") + overwrite_whitespace, bar_output = f"[{bar.format_bar()}] {percent_complete:3.0f}%"
newline=False, sys.stdout.write(
) f"\r {bar_output} {task_status['status']}{overwrite_whitespace}"
task_status = pvc.lib.common.task_status(
CLI_CONFIG, task_id=task_id, is_watching=True
) )
sys.stdout.flush()
if task_status.get("state") == "SUCCESS": if task_status.get("state") == "SUCCESS":
bar.update(total_task - last_task) bar.update(total_task - last_task)