From ef0814528693d736b88e72ccbdc45639b6943b72 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Fri, 8 Dec 2023 21:41:37 +0900 Subject: [PATCH 01/12] [udpate] check terminal session --- scripts/auto-stop-idle/autostop.py | 83 +++++++++++++++++++----------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 643375d..1c07482 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -11,12 +11,14 @@ # express or implied. See the License for the specific language governing # permissions and limitations under the License. -import requests +import getopt +import json +import sys from datetime import datetime -import getopt, sys -import urllib3 + import boto3 -import json +import requests +import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) @@ -39,10 +41,12 @@ # Read in command-line parameters idle = True -port = '8443' +port = "8443" ignore_connections = False try: - opts, args = getopt.getopt(sys.argv[1:], "ht:p:c", ["help","time=","port=","ignore-connections"]) + opts, args = getopt.getopt( + sys.argv[1:], "ht:p:c", ["help", "time=", "port=", "ignore-connections"] + ) if len(opts) == 0: raise getopt.GetoptError("No input parameters!") for opt, arg in opts: @@ -69,57 +73,78 @@ def is_idle(last_activity): - last_activity = datetime.strptime(last_activity,"%Y-%m-%dT%H:%M:%S.%fz") + last_activity = datetime.strptime(last_activity, "%Y-%m-%dT%H:%M:%S.%fz") if (datetime.now() - last_activity).total_seconds() > time: - print('Notebook is idle. Last activity time = ', last_activity) + print("Jupyter is idle. Last activity time = ", last_activity) return True else: - print('Notebook is not idle. Last activity time = ', last_activity) + print("Jupyter is not idle. Last activity time = ", last_activity) return False +def is_terminal_active(): + response = requests.get( + f"https://localhost:{port}/api/terminals", + verify=False, + ) + terminal_data = response.json() + return len(terminal_data) > 0 + + def get_notebook_name(): - log_path = '/opt/ml/metadata/resource-metadata.json' - with open(log_path, 'r') as logs: + log_path = "/opt/ml/metadata/resource-metadata.json" + with open(log_path, "r") as logs: _logs = json.load(logs) - return _logs['ResourceName'] + return _logs["ResourceName"] + # This is hitting Jupyter's sessions API: https://github.com/jupyter/jupyter/wiki/Jupyter-Notebook-Server-API#Sessions-API -response = requests.get('https://localhost:'+port+'/api/sessions', verify=False) +response = requests.get("https://localhost:" + port + "/api/sessions", verify=False) data = response.json() if len(data) > 0: for notebook in data: # Idleness is defined by Jupyter # https://github.com/jupyter/notebook/issues/4634 - if notebook['kernel']['execution_state'] == 'idle': + if notebook["kernel"]["execution_state"] == "idle": if not ignore_connections: - if notebook['kernel']['connections'] == 0: - if not is_idle(notebook['kernel']['last_activity']): + if notebook["kernel"]["connections"] == 0: + if not is_idle(notebook["kernel"]["last_activity"]): idle = False else: idle = False - print('Notebook idle state set as %s because no kernel has been detected.' % idle) + print( + "Jupyter idle state set as %s because no kernel has been detected." + % idle + ) else: - if not is_idle(notebook['kernel']['last_activity']): + if not is_idle(notebook["kernel"]["last_activity"]): idle = False - print('Notebook idle state set as %s since kernel connections are ignored.' % idle) + print( + "Jupyter idle state set as %s since kernel connections are ignored." + % idle + ) + elif not is_terminal_active(): + if not is_idle(notebook["kernel"]["last_activity"]): + idle = False + print( + "Jupyter idle state set as %s since kernel connections are ignored." + % idle + ) else: - print('Notebook is not idle:', notebook['kernel']['execution_state']) + print("Jupyter is not idle:", notebook["kernel"]["execution_state"]) idle = False else: - client = boto3.client('sagemaker') + client = boto3.client("sagemaker") uptime = client.describe_notebook_instance( NotebookInstanceName=get_notebook_name() - )['LastModifiedTime'] + )["LastModifiedTime"] if not is_idle(uptime.strftime("%Y-%m-%dT%H:%M:%S.%fz")): idle = False - print('Notebook idle state set as %s since no sessions detected.' % idle) + print("Jupyter idle state set as %s since no sessions detected." % idle) if idle: - print('Closing idle notebook') - client = boto3.client('sagemaker') - client.stop_notebook_instance( - NotebookInstanceName=get_notebook_name() - ) + print("Closing idle notebook") + client = boto3.client("sagemaker") + client.stop_notebook_instance(NotebookInstanceName=get_notebook_name()) else: - print('Notebook not idle. Pass.') \ No newline at end of file + print("Notebook not idle. Pass.") From e45281a3a2ee850acaef036578198a3c472cebd8 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Fri, 8 Dec 2023 23:17:13 +0900 Subject: [PATCH 02/12] [update] check terminal exists --- scripts/auto-stop-idle/autostop.py | 110 ++++++++++++----------------- 1 file changed, 46 insertions(+), 64 deletions(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 1c07482..9c9e4a5 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -22,30 +22,33 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) -# Usage +# 使用方法 usageInfo = """Usage: -This scripts checks if a notebook is idle for X seconds if it does, it'll stop the notebook: +This script checks if a notebook is idle for X seconds and if it does, it'll stop the notebook: python autostop.py --time [--port ] [--ignore-connections] Type "python autostop.py -h" for available options. """ -# Help info + +# ヘルプ情報 helpInfo = """-t, --time Auto stop time in seconds -p, --port - jupyter port + Jupyter port -c --ignore-connections Stop notebook once idle, ignore connected users -h, --help Help information """ -# Read in command-line parameters +# コマンドラインパラメータの読み込み idle = True port = "8443" ignore_connections = False try: opts, args = getopt.getopt( - sys.argv[1:], "ht:p:c", ["help", "time=", "port=", "ignore-connections"] + sys.argv[1:], + "ht:p:c", + ["help", "time=", "port=", "ignore-connections"], ) if len(opts) == 0: raise getopt.GetoptError("No input parameters!") @@ -63,7 +66,7 @@ print(usageInfo) exit(1) -# Missing configuration notification +# 構成情報が不足している場合の通知 missingConfiguration = False if not time: print("Missing '-t' or '--time'") @@ -72,25 +75,35 @@ exit(2) -def is_idle(last_activity): - last_activity = datetime.strptime(last_activity, "%Y-%m-%dT%H:%M:%S.%fz") - if (datetime.now() - last_activity).total_seconds() > time: - print("Jupyter is idle. Last activity time = ", last_activity) - return True - else: - print("Jupyter is not idle. Last activity time = ", last_activity) - return False +# 最後のアクティビティ時間を取得する関数 +def get_last_activity(): + notebook_response = requests.get( + f"https://localhost:{port}/api/sessions", verify=False + ) + notebook_data = notebook_response.json() + terminal_response = requests.get( + f"https://localhost:{port}/api/terminals", verify=False + ) + terminal_data = terminal_response.json() + last_activity = datetime.min -def is_terminal_active(): - response = requests.get( - f"https://localhost:{port}/api/terminals", - verify=False, - ) - terminal_data = response.json() - return len(terminal_data) > 0 + # Jupyter Notebookのセッションから最後のアクティビティ時間を取得 + for notebook in notebook_data: + activity_time = datetime.strptime( + notebook["kernel"]["last_activity"], "%Y-%m-%dT%H:%M:%S.%fz" + ) + if activity_time > last_activity: + last_activity = activity_time + # ターミナルセッションが存在する場合、現在時刻を最後のアクティビティ時間として扱う + if len(terminal_data) > 0: + last_activity = datetime.now() + return last_activity + + +# ノートブックの名前を取得する関数 def get_notebook_name(): log_path = "/opt/ml/metadata/resource-metadata.json" with open(log_path, "r") as logs: @@ -98,50 +111,19 @@ def get_notebook_name(): return _logs["ResourceName"] -# This is hitting Jupyter's sessions API: https://github.com/jupyter/jupyter/wiki/Jupyter-Notebook-Server-API#Sessions-API -response = requests.get("https://localhost:" + port + "/api/sessions", verify=False) -data = response.json() -if len(data) > 0: - for notebook in data: - # Idleness is defined by Jupyter - # https://github.com/jupyter/notebook/issues/4634 - if notebook["kernel"]["execution_state"] == "idle": - if not ignore_connections: - if notebook["kernel"]["connections"] == 0: - if not is_idle(notebook["kernel"]["last_activity"]): - idle = False - else: - idle = False - print( - "Jupyter idle state set as %s because no kernel has been detected." - % idle - ) - else: - if not is_idle(notebook["kernel"]["last_activity"]): - idle = False - print( - "Jupyter idle state set as %s since kernel connections are ignored." - % idle - ) - elif not is_terminal_active(): - if not is_idle(notebook["kernel"]["last_activity"]): - idle = False - print( - "Jupyter idle state set as %s since kernel connections are ignored." - % idle - ) - else: - print("Jupyter is not idle:", notebook["kernel"]["execution_state"]) - idle = False +# アイドル状態のチェック +last_activity = get_last_activity() +if (datetime.now() - last_activity).total_seconds() > time: + print( + "Jupyter has been idle for more than specified time. Last activity time = ", + last_activity, + ) + idle = True else: - client = boto3.client("sagemaker") - uptime = client.describe_notebook_instance( - NotebookInstanceName=get_notebook_name() - )["LastModifiedTime"] - if not is_idle(uptime.strftime("%Y-%m-%dT%H:%M:%S.%fz")): - idle = False - print("Jupyter idle state set as %s since no sessions detected." % idle) + print("Jupyter is not idle. Last activity time = ", last_activity) + idle = False +# インスタンスの停止処理 if idle: print("Closing idle notebook") client = boto3.client("sagemaker") From 147ee014a9104d21e067a9f084e604ce6e2cc9fc Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Sat, 9 Dec 2023 18:06:12 +0900 Subject: [PATCH 03/12] [update] use /api/terminals last_activity --- scripts/auto-stop-idle/autostop.py | 85 ++++++++++++------------------ 1 file changed, 35 insertions(+), 50 deletions(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 9c9e4a5..06c5c01 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -22,14 +22,12 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) -# 使用方法 +# 使用方法とヘルプ情報 usageInfo = """Usage: -This script checks if a notebook is idle for X seconds and if it does, it'll stop the notebook: +This script checks if a notebook and terminal are idle for X seconds and if so, it'll stop the notebook: python autostop.py --time [--port ] [--ignore-connections] Type "python autostop.py -h" for available options. """ - -# ヘルプ情報 helpInfo = """-t, --time Auto stop time in seconds -p, --port @@ -46,9 +44,7 @@ ignore_connections = False try: opts, args = getopt.getopt( - sys.argv[1:], - "ht:p:c", - ["help", "time=", "port=", "ignore-connections"], + sys.argv[1:], "ht:p:c", ["help", "time=", "port=", "ignore-connections"] ) if len(opts) == 0: raise getopt.GetoptError("No input parameters!") @@ -66,44 +62,29 @@ print(usageInfo) exit(1) -# 構成情報が不足している場合の通知 -missingConfiguration = False if not time: print("Missing '-t' or '--time'") - missingConfiguration = True -if missingConfiguration: exit(2) -# 最後のアクティビティ時間を取得する関数 -def get_last_activity(): - notebook_response = requests.get( - f"https://localhost:{port}/api/sessions", verify=False - ) - notebook_data = notebook_response.json() - terminal_response = requests.get( - f"https://localhost:{port}/api/terminals", verify=False - ) - terminal_data = terminal_response.json() +def is_idle(last_activity): + last_activity = datetime.strptime(last_activity, "%Y-%m-%dT%H:%M:%S.%fZ") + return (datetime.now() - last_activity).total_seconds() > time - last_activity = datetime.min - # Jupyter Notebookのセッションから最後のアクティビティ時間を取得 - for notebook in notebook_data: +def get_latest_terminal_activity(): + response = requests.get(f"https://localhost:{port}/api/terminals", verify=False) + terminals = response.json() + latest_activity = None + for terminal in terminals: activity_time = datetime.strptime( - notebook["kernel"]["last_activity"], "%Y-%m-%dT%H:%M:%S.%fz" + terminal["last_activity"], "%Y-%m-%dT%H:%M:%S.%fZ" ) - if activity_time > last_activity: - last_activity = activity_time - - # ターミナルセッションが存在する場合、現在時刻を最後のアクティビティ時間として扱う - if len(terminal_data) > 0: - last_activity = datetime.now() + if latest_activity is None or activity_time > latest_activity: + latest_activity = activity_time + return latest_activity - return last_activity - -# ノートブックの名前を取得する関数 def get_notebook_name(): log_path = "/opt/ml/metadata/resource-metadata.json" with open(log_path, "r") as logs: @@ -111,22 +92,26 @@ def get_notebook_name(): return _logs["ResourceName"] -# アイドル状態のチェック -last_activity = get_last_activity() -if (datetime.now() - last_activity).total_seconds() > time: - print( - "Jupyter has been idle for more than specified time. Last activity time = ", - last_activity, - ) - idle = True -else: - print("Jupyter is not idle. Last activity time = ", last_activity) - idle = False - -# インスタンスの停止処理 -if idle: - print("Closing idle notebook") +# ノートブックセッションのアイドル状態をチェック +response = requests.get(f"https://localhost:{port}/api/sessions", verify=False) +notebooks = response.json() +notebook_idle = all( + notebook["kernel"]["execution_state"] == "idle" + and (ignore_connections or notebook["kernel"]["connections"] == 0) + and is_idle(notebook["kernel"]["last_activity"]) + for notebook in notebooks +) + +# ターミナルセッションのアイドル状態をチェック +latest_terminal_activity = get_latest_terminal_activity() +terminal_idle = not latest_terminal_activity or is_idle( + latest_terminal_activity.isoformat() +) + +# 両方がアイドル状態であればインスタンスを停止 +if notebook_idle and terminal_idle: + print("Closing idle notebook and terminal") client = boto3.client("sagemaker") client.stop_notebook_instance(NotebookInstanceName=get_notebook_name()) else: - print("Notebook not idle. Pass.") + print("Notebook or terminal not idle. Pass.") From 2c5f50441905f1bee1f1af5e72e44b5b1e6b6460 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Tue, 12 Dec 2023 10:22:55 +0900 Subject: [PATCH 04/12] [fix] to judge from terminals last activity --- scripts/auto-stop-idle/autostop.py | 97 +++++++++++++++++++----------- 1 file changed, 63 insertions(+), 34 deletions(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 06c5c01..1c181d6 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -22,23 +22,24 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) -# 使用方法とヘルプ情報 +# Usage usageInfo = """Usage: -This script checks if a notebook and terminal are idle for X seconds and if so, it'll stop the notebook: +This scripts checks if a notebook is idle for X seconds if it does, it'll stop the notebook: python autostop.py --time [--port ] [--ignore-connections] Type "python autostop.py -h" for available options. """ +# Help info helpInfo = """-t, --time Auto stop time in seconds -p, --port - Jupyter port + jupyter port -c --ignore-connections Stop notebook once idle, ignore connected users -h, --help Help information """ -# コマンドラインパラメータの読み込み +# Read in command-line parameters idle = True port = "8443" ignore_connections = False @@ -62,27 +63,30 @@ print(usageInfo) exit(1) +# Missing configuration notification +missingConfiguration = False if not time: print("Missing '-t' or '--time'") + missingConfiguration = True +if missingConfiguration: exit(2) def is_idle(last_activity): - last_activity = datetime.strptime(last_activity, "%Y-%m-%dT%H:%M:%S.%fZ") - return (datetime.now() - last_activity).total_seconds() > time + last_activity = datetime.strptime(last_activity, "%Y-%m-%dT%H:%M:%S.%fz") + if (datetime.now() - last_activity).total_seconds() > time: + print("Kernel is idle. Last activity time = ", last_activity) + return True + else: + print("Kernel is not idle. Last activity time = ", last_activity) + return False -def get_latest_terminal_activity(): - response = requests.get(f"https://localhost:{port}/api/terminals", verify=False) - terminals = response.json() - latest_activity = None - for terminal in terminals: - activity_time = datetime.strptime( - terminal["last_activity"], "%Y-%m-%dT%H:%M:%S.%fZ" - ) - if latest_activity is None or activity_time > latest_activity: - latest_activity = activity_time - return latest_activity +def is_terminal_state(response): + for notebook in response: + if is_idle(notebook["last_activity"]): + return True + return False def get_notebook_name(): @@ -92,26 +96,51 @@ def get_notebook_name(): return _logs["ResourceName"] -# ノートブックセッションのアイドル状態をチェック +# This is hitting Jupyter's sessions API: https://github.com/jupyter/jupyter/wiki/Jupyter-Notebook-Server-API#Sessions-API response = requests.get(f"https://localhost:{port}/api/sessions", verify=False) -notebooks = response.json() -notebook_idle = all( - notebook["kernel"]["execution_state"] == "idle" - and (ignore_connections or notebook["kernel"]["connections"] == 0) - and is_idle(notebook["kernel"]["last_activity"]) - for notebook in notebooks -) - -# ターミナルセッションのアイドル状態をチェック -latest_terminal_activity = get_latest_terminal_activity() -terminal_idle = not latest_terminal_activity or is_idle( - latest_terminal_activity.isoformat() +data = response.json() +if len(data) > 0: + for notebook in data: + # Idleness is defined by Jupyter + # https://github.com/jupyter/notebook/issues/4634 + if notebook["kernel"]["execution_state"] == "idle": + if not ignore_connections: + if notebook["kernel"]["connections"] == 0: + if not is_idle(notebook["kernel"]["last_activity"]): + idle = False + else: + idle = False + print( + f"Notebook idle state set as {idle} because no kernel has been detected." + ) + else: + if not is_idle(notebook["kernel"]["last_activity"]): + idle = False + print( + f"Notebook idle state set as {idle} since kernel connections are ignored." + ) + else: + print("Notebook is not idle:", notebook["kernel"]["execution_state"]) + idle = False +else: + client = boto3.client("sagemaker") + uptime = client.describe_notebook_instance( + NotebookInstanceName=get_notebook_name() + )["LastModifiedTime"] + if not is_idle(uptime.strftime("%Y-%m-%dT%H:%M:%S.%fz")): + idle = False + print(f"Notebook idle state set as {idle} since no sessions detected.") + +# Check terminals is idle or not +response = requests.get( + f"https://localhost:{port}/api/terminals", + verify=False, ) +idle = idle or is_terminal_state(data) -# 両方がアイドル状態であればインスタンスを停止 -if notebook_idle and terminal_idle: - print("Closing idle notebook and terminal") +if idle: + print("Closing idle notebook") client = boto3.client("sagemaker") client.stop_notebook_instance(NotebookInstanceName=get_notebook_name()) else: - print("Notebook or terminal not idle. Pass.") + print("Kernel not idle. Pass.") From c778003b6c34a4979daa068fa19af0a4ae66b7d5 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Tue, 12 Dec 2023 11:17:31 +0900 Subject: [PATCH 05/12] [update] print response for debgg --- scripts/auto-stop-idle/autostop.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 1c181d6..87b1cda 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -133,9 +133,10 @@ def get_notebook_name(): # Check terminals is idle or not response = requests.get( - f"https://localhost:{port}/api/terminals", + f"https://localhost:{port}//terminals", verify=False, ) +print(response) idle = idle or is_terminal_state(data) if idle: From 2b16dbec6a00350a50e58ac7d7a52a7a959ccbe1 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Tue, 12 Dec 2023 11:18:46 +0900 Subject: [PATCH 06/12] [fix] var name notebook -> terminal --- scripts/auto-stop-idle/autostop.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 87b1cda..cd99557 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -83,8 +83,9 @@ def is_idle(last_activity): def is_terminal_state(response): - for notebook in response: - if is_idle(notebook["last_activity"]): + for terminal in response: + print(terminal) + if is_idle(terminal["last_activity"]): return True return False From 4fbb46e44e408dab93fceb57855d10c7abc201e1 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Tue, 12 Dec 2023 11:31:44 +0900 Subject: [PATCH 07/12] [fix] response dump to json --- scripts/auto-stop-idle/autostop.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index cd99557..3ac4829 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -137,7 +137,8 @@ def get_notebook_name(): f"https://localhost:{port}//terminals", verify=False, ) -print(response) +data = response.json() +print(data) idle = idle or is_terminal_state(data) if idle: From ea6b6520bde5c9aa24804b7a626e65ee431abdbc Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Tue, 12 Dec 2023 11:41:51 +0900 Subject: [PATCH 08/12] [fix] fill blank --- scripts/auto-stop-idle/autostop.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 3ac4829..40d67f5 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -134,7 +134,7 @@ def get_notebook_name(): # Check terminals is idle or not response = requests.get( - f"https://localhost:{port}//terminals", + f"https://localhost:{port}/api/terminals", verify=False, ) data = response.json() From d1f1b735a4b928e038269fb491e12ff09a625d4c Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Tue, 12 Dec 2023 11:56:18 +0900 Subject: [PATCH 09/12] [clean] remove debug line --- scripts/auto-stop-idle/autostop.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 40d67f5..5779775 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -138,7 +138,6 @@ def get_notebook_name(): verify=False, ) data = response.json() -print(data) idle = idle or is_terminal_state(data) if idle: From 967f146f413f168246c7ad165ab284b0657c919e Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Tue, 12 Dec 2023 23:19:30 +0900 Subject: [PATCH 10/12] [fix] do not shutdown when notebook doesn't have any sessions --- scripts/auto-stop-idle/autostop.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 5779775..e23677b 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -138,7 +138,7 @@ def get_notebook_name(): verify=False, ) data = response.json() -idle = idle or is_terminal_state(data) +idle = idle and is_terminal_state(data) if idle: print("Closing idle notebook") From 01de6202a46621366433f0b43dc79aedc3ce7e32 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Mon, 18 Dec 2023 15:06:55 +0900 Subject: [PATCH 11/12] [fix] fix an issue that the app continue working without a terminal --- scripts/auto-stop-idle/autostop.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index e23677b..03fe867 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -138,7 +138,10 @@ def get_notebook_name(): verify=False, ) data = response.json() -idle = idle and is_terminal_state(data) + +terminal_idle = is_terminal_state(data) if data else True + +idle = idle and terminal_idle if idle: print("Closing idle notebook") From 29c0c87b0c4ae9419ae43ab0c92ba286fd23caf8 Mon Sep 17 00:00:00 2001 From: Takeru Matsumoto Date: Mon, 18 Dec 2023 16:46:31 +0900 Subject: [PATCH 12/12] [fix] variable name --- scripts/auto-stop-idle/autostop.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/auto-stop-idle/autostop.py b/scripts/auto-stop-idle/autostop.py index 03fe867..6405913 100644 --- a/scripts/auto-stop-idle/autostop.py +++ b/scripts/auto-stop-idle/autostop.py @@ -133,13 +133,13 @@ def get_notebook_name(): print(f"Notebook idle state set as {idle} since no sessions detected.") # Check terminals is idle or not -response = requests.get( +terminal_response = requests.get( f"https://localhost:{port}/api/terminals", verify=False, ) -data = response.json() +terminal_data = terminal_response.json() -terminal_idle = is_terminal_state(data) if data else True +terminal_idle = is_terminal_state(terminal_data) if terminal_data else True idle = idle and terminal_idle