From ce662e99333eb5c43ed2a620a221146c502a9bf2 Mon Sep 17 00:00:00 2001 From: manskx Date: Fri, 21 Oct 2022 14:13:03 +0200 Subject: [PATCH 1/5] delete apps older than 1 hours CI account --- src/lightning_app/testing/testing.py | 30 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/lightning_app/testing/testing.py b/src/lightning_app/testing/testing.py index 21ee0c8605609..58d934c92b113 100644 --- a/src/lightning_app/testing/testing.py +++ b/src/lightning_app/testing/testing.py @@ -458,11 +458,24 @@ def wait_for(page, callback: Callable, *args, **kwargs) -> Any: sleep(2) +def _delete_lightning_app(client, project_id, app_id, app_name): + print(f"Deleting {app_name} id: {app_id}") + try: + res = client.lightningapp_instance_service_delete_lightningapp_instance( + project_id=project_id, + id=app_id, + ) + assert res == {} + except ApiException as e: + print(f"Failed to delete {app_name}. Exception {e}") + + def delete_cloud_lightning_apps(): """Cleanup cloud apps that start with the name test-{PR_NUMBER}-{TEST_APP_NAME}. PR_NUMBER and TEST_APP_NAME are environment variables. """ + import datetime client = LightningClient() @@ -483,12 +496,11 @@ def delete_cloud_lightning_apps(): for lit_app in list_apps.lightningapps: if pr_number and app_name and not lit_app.name.startswith(f"test-{pr_number}-{app_name}-"): continue - print(f"Deleting {lit_app.name}") - try: - res = client.lightningapp_instance_service_delete_lightningapp_instance( - project_id=project.project_id, - id=lit_app.id, - ) - assert res == {} - except ApiException as e: - print(f"Failed to delete {lit_app.name}. Exception {e}") + _delete_lightning_app(client, project_id=project.project_id, app_id=lit_app.id, app_name=lit_app.name) + + print(f"deleting apps that were created more than 1 hour ago.") + + for lit_app in list_apps.lightningapps: + + if lit_app.created_at < datetime.datetime.now(lit_app.created_at.tzinfo) - datetime.timedelta(hours=1): + _delete_lightning_app(client, project_id=project.project_id, app_id=lit_app.id, app_name=lit_app.name) From 9f119832d5dab024740f9e15d1055896a7f54b9c Mon Sep 17 00:00:00 2001 From: manskx Date: Fri, 21 Oct 2022 14:32:28 +0200 Subject: [PATCH 2/5] remove print --- src/lightning_app/testing/testing.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lightning_app/testing/testing.py b/src/lightning_app/testing/testing.py index 58d934c92b113..b6ba8511d1177 100644 --- a/src/lightning_app/testing/testing.py +++ b/src/lightning_app/testing/testing.py @@ -491,8 +491,6 @@ def delete_cloud_lightning_apps(): project = _get_project(client) list_apps = client.lightningapp_instance_service_list_lightningapp_instances(project_id=project.project_id) - print([lit_app.name for lit_app in list_apps.lightningapps]) - for lit_app in list_apps.lightningapps: if pr_number and app_name and not lit_app.name.startswith(f"test-{pr_number}-{app_name}-"): continue From e8e2aa01137f2fbcc8b5a69fa123d7fe817bfcc2 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Sat, 22 Oct 2022 13:32:04 +0200 Subject: [PATCH 3/5] Apply suggestions from code review --- src/lightning_app/testing/testing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lightning_app/testing/testing.py b/src/lightning_app/testing/testing.py index b6ba8511d1177..a18cc0ea98033 100644 --- a/src/lightning_app/testing/testing.py +++ b/src/lightning_app/testing/testing.py @@ -466,8 +466,8 @@ def _delete_lightning_app(client, project_id, app_id, app_name): id=app_id, ) assert res == {} - except ApiException as e: - print(f"Failed to delete {app_name}. Exception {e}") + except ApiException as ex: + print(f"Failed to delete {app_name}. Exception {ex}") def delete_cloud_lightning_apps(): From 28642c69b421cf0a0b316fa68faa7fc65374f712 Mon Sep 17 00:00:00 2001 From: manskx Date: Mon, 24 Oct 2022 20:37:48 +0200 Subject: [PATCH 4/5] move import to top --- src/lightning_app/testing/testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_app/testing/testing.py b/src/lightning_app/testing/testing.py index a18cc0ea98033..5f23d2016e1f5 100644 --- a/src/lightning_app/testing/testing.py +++ b/src/lightning_app/testing/testing.py @@ -1,4 +1,5 @@ import asyncio +import datetime import json import os import shutil @@ -475,7 +476,6 @@ def delete_cloud_lightning_apps(): PR_NUMBER and TEST_APP_NAME are environment variables. """ - import datetime client = LightningClient() From 5a3f3fa57e0f3c2561dbe13dc9706869bc84f412 Mon Sep 17 00:00:00 2001 From: manskx Date: Tue, 25 Oct 2022 12:20:56 +0200 Subject: [PATCH 5/5] fix black --- src/lightning_app/testing/testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_app/testing/testing.py b/src/lightning_app/testing/testing.py index 5f23d2016e1f5..aab9d8f0a2f92 100644 --- a/src/lightning_app/testing/testing.py +++ b/src/lightning_app/testing/testing.py @@ -496,7 +496,7 @@ def delete_cloud_lightning_apps(): continue _delete_lightning_app(client, project_id=project.project_id, app_id=lit_app.id, app_name=lit_app.name) - print(f"deleting apps that were created more than 1 hour ago.") + print("deleting apps that were created more than 1 hour ago.") for lit_app in list_apps.lightningapps: