@@ -557,6 +557,48 @@ protected URI calculateTarget(Container container, PortMappings.PortMappingEntry
557557 return new URI (String .format ("%s://%s:%s%s" , getDefaultTargetProtocol (), targetHostName , targetPort , portMapping .getTargetPath ()));
558558 }
559559
560+ /**
561+ * Removes task definitions that are marked for deletion.
562+ * On shutdown of ShinyProxy there is not enough time to delete the task definitions (because of the rate limit).
563+ */
564+ @ Scheduled (initialDelay = 5 , timeUnit = TimeUnit .MINUTES )
565+ public void cleanupTaskDefinitions () {
566+ if (environment .getProperty ("proxy.store-mode" , "None" ).equals ("Redis" )) {
567+ // no task-definitions to clean up if using Redis
568+ return ;
569+ }
570+
571+ List <String > toDelete = new ArrayList <>();
572+ for (String arn : ecsClient .listTaskDefinitionsPaginator ().taskDefinitionArns ()) {
573+ List <Tag > tags = ecsClient .listTagsForResource (builder -> builder .resourceArn (arn )).tags ();
574+ if (tags .contains (TO_DELETE_TAG )) {
575+ toDelete .add (arn );
576+ try {
577+ // sleep to respect rate limit
578+ Thread .sleep (100 );
579+ } catch (InterruptedException e ) {
580+ Thread .currentThread ().interrupt ();
581+ return ;
582+ }
583+ }
584+ }
585+ if (!toDelete .isEmpty ()) {
586+ log .info ("Deleting {} task definitions from previous run" , toDelete .size ());
587+ for (String arn : toDelete ) {
588+ ecsClient .deregisterTaskDefinition (builder -> builder .taskDefinition (arn ).build ());
589+ ecsClient .deleteTaskDefinitions (builder -> builder .taskDefinitions (arn ).build ());
590+ try {
591+ // sleep to respect rate limit
592+ Thread .sleep (1250 );
593+ } catch (InterruptedException e ) {
594+ Thread .currentThread ().interrupt ();
595+ return ;
596+ }
597+ }
598+ log .info ("Deleted {} task definitions" , toDelete .size ());
599+ }
600+ }
601+
560602 private Optional <Task > getTask (Container container ) {
561603 return getTaskInfo (container ).flatMap (this ::getTask );
562604 }
0 commit comments