Skip to content

Commit b6e48de

Browse files
authored
Make --log wait automatically in run (#764)
* Wait for app to start with torchx log Summary: Right now it silently times out after waiting for 10s and then (depending on the scheduler) fails because the job hasn't actually started; I'm trying to use `--log` with the `run` command and inevitably run into this. I wasn't sure if we want to explicitly timeout, or what the best ergonomics would be -- I can increase the timeout to 10 minutes and blow up if it doesn't start in that time, or change it to a while loop (as done here) -- wanted to check :). Differential Revision: D48840617 fbshipit-source-id: 111ec6708e18d83f70a1d946429226f6ab025082 * Make --log wait automatically in run (#764) Summary: Pull Request resolved: #764 I generally used to find it confusing and assume that `--log` was broken before realizing I always need to specify `--wait` to make it work -- I think it would be better to have `--log` automatically wait for the job to finish. Differential Revision: D48874784 fbshipit-source-id: 728325213d695b5077b8b4139e42b763875d8a91
1 parent b9a1d0d commit b6e48de

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

torchx/cli/cmd_log.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ def get_logs(
101101
if len(path) == 4:
102102
replica_ids = [(role_name, int(id)) for id in path[3].split(",") if id]
103103
else:
104-
for i in range(10):
104+
display_waiting = True
105+
while True:
105106
status = runner.status(app_handle)
106107
if status and is_started(status.state):
107108
break
108-
if i == 0:
109-
logger.info("Waiting for app to start before logging...")
109+
elif display_waiting:
110+
logger.info("Waiting for app state response before fetching logs...")
111+
display_waiting = False
110112
time.sleep(1)
111113

112114
app = none_throws(runner.describe(app_handle))

torchx/cli/cmd_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def _run(self, runner: Runner, args: argparse.Namespace) -> None:
223223
app_status = runner.status(app_handle)
224224
if app_status:
225225
logger.info(app_status.format())
226-
if args.wait:
226+
if args.wait or args.log:
227227
self._wait_and_exit(runner, app_handle, log=args.log)
228228

229229
except (ComponentValidationException, ComponentNotFoundException) as e:

torchx/cli/test/cmd_log_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ def log_lines(
6969
return iter([line for line in log_lines if re.match(regex, line)])
7070

7171

72+
class MockPendingRunner(MockRunner):
73+
def __init__(self) -> None:
74+
self._status_checks = 0
75+
76+
def status(self, app_handle: str) -> AppStatus:
77+
if self._status_checks < 2:
78+
self._status_checks += 1
79+
return AppStatus(state=AppState.PENDING)
80+
else:
81+
return super().status(app_handle)
82+
83+
7284
class CmdLogTest(unittest.TestCase):
7385
@patch("sys.exit", side_effect=SentinelError)
7486
def test_cmd_log_bad_job_identifier(self, exit_mock: MagicMock) -> None:
@@ -164,6 +176,15 @@ def test_cmd_log_some_replicas(self, mock_runner: MagicMock) -> None:
164176
set(out.getvalue().split("\n")),
165177
)
166178

179+
@patch(RUNNER, new_callable=MockPendingRunner)
180+
def test_cmd_log_wait_to_start(self, mock_runner: MagicMock) -> None:
181+
out = io.StringIO()
182+
with self.assertLogs(level="INFO") as cm:
183+
get_logs(
184+
out, "local_docker://test-session/SparseNNAppdef/trainer", regex=None
185+
)
186+
self.assertIn("Waiting", "\n".join(cm.output))
187+
167188
@patch(RUNNER, new_callable=MockRunner)
168189
def test_print_log_lines_throws(self, mock_runner: MagicMock) -> None:
169190
# makes sure that when the function executed in the threadpool

0 commit comments

Comments
 (0)