@@ -61,8 +61,6 @@ export interface DescribeInstancesResultRegion {
6161 describeInstanceResult : PromiseResult < EC2 . Types . DescribeInstancesResult , AWS . AWSError > ;
6262}
6363
64- const SHOULD_NOT_TRY_LIST_SSM = 'SHOULD_NOT_TRY_LIST_SSM' ;
65-
6664// Keep the cache as long as half of minimum time, this should reduce calls to AWS API
6765const ssmParametersCache = new LRU ( { maxAge : ( Config . Instance . minimumRunningTimeInMinutes * 60 * 1000 ) / 2 } ) ;
6866
@@ -357,7 +355,6 @@ export async function terminateRunners(runners: RunnerInfo[], metrics: Metrics):
357355
358356async function terminateRunnersInRegion ( runners : RunnerInfo [ ] , metrics : Metrics , region : string ) : Promise < void > {
359357 const ec2 = new EC2 ( { region } ) ;
360- const ssm = new SSM ( { region } ) ;
361358
362359 // Keep track of runners that were successfully terminated so we can clean up their SSM parameters even
363360 // if a later batch fails.
@@ -411,87 +408,6 @@ async function terminateRunnersInRegion(runners: RunnerInfo[], metrics: Metrics,
411408 }
412409}
413410
414- async function cleanupSSMParametersForRunners (
415- runners : RunnerInfo [ ] ,
416- metrics : Metrics ,
417- region : string ,
418- ssm : SSM ,
419- ) : Promise < void > {
420- const paramNames = runners . map ( ( runner ) =>
421- getParameterNameForRunner ( runner . environment || Config . Instance . environment , runner . instanceId ) ,
422- ) ;
423-
424- const cacheName = `${ SHOULD_NOT_TRY_LIST_SSM } _${ region } ` ;
425-
426- if ( ssmParametersCache . has ( cacheName ) ) {
427- // If we've had recent failures listing parameters, just try to delete them directly
428- await deleteSSMParametersInBatches ( paramNames , metrics , region , ssm ) ;
429- } else {
430- try {
431- const existingParams = await listSSMParameters ( metrics , region ) ;
432- const paramsToDelete = paramNames . filter ( ( paramName ) => existingParams . has ( paramName ) ) ;
433-
434- if ( paramsToDelete . length > 0 ) {
435- await deleteSSMParametersInBatches ( paramsToDelete , metrics , region , ssm ) ;
436- } else {
437- console . info ( `[${ region } ] No SSM parameters found to delete for ${ paramNames . length } runners` ) ;
438- }
439- } catch ( e ) {
440- ssmParametersCache . set ( cacheName , 1 , 60 * 1000 ) ;
441- console . error (
442- `[terminateRunnersInRegion - listSSMParameters] [${ region } ] ` +
443- `Failed to list parameters, attempting direct deletion: ${ e } ` ,
444- ) ;
445- await deleteSSMParametersInBatches ( paramNames , metrics , region , ssm ) ;
446- }
447- }
448- }
449-
450- async function deleteSSMParametersInBatches (
451- paramNames : string [ ] ,
452- metrics : Metrics ,
453- region : string ,
454- ssm : SSM ,
455- ) : Promise < void > {
456- const batches = chunkArray ( paramNames , 10 ) ;
457-
458- console . info ( `[${ region } ] Processing ${ paramNames . length } SSM parameters in ${ batches . length } batch(es)` ) ;
459-
460- for ( const [ batchIndex , batch ] of batches . entries ( ) ) {
461- console . info (
462- `[${ region } ] Processing SSM batch ${ batchIndex + 1 } /${ batches . length } with ${
463- batch . length
464- } parameters: ${ batch . join ( ', ' ) } `,
465- ) ;
466-
467- try {
468- await Promise . all (
469- batch . map ( ( paramName ) =>
470- expBackOff ( ( ) => {
471- return metrics . trackRequestRegion (
472- region ,
473- metrics . ssmdeleteParameterAWSCallSuccess ,
474- metrics . ssmdeleteParameterAWSCallFailure ,
475- ( ) => {
476- return ssm . deleteParameter ( { Name : paramName } ) . promise ( ) ;
477- } ,
478- ) ;
479- } ) ,
480- ) ,
481- ) ;
482-
483- console . info (
484- `[${ region } ] Successfully deleted SSM batch ${ batchIndex + 1 } /${ batches . length } : ${ batch . join ( ', ' ) } ` ,
485- ) ;
486- } catch ( e ) {
487- console . error (
488- `[${ region } ] Failed to delete SSM batch ${ batchIndex + 1 } /${ batches . length } : ${ batch . join ( ', ' ) } - ${ e } ` ,
489- ) ;
490- // Continue with other batches even if one fails
491- }
492- }
493- }
494-
495411function chunkArray < T > ( array : T [ ] , chunkSize : number ) : T [ ] [ ] {
496412 const chunks : T [ ] [ ] = [ ] ;
497413 for ( let i = 0 ; i < array . length ; i += chunkSize ) {
@@ -537,13 +453,15 @@ async function addSSMParameterRunnerConfig(
537453 // NOTE: This does need to be a string, check docs at:
538454 // https://docs.aws.amazon.com/systems-manager/latest/userguide/example_ssm_PutParameter_section.html
539455 // Policies must be an array, even for a single policy
540- Policies : JSON . stringify ( [ {
541- Type : 'Expiration' ,
542- Version : '1.0' ,
543- Attributes : {
544- Timestamp : new Date ( Date . now ( ) + 30 * 60 * 1000 ) . toISOString ( ) ,
456+ Policies : JSON . stringify ( [
457+ {
458+ Type : 'Expiration' ,
459+ Version : '1.0' ,
460+ Attributes : {
461+ Timestamp : new Date ( Date . now ( ) + 30 * 60 * 1000 ) . toISOString ( ) ,
462+ } ,
545463 } ,
546- } ] ) ,
464+ ] ) ,
547465 } )
548466 . promise ( ) ;
549467 return parameterName ;
0 commit comments