Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,15 @@ project uses [semantic versioning][semver].
exactly, because there the byte is the useful fact; a run of them is bounded
by the first and the last, because there it is not.

- **Progress lines when stderr is not a terminal.** A background job, CI run
or redirected log used to print nothing until the final summary, so a
six-hour `offloader report` was indistinguishable from a hung one. Off a TTY,
`offload`, `report` and `verify` now write plain newline-terminated lines
(percent of job bytes, files done/total, file name, throughput and ETA): one
when a file starts, one every 30 s (including partway through a single large
file, via a new `on_bytes` callback on `hash_file`), and one on completion. The single-line
bar on a terminal is unchanged and `--quiet` still silences both.

### Fixed

Found by fuzzing the edges: `tests/test_fuzz_edges.py` for what a card can
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pip install -e ".[dev]"
`ffmpeg` and `ffprobe` on `PATH` are optional — the suite runs without them.

```sh
pytest # 916 tests, about 20s
pytest # 1308 tests, about 20s
pytest --fuzz # property tests at 3000 examples each, about 3 min
ruff check src tests
pytest --cov=offloader --cov-report=term-missing
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,13 @@ Full verification only when every present destination was hashed.

```sh
pip install -e ".[dev]"
pytest # 916 tests, ~20s
pytest # 1308 tests, ~20s
pytest --fuzz # same suite, 3000 examples per property (~3 min)
ruff check src tests
pytest --cov=offloader --cov-report=term-missing
```

916 tests at 89% line coverage. They cover formatting against the reference's
1308 tests at 88% line coverage. They cover formatting against the reference's
exact strings, checksum vectors and streaming equivalence, copy/verify
behaviour including simulated destination corruption, pause/resume/cancel
concurrency, retry discrimination, BRAW container parsing, ffprobe parsing,
Expand Down
108 changes: 97 additions & 11 deletions src/offloader/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,46 @@
EXIT_UNRESOLVED = EXIT_INCOMPLETE


#: Seconds between periodic progress lines when stderr is not a terminal.
LOG_INTERVAL = 30.0
#: Minimum seconds between "file started" lines, so a card of thousands of
#: small clips cannot flood a log.
LOG_START_GAP = 1.0


class _Progress:
"""Single-line progress on stderr; silent when not a TTY."""
"""Progress on stderr: a single redrawn line on a TTY, plain lines otherwise.

A background job or CI run has no terminal to redraw, and a six-hour run
that logs nothing until its summary is indistinguishable from a hung one.
So off a TTY it writes newline-terminated lines, throttled to stay readable.
`--quiet` silences both.
"""

def __init__(self, enabled: bool) -> None:
self.enabled = enabled and sys.stderr.isatty()
self.enabled = enabled
self.tty = sys.stderr.isatty()
self._last = 0.0
self._width = 0
self._started = time.monotonic()
self._last_start = -LOG_START_GAP
self._last_log = self._started
self._key: tuple[int, str] | None = None
self._logged_final = False

def __call__(self, event: engine.ProgressEvent) -> None:
if not self.enabled:
return
now = time.monotonic()
final = event.job_bytes_done >= event.job_bytes_total
if not self.tty:
self._log(event, now, final)
return
if now - self._last < 0.1 and not final:
return
self._last = now

pct = (event.job_bytes_done / event.job_bytes_total * 100
if event.job_bytes_total else 100.0)
pct = self._percent(event)
line = (f" [{pct:5.1f}%] {event.stage:<6} "
f"{event.file_index + 1}/{event.file_total} {event.file_name}")
line = line[:110]
Expand All @@ -66,8 +87,40 @@ def __call__(self, event: engine.ProgressEvent) -> None:
sys.stderr.write("\r" + line + " " * pad)
sys.stderr.flush()

@staticmethod
def _percent(event: engine.ProgressEvent) -> float:
return (event.job_bytes_done / event.job_bytes_total * 100
if event.job_bytes_total else 100.0)

def _log(self, event: engine.ProgressEvent, now: float, final: bool) -> None:
key = (event.file_index, event.stage)
new_file = key != self._key
self._key = key
due = now - self._last_log >= LOG_INTERVAL
started = new_file and now - self._last_start >= LOG_START_GAP
finished = final and not self._logged_final
if not (due or started or finished):
return
Comment on lines +99 to +103

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Connect periodic logging to progress during hashing

LOG_INTERVAL is checked only when a progress callback arrives. engine.rescan() calls progress once before hash_file() for each file, and hash_file() emits no callbacks while reading. verify_manifest() and the full-verification read in engine.run() have the same gap. A large camera clip that takes minutes to hash therefore still leaves the log silent for that entire read, which is the long-running background workflow this change is intended to make observable.

I reproduced this through the real report command with four checksum reads and a clock advancing 31 seconds per read: stderr contained only [ 0.0%] verify 1/1 clip.mov from start through completion, with no periodic update. rescan() also never emits its final byte count, so this path produces no completion progress line either. The standalone verify command likewise emitted no additional lines during the 124 simulated seconds.

Pass byte-progress callbacks through the hashing loop, or provide a timer-based heartbeat, and publish completed progress when the operation finishes. Add an integration test that advances beyond LOG_INTERVAL while a single file is still being hashed; the current tests manually invoke progress again or only check that some output exists.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: hash_file now takes an optional on_bytes callback (called per chunk, default None), wired into rescan, the full-verify read in run() and verify_manifest, so periodic lines fire mid-file. rescan also emits its final count, so report logs a 100% line. Tests advance a fake clock per chunk and fail without the change.

if new_file:
self._last_start = now
self._last_log = now
self._logged_final = self._logged_final or final

pct = self._percent(event)
parts = [f"[{pct:5.1f}%] {event.stage} "
f"{event.file_index + 1}/{event.file_total} {event.file_name}"]
elapsed = now - self._started
if event.job_bytes_done and elapsed > 0:
rate = event.job_bytes_done / elapsed
parts.append(f"{format_size(int(rate))}/s")
remaining = event.job_bytes_total - event.job_bytes_done
if remaining > 0:
parts.append(f"ETA {format_elapsed(remaining / rate)}")
sys.stderr.write(" ".join(parts) + "\n")
sys.stderr.flush()

def done(self) -> None:
if self.enabled and self._width:
if self.enabled and self.tty and self._width:
sys.stderr.write("\r" + " " * self._width + "\r")
sys.stderr.flush()
self._width = 0
Expand Down Expand Up @@ -629,26 +682,59 @@ def cmd_verify(args: argparse.Namespace) -> int:
print(f"error: no .mhl manifest found under {target}", file=sys.stderr)
return 2

show = not args.quiet and sys.stderr.isatty()
show = not args.quiet
tty = sys.stderr.isatty()
last_log = time.monotonic()
last_start = -LOG_START_GAP
started_index = -1

def progress(index: int, total: int, path: Path) -> None:
if show:
if show and tty:
sys.stderr.write('\r' + f" [{index + 1}/{total}] {path.name[:70]:<70}")
sys.stderr.flush()

def log_bytes(index: int, total: int, path: Path, done: int, size: int) -> None:
"""Off a TTY: a line as each file starts and one per LOG_INTERVAL after.

Percent counts files completed plus the share of this one read so far,
and stops short of 100 until the last hash has returned (the manifest's
own completion line below says so).
"""
nonlocal last_log, last_start, started_index
if not show or tty:
return
now = time.monotonic()
new_file = index != started_index
started_index = index
due = now - last_log >= LOG_INTERVAL
if not (due or (new_file and now - last_start >= LOG_START_GAP)):
return
if new_file:
last_start = now
last_log = now
share = done / size if size else 0.0
pct = min((index + share) / total * 100, 99.9) if total else 0.0
sys.stderr.write(f"[{pct:5.1f}%] verify {index + 1}/{total} {path.name}\n")
sys.stderr.flush()

worst = 0
reports = []
for manifest in manifests:
try:
reports.append(verify_mod.verify_manifest(
manifest, progress=progress, bypass_cache=not args.allow_cache,
confine_to=confine_to))
report = verify_mod.verify_manifest(
manifest, progress=progress, bytes_progress=log_bytes,
bypass_cache=not args.allow_cache, confine_to=confine_to)
reports.append(report)
except Exception as exc:
print(f"error: could not read {manifest}: {exc}", file=sys.stderr)
worst = max(worst, 2)
continue
if show:
if show and tty:
sys.stderr.write('\r' + " " * 90 + '\r')
elif show and report.verdicts:
sys.stderr.write(f"[100.0%] verify {len(report.verdicts)}/"
f"{len(report.verdicts)} {manifest.name} done\n")
sys.stderr.flush()

# Every manifest is checked before any is reported, so a rerun's manifest
# can answer the entries an earlier failed run could not hash. Only that
Expand Down
23 changes: 20 additions & 3 deletions src/offloader/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1696,9 +1696,15 @@ def copy_once(_src=source, _partials=partials, _idx=index,
# the job before promotion. JobCancelled is not an
# OSError, so retry_mod.call lets it propagate.
dst_sum, verify_attempts = retry_mod.call(
lambda p=partial: hash_file(
lambda p=partial, _i=index, _s=source, _z=stat.st_size:
hash_file(
p, options.algorithm,
checkpoint=control.checkpoint if control else None),
checkpoint=control.checkpoint if control else None,
on_bytes=lambda n: emit(ProgressEvent(
_i, len(files), _s.name, "verify",
n, _z,
counters.job_bytes_done,
counters.job_bytes_total))),
options.retry,
sleep=_sleeper(control),
)
Expand Down Expand Up @@ -2097,7 +2103,12 @@ def rescan(source_root: Path, destination_roots: Sequence[Path],
0, stat.st_size, done, total))
if algorithm.factory is not None:
try:
entry.checksum = hash_file(source, options.algorithm)
entry.checksum = hash_file(
source, options.algorithm,
on_bytes=lambda n, _i=index, _s=source, _z=stat.st_size, _d=done:
progress(ProgressEvent(_i, len(files), _s.name, "verify",
n, _z, _d + n, total))
if progress else None)
except OSError:
entry.checksum = None
done += stat.st_size
Expand Down Expand Up @@ -2162,6 +2173,12 @@ def rescan(source_root: Path, destination_roots: Sequence[Path],
f"{source.name}: metadata could not be read ({exc})")
job.files.append(entry)

if progress and files:
# The last per-file event fires before that file is hashed, so say
# outright that the job is complete.
progress(ProgressEvent(len(files) - 1, len(files), files[-1].name, "verify",
stat.st_size, stat.st_size, total, total))

# Claim a verification level only for work this call did. Anything that
# was not read back (no hash algorithm, or the source standing in as its
# own destination) leaves the job at NONE.
Expand Down
10 changes: 9 additions & 1 deletion src/offloader/hashers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ def new_hasher(key: str) -> Hasher:
return get_algorithm(key).new() or _NullHasher()


def hash_file(path: Path, key: str, chunk_size: int = 8 << 20, *,
def hash_file(path: Path, key: str, chunk_size: int = 8 << 20,
on_bytes: Callable[[int], None] | None = None, *,
checkpoint: Callable[[], None] | None = None) -> str:
"""Hash a file off disk. Used by full verification to re-read a
destination, and by the `report` and `verify` commands to reconstruct or
re-check checksums without copying.

`on_bytes`, if given, is called with the running byte count after each
chunk, so a caller can show progress inside one very large file.

`checkpoint`, when given, is called before every chunk read. The engine
passes `JobControl.checkpoint` so a long reread blocks on pause and raises
on cancel; whatever it raises propagates and the file is left unhashed.
Expand All @@ -153,6 +157,7 @@ def hash_file(path: Path, key: str, chunk_size: int = 8 << 20, *,
raise ValueError(f"chunk_size must be positive, got {chunk_size}")

hasher = new_hasher(key)
read = 0
with open_binary(path, "rb") as handle:
while True:
if checkpoint is not None:
Expand All @@ -161,6 +166,9 @@ def hash_file(path: Path, key: str, chunk_size: int = 8 << 20, *,
if not chunk:
break
hasher.update(chunk)
read += len(chunk)
if on_bytes:
on_bytes(read)
return hasher.hexdigest()


Expand Down
10 changes: 9 additions & 1 deletion src/offloader/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def verify_manifest(
manifest: Path,
*,
progress: Callable[[int, int, Path], None] | None = None,
bytes_progress: Callable[[int, int, Path, int, int], None] | None = None,
bypass_cache: bool = True,
find_unlisted: bool = True,
check_directories: bool = True,
Expand All @@ -382,6 +383,9 @@ def verify_manifest(
`bypass_cache` evicts each file before reading it, so a freshly written tree
is read off the device rather than out of memory.

`progress` fires as each entry starts; `bytes_progress` (index, total, path,
bytes read, file size) fires after each chunk read within the entry.

`check_directories` recomputes an ASC MHL manifest's directory hashes from
the files the manifest lists, mirroring the writer, whose directory hashes
and root hash cover only the job's own files. Files the manifest does not
Expand Down Expand Up @@ -535,7 +539,11 @@ def _evict(path: Path) -> None:
actual_size = longpath.stat(path).st_size
if bypass_cache:
_evict(path)
actual = hash_file(path, algorithm_key)
actual = hash_file(
path, algorithm_key,
on_bytes=(lambda n, _i=index, _p=path, _z=actual_size:
bytes_progress(_i, len(entries), _p, n, _z))
if bytes_progress else None)
except OSError as exc:
note(entry, None)
report.verdicts.append(
Expand Down
Loading
Loading