diff --git a/src/huggingface_hub/cli/jobs.py b/src/huggingface_hub/cli/jobs.py index 331f159f5d..cab7e4dcad 100644 --- a/src/huggingface_hub/cli/jobs.py +++ b/src/huggingface_hub/cli/jobs.py @@ -856,9 +856,13 @@ def jobs_logs( logs = api.fetch_job_logs(job_id=job_id, namespace=namespace, follow=follow, tail=tail) for log in logs: out.text(log) + job_ref = f"{namespace}/{job_id}" if namespace else job_id if follow: - job_ref = f"{namespace}/{job_id}" if namespace else job_id out.hint(f"Stream ended. Run `hf jobs inspect {job_ref}` to check the final status (e.g. COMPLETED or ERROR).") + else: + out.hint( + f"If the job is still running, use `hf jobs logs -f {job_ref}` to stream new logs until it completes." + ) def _clear_line(n: int) -> None: diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index a31f8494b5..7164642377 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -24,7 +24,8 @@ from collections.abc import Callable, Iterable, Iterator from concurrent.futures import Future, ThreadPoolExecutor from dataclasses import asdict, dataclass, field -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone +from email.utils import parsedate_to_datetime from functools import wraps from itertools import islice from pathlib import Path @@ -8454,6 +8455,10 @@ def _stream_sse_events( params=params, ) as response: if response.status_code == 200: + # no-follow: stop at the first event logged after the server's Date (1s resolution) + replay_end = None + if not follow and (date := response.headers.get("date")): + replay_end = parsedate_to_datetime(date) + timedelta(seconds=1) event_idx = -1 for line in response.iter_lines(): if line and line.startswith("data: {"): @@ -8461,7 +8466,14 @@ def _stream_sse_events( if event_idx >= start_event_idx: if skip_previous_events_on_retry: start_event_idx += 1 - yield json.loads(line[len("data: ") :]) + event = json.loads(line[len("data: ") :]) + if ( + replay_end + and (ts := event.get("timestamp")) + and parse_datetime(ts) > replay_end + ): + break + yield event break elif response.status_code not in tolerated_status_codes: hf_raise_for_status(response)