fix progress bars

This commit is contained in:
Nick Sweeting
2025-12-31 01:54:00 -08:00
parent cb97f6651b
commit d5c0c64dcd
4 changed files with 109 additions and 11 deletions

View File

@@ -265,13 +265,60 @@ class Orchestrator:
def runloop(self) -> None:
"""Main orchestrator loop."""
from rich.live import Live
from rich.table import Table
from rich.console import Group
from archivebox.misc.logging import IS_TTY, CONSOLE
self.on_startup()
# Enable progress bars only in TTY + foreground mode
show_progress = IS_TTY and self.exit_on_idle
def make_progress_table():
"""Generate progress table for active snapshots."""
from archivebox.core.models import Snapshot
table = Table(show_header=False, show_edge=False, pad_edge=False, box=None)
table.add_column("URL", style="cyan", no_wrap=False)
table.add_column("Progress", width=42)
table.add_column("Percent", justify="right", width=6)
active_snapshots = Snapshot.objects.filter(status='started').iterator(chunk_size=100)
for snapshot in active_snapshots:
total = snapshot.archiveresult_set.count()
if total == 0:
continue
completed = snapshot.archiveresult_set.filter(
status__in=['succeeded', 'skipped', 'failed']
).count()
percentage = (completed / total) * 100
bar_width = 40
filled = int(bar_width * completed / total)
bar = '' * filled + '' * (bar_width - filled)
url = snapshot.url[:60] + '...' if len(snapshot.url) > 60 else snapshot.url
table.add_row(url, bar, f"{percentage:>3.0f}%")
return table
live = Live(make_progress_table(), console=CONSOLE, refresh_per_second=4, transient=False) if show_progress else None
try:
if live:
live.start()
while True:
# Check queues and spawn workers
queue_sizes = self.check_queues_and_spawn_workers()
# Update progress display
if live:
live.update(make_progress_table())
# Track idle state
if self.has_pending_work(queue_sizes) or self.has_running_workers():
self.idle_count = 0
@@ -279,7 +326,7 @@ class Orchestrator:
else:
self.idle_count += 1
self.on_idle()
# Check if we should exit
if self.should_exit(queue_sizes):
log_worker_event(
@@ -289,9 +336,9 @@ class Orchestrator:
pid=self.pid,
)
break
time.sleep(self.POLL_INTERVAL)
except KeyboardInterrupt:
print() # Newline after ^C
except BaseException as e:
@@ -299,6 +346,9 @@ class Orchestrator:
raise
else:
self.on_shutdown()
finally:
if live:
live.stop()
def start(self) -> int:
"""