-
Notifications
You must be signed in to change notification settings - Fork 0
Render asynchronous operations with Rich Live and spinners #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """Rich Live rendering for structured background-job progress.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
|
|
||
| from rich.console import Console, Group | ||
| from rich.live import Live | ||
| from rich.progress_bar import ProgressBar | ||
| from rich.spinner import Spinner | ||
| from rich.table import Table | ||
| from rich.text import Text | ||
|
|
||
| from ancestryllm.core.jobs import JobSnapshot, JobState | ||
|
|
||
| _ACTIVE_STATES = frozenset({JobState.QUEUED, JobState.RUNNING}) | ||
| _REFRESHES_PER_SECOND = 8 | ||
|
|
||
|
|
||
| class JobProgressDisplay: | ||
| """Keep active jobs visible without owning job execution or service state.""" | ||
|
|
||
| def __init__(self, console: Console) -> None: | ||
| self.console = console | ||
| self._lock = threading.RLock() | ||
| self._active: dict[str, JobSnapshot] = {} | ||
| self._live: Live | None = None | ||
|
|
||
| @property | ||
| def active(self) -> bool: | ||
| return self._live is not None | ||
|
|
||
| @property | ||
| def renderable(self) -> Table: | ||
| return self._render() | ||
|
|
||
| def handle(self, snapshot: JobSnapshot) -> None: | ||
| with self._lock: | ||
| if snapshot.state in _ACTIVE_STATES: | ||
| self._active[snapshot.job_id] = snapshot | ||
| if self._live is None: | ||
| live = Live( | ||
| self._render(), | ||
| console=self.console, | ||
| auto_refresh=True, | ||
| refresh_per_second=_REFRESHES_PER_SECOND, | ||
| transient=True, | ||
| redirect_stdout=False, | ||
| redirect_stderr=False, | ||
| ) | ||
| live.start(refresh=True) | ||
| self._live = live | ||
| else: | ||
| self._live.update(self._render(), refresh=True) | ||
| return | ||
|
|
||
| self._active.pop(snapshot.job_id, None) | ||
| if self._live is not None and self._active: | ||
| self._live.update(self._render(), refresh=True) | ||
| elif self._live is not None: | ||
| self._stop_live() | ||
| self.console.print(self._summary(snapshot)) | ||
|
|
||
| def _render(self) -> Table: | ||
| table = Table(title="Background jobs", box=None, show_header=True) | ||
| table.add_column("Job") | ||
| table.add_column("Operation") | ||
| table.add_column("Progress") | ||
| for snapshot in sorted(self._active.values(), key=lambda item: item.job_id): | ||
| operation = snapshot.progress.operation if snapshot.progress else snapshot.name | ||
| if snapshot.state is JobState.QUEUED: | ||
| indicator: Spinner | Group = Spinner("dots", text="queued") | ||
| elif snapshot.progress and snapshot.progress.total is not None: | ||
| completed = snapshot.progress.completed or 0 | ||
| total = snapshot.progress.total | ||
| indicator = Group( | ||
| ProgressBar(total=total, completed=completed, width=20), | ||
| Text(f"{completed}/{total}"), | ||
| ) | ||
| else: | ||
| indicator = Spinner("dots", text="working") | ||
| table.add_row(snapshot.job_id, operation, indicator) | ||
| return table | ||
|
|
||
| @staticmethod | ||
| def _summary(snapshot: JobSnapshot) -> Text: | ||
| if snapshot.state is JobState.COMPLETED: | ||
| return Text(f"{snapshot.job_id} completed: {snapshot.name}", style="green") | ||
| if snapshot.state is JobState.CANCELLED: | ||
| return Text(f"{snapshot.job_id} cancelled: {snapshot.name}", style="yellow") | ||
| return Text( | ||
| f"{snapshot.job_id} failed ({snapshot.error_code or 'JOB_FAILED'}): {snapshot.name}", | ||
| style="bold red", | ||
| ) | ||
|
|
||
| def close(self) -> None: | ||
| with self._lock: | ||
| try: | ||
| self._stop_live() | ||
| finally: | ||
| self._active.clear() | ||
|
|
||
| def _stop_live(self) -> None: | ||
| live = self._live | ||
| self._live = None | ||
| if live is not None: | ||
| live.stop() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This final status is written to the same console that
ReplApplicationuses for normal stdout, so any background job that completes while a user is producing--jsonoutput can interleave a plain-textj000001 completed...line into the machine-readable stream. This also affects a background command submitted with--json, where the job response/result shares stdout with the progress summary; progress/status should go to stderr or be suppressed while JSON output is active.Useful? React with 👍 / 👎.