Skip to content
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

[UX] Support --tail parameter for sky logs #4241

Merged
merged 24 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 6 additions & 2 deletions sky/backends/cloud_vm_ray_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3708,18 +3708,22 @@ def tail_logs(self,
handle: CloudVmRayResourceHandle,
job_id: Optional[int],
managed_job_id: Optional[int] = None,
follow: bool = True) -> int:
follow: bool = True,
tail: int = 0) -> int:
"""Tail the logs of a job.

Args:
handle: The handle to the cluster.
job_id: The job ID to tail the logs of.
managed_job_id: The managed job ID for display purpose only.
follow: Whether to follow the logs.
tail: The number of lines to display from the end of the
log file. If 0, print all lines.
"""
code = job_lib.JobLibCodeGen.tail_logs(job_id,
managed_job_id=managed_job_id,
follow=follow)
follow=follow,
tail=tail)
if job_id is None and managed_job_id is None:
logger.info(
'Job ID not provided. Streaming the logs of the latest job.')
Expand Down
9 changes: 8 additions & 1 deletion sky/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,12 @@ def queue(clusters: List[str], skip_finished: bool, all_users: bool):
help=('Follow the logs of a job. '
'If --no-follow is specified, print the log so far and exit. '
'[default: --follow]'))
@click.option(
'--tail',
default=0,
type=int,
help=('The number of lines to display from the end of the log file. '
'Default is 0, which means print all lines.'))
@click.argument('cluster',
required=True,
type=str,
Expand All @@ -2024,6 +2030,7 @@ def logs(
sync_down: bool,
status: bool, # pylint: disable=redefined-outer-name
follow: bool,
tail: int,
):
# NOTE(dev): Keep the docstring consistent between the Python API and CLI.
"""Tail the log of a job.
Expand Down Expand Up @@ -2090,7 +2097,7 @@ def logs(
click.secho(f'Job {id_str}not found', fg='red')
sys.exit(1)

core.tail_logs(cluster, job_id, follow)
core.tail_logs(cluster, job_id, follow, tail)


@cli.command()
Expand Down
5 changes: 3 additions & 2 deletions sky/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,8 @@ def cancel(
@usage_lib.entrypoint
def tail_logs(cluster_name: str,
job_id: Optional[int],
follow: bool = True) -> None:
follow: bool = True,
tail: int = 0) -> None:
# NOTE(dev): Keep the docstring consistent between the Python API and CLI.
"""Tail the logs of a job.

Expand Down Expand Up @@ -775,7 +776,7 @@ def tail_logs(cluster_name: str,
f'{colorama.Style.RESET_ALL}')

usage_lib.record_cluster_name_for_current_operation(cluster_name)
backend.tail_logs(handle, job_id, follow=follow)
backend.tail_logs(handle, job_id, follow=follow, tail=tail)


@usage_lib.entrypoint
Expand Down
6 changes: 4 additions & 2 deletions sky/skylet/job_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,16 @@ def fail_all_jobs_in_progress(cls) -> str:
def tail_logs(cls,
job_id: Optional[int],
managed_job_id: Optional[int],
follow: bool = True) -> str:
follow: bool = True,
tail: int = 0) -> str:
# pylint: disable=line-too-long
code = [
f'job_id = {job_id} if {job_id} != None else job_lib.get_latest_job_id()',
'run_timestamp = job_lib.get_run_timestamp(job_id)',
f'log_dir = None if run_timestamp is None else os.path.join({constants.SKY_LOGS_DIRECTORY!r}, run_timestamp)',
f'log_lib.tail_logs(job_id=job_id, log_dir=log_dir, '
f'managed_job_id={managed_job_id!r}, follow={follow})',
f'managed_job_id={managed_job_id!r}, follow={follow}, '
f'tail={tail})',
zpoint marked this conversation as resolved.
Show resolved Hide resolved
]
return cls._build(code)

Expand Down
21 changes: 19 additions & 2 deletions sky/skylet/log_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

This is a remote utility module that provides logging functionality.
"""
from collections import deque
zpoint marked this conversation as resolved.
Show resolved Hide resolved
import copy
import io
import multiprocessing.pool
Expand Down Expand Up @@ -381,7 +382,8 @@ def _follow_job_logs(file,
def tail_logs(job_id: Optional[int],
log_dir: Optional[str],
managed_job_id: Optional[int] = None,
follow: bool = True) -> None:
follow: bool = True,
tail: int = 0) -> None:
"""Tail the logs of a job.

Args:
Expand All @@ -390,6 +392,8 @@ def tail_logs(job_id: Optional[int],
managed_job_id: The managed job id (for logging info only to avoid
confusion).
follow: Whether to follow the logs or print the logs so far and exit.
tail: The number of lines to display from the end of the log file,
if 0, print all lines.
"""
if job_id is None:
# This only happens when job_lib.get_latest_job_id() returns None,
Expand Down Expand Up @@ -440,6 +444,14 @@ def tail_logs(job_id: Optional[int],
with open(log_path, 'r', newline='', encoding='utf-8') as log_file:
# Using `_follow` instead of `tail -f` to streaming the whole
# log and creating a new process for tail.
if tail > 0:
lines = deque(log_file.readlines(), maxlen=tail)
for line in lines:
print(line, end='')
# Flush the last n lines
print(end='', flush=True)
# Now, the cursor is at the end of the last lines
# if tail_f_lines > 0
zpoint marked this conversation as resolved.
Show resolved Hide resolved
for line in _follow_job_logs(log_file,
job_id=job_id,
start_streaming_at=start_stream_at):
zpoint marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -448,7 +460,12 @@ def tail_logs(job_id: Optional[int],
try:
start_stream = False
with open(log_path, 'r', encoding='utf-8') as f:
for line in f.readlines():
if tail > 0:
lines = deque(f.readlines(), maxlen=tail)
start_stream = True
zpoint marked this conversation as resolved.
Show resolved Hide resolved
else:
lines = f.readlines()
for line in lines:
if start_stream_at in line:
start_stream = True
if start_stream:
Expand Down
Loading