Skip to content

Commit f05f2a9

Browse files
chrisjwiebefacebook-github-bot
authored andcommitted
torch/runner: rename stop command to cancel (#501)
Summary: Pull Request resolved: #501 See #497 * added cancel command to be consistent with CLI and scheduler API * updated CLI cancel command to call runner.cancel() * updated unit tests * added warning message when using runner.stop() Reviewed By: d4l3k Differential Revision: D36620537 fbshipit-source-id: 8bb5415d3021d21a91cbd21458126e030bc162c2
1 parent f80dd20 commit f05f2a9

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

torchx/cli/cmd_cancel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ def run(self, args: argparse.Namespace) -> None:
2727
app_handle = args.app_handle
2828
_, session_name, _ = api.parse_app_handle(app_handle)
2929
runner = get_runner(name=session_name)
30-
runner.stop(app_handle)
30+
runner.cancel(app_handle)

torchx/cli/test/cmd_cancel_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414

1515
class CmdCancelTest(unittest.TestCase):
16-
@patch("torchx.runner.api.Runner.stop")
17-
def test_run(self, stop: MagicMock) -> None:
16+
@patch("torchx.runner.api.Runner.cancel")
17+
def test_run(self, cancel: MagicMock) -> None:
1818
parser = argparse.ArgumentParser()
1919
cmd_runopts = CmdCancel()
2020
cmd_runopts.add_arguments(parser)
2121

2222
args = parser.parse_args(["foo://session/id"])
2323
cmd_runopts.run(args)
2424

25-
self.assertEqual(stop.call_count, 1)
26-
stop.assert_called_with("foo://session/id")
25+
self.assertEqual(cancel.call_count, 1)
26+
cancel.assert_called_with("foo://session/id")

torchx/runner/api.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def list(self) -> Dict[AppHandle, AppDef]:
405405
self.status(app_id)
406406
return self._apps
407407

408-
def stop(self, app_handle: AppHandle) -> None:
408+
def cancel(self, app_handle: AppHandle) -> None:
409409
"""
410410
Stops the application, effectively directing the scheduler to cancel
411411
the job. Does nothing if the app does not exist.
@@ -419,11 +419,25 @@ def stop(self, app_handle: AppHandle) -> None:
419419
420420
"""
421421
scheduler, scheduler_backend, app_id = self._scheduler_app_id(app_handle)
422-
with log_event("stop", scheduler_backend, app_id):
422+
with log_event("cancel", scheduler_backend, app_id):
423423
status = self.status(app_handle)
424424
if status is not None and not status.is_terminal():
425425
scheduler.cancel(app_id)
426426

427+
def stop(self, app_handle: AppHandle) -> None:
428+
"""
429+
See method ``cancel``.
430+
431+
.. warning:: This method will be deprecated in the future. It has been
432+
replaced with ``cancel`` which provides the same functionality.
433+
The change is to be consistent with the CLI and scheduler API.
434+
"""
435+
warnings.warn(
436+
"This method will be deprecated in the future, please use `cancel` instead.",
437+
PendingDeprecationWarning,
438+
)
439+
self.cancel(app_handle)
440+
427441
def describe(self, app_handle: AppHandle) -> Optional[AppDef]:
428442
"""
429443
Reconstructs the application (to the best extent) given the app handle.

torchx/runner/test/api_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def test_status(self, _) -> None:
293293
app_handle = runner.run(app, scheduler="local_dir", cfg=self.cfg)
294294
app_status = none_throws(runner.status(app_handle))
295295
self.assertEqual(AppState.RUNNING, app_status.state)
296-
runner.stop(app_handle)
296+
runner.cancel(app_handle)
297297
app_status = none_throws(runner.status(app_handle))
298298
self.assertEqual(AppState.CANCELLED, app_status.state)
299299

@@ -364,6 +364,13 @@ def test_wait_unknown_app(self, _) -> None:
364364
runner.wait("local_dir://another_session/some_app", wait_interval=0.1)
365365
)
366366

367+
def test_cancel(self, _) -> None:
368+
with Runner(
369+
name=SESSION_NAME,
370+
schedulers={"local_dir": self.scheduler},
371+
) as runner:
372+
self.assertIsNone(runner.cancel("local_dir://test_session/unknown_app_id"))
373+
367374
def test_stop(self, _) -> None:
368375
with Runner(
369376
name=SESSION_NAME,

0 commit comments

Comments
 (0)