11import asyncio
2+ import datetime
23import json
34import os
45import shutil
@@ -472,6 +473,18 @@ def wait_for(page, callback: Callable, *args, **kwargs) -> Any:
472473 sleep (2 )
473474
474475
476+ def _delete_lightning_app (client , project_id , app_id , app_name ):
477+ print (f"Deleting { app_name } id: { app_id } " )
478+ try :
479+ res = client .lightningapp_instance_service_delete_lightningapp_instance (
480+ project_id = project_id ,
481+ id = app_id ,
482+ )
483+ assert res == {}
484+ except ApiException as ex :
485+ print (f"Failed to delete { app_name } . Exception { ex } " )
486+
487+
475488def delete_cloud_lightning_apps ():
476489 """Cleanup cloud apps that start with the name test-{PR_NUMBER}-{TEST_APP_NAME}.
477490
@@ -492,17 +505,14 @@ def delete_cloud_lightning_apps():
492505 project = _get_project (client )
493506 list_apps = client .lightningapp_instance_service_list_lightningapp_instances (project_id = project .project_id )
494507
495- print ([lit_app .name for lit_app in list_apps .lightningapps ])
496-
497508 for lit_app in list_apps .lightningapps :
498509 if pr_number and app_name and not lit_app .name .startswith (f"test-{ pr_number } -{ app_name } -" ):
499510 continue
500- print (f"Deleting { lit_app .name } " )
501- try :
502- res = client .lightningapp_instance_service_delete_lightningapp_instance (
503- project_id = project .project_id ,
504- id = lit_app .id ,
505- )
506- assert res == {}
507- except ApiException as e :
508- print (f"Failed to delete { lit_app .name } . Exception { e } " )
511+ _delete_lightning_app (client , project_id = project .project_id , app_id = lit_app .id , app_name = lit_app .name )
512+
513+ print ("deleting apps that were created more than 1 hour ago." )
514+
515+ for lit_app in list_apps .lightningapps :
516+
517+ if lit_app .created_at < datetime .datetime .now (lit_app .created_at .tzinfo ) - datetime .timedelta (hours = 1 ):
518+ _delete_lightning_app (client , project_id = project .project_id , app_id = lit_app .id , app_name = lit_app .name )
0 commit comments