Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

Commit 1eba709

Browse files
committed
cli: Add "restart" command to restart sessions.
1 parent 9ef938b commit 1eba709

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changes
22
=======
33

4+
19.09.5 (2020-03-xx)
5+
--------------------
6+
7+
* NEW: Add ``backend.ai restart`` command and improve error handling in
8+
the ``backend.ai terminate`` command.
9+
410
19.09.4 (2020-02-10)
511
--------------------
612

src/ai/backend/client/cli/run.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -846,23 +846,23 @@ def start(image, session_id, owner, # base args
846846

847847

848848
@main.command(aliases=['rm', 'kill'])
849-
@click.argument('sess_id_or_alias', metavar='SESSID', nargs=-1)
849+
@click.argument('session_ids', metavar='SESSID', nargs=-1)
850850
@click.option('-o', '--owner', '--owner-access-key', metavar='ACCESS_KEY',
851851
help='Specify the owner of the target session explicitly.')
852852
@click.option('-s', '--stats', is_flag=True,
853853
help='Show resource usage statistics after termination')
854-
def terminate(sess_id_or_alias, owner, stats):
854+
def terminate(session_ids, owner, stats):
855855
'''
856856
Terminate the given session.
857857
858-
SESSID: session ID or its alias given when creating the session.
858+
SESSID: session ID given/generated when creating the session.
859859
'''
860860
print_wait('Terminating the session(s)...')
861861
with Session() as session:
862862
has_failure = False
863-
for sess in sess_id_or_alias:
863+
for session_id in session_ids:
864864
try:
865-
kernel = session.Kernel(sess, owner)
865+
kernel = session.Kernel(session_id, owner)
866866
ret = kernel.destroy()
867867
except BackendAPIError as e:
868868
print_error(e)
@@ -874,16 +874,49 @@ def terminate(sess_id_or_alias, owner, stats):
874874
except Exception as e:
875875
print_error(e)
876876
has_failure = True
877-
if has_failure:
878-
sys.exit(1)
879877
else:
880-
print_done('Done.')
878+
if not has_failure:
879+
print_done('Done.')
881880
if stats:
882881
stats = ret.get('stats', None) if ret else None
883882
if stats:
884883
print(_format_stats(stats))
885884
else:
886885
print('Statistics is not available.')
886+
if has_failure:
887+
sys.exit(1)
888+
889+
890+
@main.command()
891+
@click.argument('session_ids', metavar='SESSID', nargs=-1)
892+
def restart(session_ids):
893+
'''
894+
Restart the given session.
895+
896+
SESSID: session ID given/generated when creating the session.
897+
'''
898+
print_wait('Restarting the session(s)...')
899+
with Session() as session:
900+
has_failure = False
901+
for sess in session_ids:
902+
try:
903+
kernel = session.Kernel(sess)
904+
kernel.restart()
905+
except BackendAPIError as e:
906+
print_error(e)
907+
if e.status == 404:
908+
print_info(
909+
'If you are an admin, use "-o" / "--owner" option '
910+
'to terminate other user\'s session.')
911+
has_failure = True
912+
except Exception as e:
913+
print_error(e)
914+
has_failure = True
915+
else:
916+
if not has_failure:
917+
print_done('Done.')
918+
if has_failure:
919+
sys.exit(1)
887920

888921

889922
@main.command()

0 commit comments

Comments
 (0)