Skip to content

Commit

Permalink
Fix stream_logs Duplicate Job Handling and TypeError (skypilot-org#…
Browse files Browse the repository at this point in the history
…4274)

fix: multiple `job_id`
  • Loading branch information
andylizf authored Nov 7, 2024
1 parent f013073 commit bcf40fa
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions sky/jobs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import textwrap
import time
import typing
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Set, Tuple, Union

import colorama
import filelock
Expand Down Expand Up @@ -487,23 +487,30 @@ def stream_logs(job_id: Optional[int],
job_id = managed_job_state.get_latest_job_id()
if job_id is None:
return 'No managed job found.'

if controller:
if job_id is None:
assert job_name is not None
managed_jobs = managed_job_state.get_managed_jobs()
# We manually filter the jobs by name, instead of using
# get_nonterminal_job_ids_by_name, as with `controller=True`, we
# should be able to show the logs for jobs in terminal states.
managed_jobs = list(
filter(lambda job: job['job_name'] == job_name, managed_jobs))
if len(managed_jobs) == 0:
managed_job_ids: Set[int] = {
job['job_id']
for job in managed_jobs
if job['job_name'] == job_name
}
if len(managed_job_ids) == 0:
return f'No managed job found with name {job_name!r}.'
if len(managed_jobs) > 1:
job_ids_str = ', '.join(job['job_id'] for job in managed_jobs)
raise ValueError(
f'Multiple managed jobs found with name {job_name!r} (Job '
f'IDs: {job_ids_str}). Please specify the job_id instead.')
job_id = managed_jobs[0]['job_id']
if len(managed_job_ids) > 1:
job_ids_str = ', '.join(
str(job_id) for job_id in managed_job_ids)
with ux_utils.print_exception_no_traceback():
raise ValueError(
f'Multiple managed jobs found with name {job_name!r} '
f'(Job IDs: {job_ids_str}). Please specify the job_id '
'instead.')
job_id = managed_job_ids.pop()
assert job_id is not None, (job_id, job_name)
# TODO: keep the following code sync with
# job_lib.JobLibCodeGen.tail_logs, we do not directly call that function
Expand Down Expand Up @@ -849,6 +856,7 @@ def stream_logs(cls,
from sky.skylet import job_lib, log_lib
from sky.skylet import constants
from sky.utils import ux_utils
try:
from sky.jobs.utils import stream_logs_by_id
except ImportError:
Expand Down

0 comments on commit bcf40fa

Please sign in to comment.