Skip to content

Commit

Permalink
Emit CommandProgress event
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Jan 2, 2024
1 parent b89b132 commit b3b5439
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions yapapi/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ class CommandStdOut(CommandEvent):
class CommandStdErr(CommandEvent):
output: str

@attr.s(auto_attribs=True, repr=False)
class CommandProgress(CommandEvent):
message: str

@attr.s(auto_attribs=True, repr=False)
class TaskAccepted(TaskEvent):
Expand Down
1 change: 1 addition & 0 deletions yapapi/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def enable_default_logger(
events.CommandStdOut: "Command stdout",
events.CommandStdErr: "Command stderr",
events.CommandExecuted: "Script command executed",
events.CommandProgress: "Script command progress update",
events.GettingResults: "Getting script results",
events.ScriptFinished: "Script finished",
events.TaskAccepted: "Task accepted",
Expand Down
15 changes: 13 additions & 2 deletions yapapi/rest/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ async def __aiter__(self) -> AsyncIterator[CommandEventData]:
results = await self._get_results(timeout=min(timeout, 5))

any_new: bool = False
results = results[last_idx:]
for result in results:
for result in results[last_idx:]:
any_new = True
assert last_idx == result.index, f"Expected {last_idx}, got {result.index}"

Expand All @@ -300,6 +299,18 @@ async def __aiter__(self) -> AsyncIterator[CommandEventData]:
last_idx = result.index + 1
if result.is_batch_finished:
break

current_idx = last_idx - 1
if current_idx >= 0:
current = results[current_idx]

if current.message is not None:
kwargs = dict(
cmd_idx=current.index,
message=current.message,
)
yield events.CommandProgress, kwargs

if not any_new:
delay = min(3, max(0, self.seconds_left()))
await asyncio.sleep(delay)
Expand Down

0 comments on commit b3b5439

Please sign in to comment.