Skip to content

Commit 9ac1b2c

Browse files
committed
tasks: improve task_join()
Now task_join() will report when a task switches from waiting on one task to another. The wait log will also include color-coded status text matching what appears in the table. Signed-off-by: Stephen Brennan <[email protected]>
1 parent ab41f4a commit 9ac1b2c

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

yo/main.py

+36-30
Original file line numberDiff line numberDiff line change
@@ -746,60 +746,65 @@ def task_get_status(
746746
return task_to_status
747747

748748

749+
def _status_code(status: str, code: t.Union[int, str, None]) -> str:
750+
if status == "RUNNING":
751+
return f"[yellow]RUNNING[/yellow] (pid={code})"
752+
elif status == "FAIL":
753+
return f"[red]FAILED[/red] (code={code})"
754+
elif status == "WAITING":
755+
return f"[green]WAITING[/green] (on={code})"
756+
elif status == "UNKNOWN":
757+
return "[red]UNKNOWN[/red]"
758+
else:
759+
return "[green]SUCCESS[/green]"
760+
761+
749762
def task_status_to_table(
750763
statuses: t.Mapping[str, t.Tuple[str, t.Union[int, str]]]
751764
) -> rich.table.Table:
752765
t = rich.table.Table(title="Task Status")
753766
t.add_column("Task")
754767
t.add_column("Status")
755768
for task, (status, code) in statuses.items():
756-
if status == "RUNNING":
757-
status = f"[yellow]RUNNING[/yellow] (pid={code})"
758-
elif status == "FAIL":
759-
status = f"[red]FAILED[/red] (code={code})"
760-
elif status == "WAITING":
761-
status = f"[green]WAITING[/green] (on={code})"
762-
elif status == "UNKNOWN":
763-
status = "[red]UNKNOWN[/red]"
764-
else:
765-
status = f"[green]SUCCESS[/green] (code={code})"
766-
t.add_row(task, status)
769+
t.add_row(task, _status_code(status, code))
767770
return t
768771

769772

770773
def task_join(
771774
ctx: YoCtx,
772775
inst: YoInstance,
773-
wait_task: t.Optional[str] = None,
776+
wait_tasks: t.Iterable[str] = (),
774777
) -> t.Mapping[str, t.Tuple[str, t.Union[str, int]]]:
775778
with Live(console=ctx.con) as live:
776-
task_previous_status: t.Dict[str, str] = {}
779+
task_previous_status: t.Dict[str, t.Tuple[str, t.Union[int, str]]] = {}
777780
while True:
778781
status_dict = task_get_status(ctx, inst)
779782
live.update(task_status_to_table(status_dict))
780783
any_running = False
781-
for task, (status, _) in status_dict.items():
782-
prev_status = task_previous_status.get(task)
783-
task_previous_status[task] = status
784+
for task, (status, code) in status_dict.items():
785+
prev_status, prev_code = task_previous_status.get(
786+
task, (None, None)
787+
)
788+
task_previous_status[task] = status, code
784789
if not prev_status:
785-
live.console.log(f"{task}: starting in status {status}")
786-
elif prev_status != status:
787790
live.console.log(
788-
f"{task}: changing status {prev_status} -> {status}"
791+
f"{task}: starting in status {_status_code(status, code)}",
792+
highlight=False,
793+
)
794+
elif prev_status != status or prev_code != code:
795+
live.console.log(
796+
f"{task}: changing status {_status_code(prev_status, prev_code)} -> {_status_code(status, code)}",
797+
highlight=False,
789798
)
790799
if status in ("RUNNING", "WAITING"):
791800
any_running = True
792801

793802
# If we're waiting for no particular task, then we have to wait
794803
# until all are completed. Otherwise, we wait until just the
795-
# specific wait_task is completed.
796-
can_terminate = (wait_task is None and not any_running) or (
797-
wait_task is not None
798-
and status_dict[wait_task][0]
799-
not in (
800-
"RUNNING",
801-
"WAITING",
802-
)
804+
# specific wait_tasks are all completed.
805+
can_terminate = (not wait_tasks and not any_running) or all(
806+
(status_dict[wt][0] not in ("RUNNING", "WAITING"))
807+
for wt in wait_tasks
803808
)
804809
if can_terminate:
805810
break
@@ -1717,6 +1722,7 @@ def add_args(self, parser: argparse.ArgumentParser) -> None:
17171722
"task",
17181723
choices=arg_choices(list_tasks()),
17191724
help="name of the task to execute",
1725+
nargs="+",
17201726
)
17211727
parser.add_argument(
17221728
"-w",
@@ -1726,9 +1732,9 @@ def add_args(self, parser: argparse.ArgumentParser) -> None:
17261732
)
17271733

17281734
def run_for_instance(self, inst: YoInstance) -> None:
1729-
run_all_tasks(self.c, inst, [self.args.task])
1735+
run_all_tasks(self.c, inst, self.args.task)
17301736
if self.args.wait:
1731-
task_join(self.c, inst, wait_task=self.args.task)
1737+
task_join(self.c, inst, wait_tasks=self.args.task)
17321738
send_notification(
17331739
self.c,
17341740
f"Task {self.args.task} complete on instance {inst.name}",
@@ -1802,7 +1808,7 @@ def add_args(self, parser: argparse.ArgumentParser) -> None:
18021808
)
18031809

18041810
def run_for_instance(self, inst: YoInstance) -> None:
1805-
task_join(self.c, inst, wait_task=self.args.task)
1811+
task_join(self.c, inst, wait_tasks=[self.args.task])
18061812
send_notification(
18071813
self.c, f"Task {self.args.task} complete on instance {inst.name}"
18081814
)

0 commit comments

Comments
 (0)