Skip to content

Commit 65d79de

Browse files
author
aws-amplify-bot
committed
fix: run sequentially
1 parent 00ee5fe commit 65d79de

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

packages/amplify-e2e-tests/src/cleanup-codebuild-resources.ts

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,17 @@ const getAWSConfig = ({ accessKeyId, secretAccessKey, sessionToken }: AWSAccount
323323
maxRetries: 10,
324324
});
325325

326+
// Client cache to reuse clients and reduce memory usage
327+
const clientCache = new Map<string, any>();
328+
329+
const getClient = <T>(ClientClass: new (config: any) => T, account: AWSAccountInfo, region?: string): T => {
330+
const key = `${account.accessKeyId}-${region || 'global'}-${ClientClass.name}`;
331+
if (!clientCache.has(key)) {
332+
clientCache.set(key, new ClientClass(getAWSConfig(account, region)));
333+
}
334+
return clientCache.get(key);
335+
};
336+
326337
/**
327338
* delete an S3 bucket, copied from amplify-e2e-core
328339
*/
@@ -423,7 +434,7 @@ const getAmplifyApps = async (account: AWSAccountInfo, region: string, cbClient:
423434
if (region === 'us-east-1' && account.parent) {
424435
return []; // temporarily disabled until us-east-1 is re-enabled for this account
425436
}
426-
const amplifyClient = new AmplifyClient(getAWSConfig(account, region));
437+
const amplifyClient = getClient(AmplifyClient, account, region);
427438
try {
428439
const amplifyApps = await amplifyClient.send(new ListAppsCommand({ maxResults: 25 })); // keeping it to 25 as max supported is 25
429440
const result: AmplifyAppInfo[] = [];
@@ -1102,25 +1113,34 @@ const getEnvVarCredentials = (): AWSAccountInfo => {
11021113

11031114
const cleanupAccount = async (account: AWSAccountInfo, accountIndex: number, filterPredicate: JobFilterPredicate): Promise<void> => {
11041115
const cbClient = new CodeBuildClient(getAWSConfig(account));
1105-
const appPromises = AWS_REGIONS_TO_RUN_TESTS.map((region) => getAmplifyApps(account, region, cbClient));
1106-
const stackPromises = AWS_REGIONS_TO_RUN_TESTS.map((region) => getStacks(account, region, cbClient));
1107-
const bucketPromise = getS3Buckets(account, cbClient);
1108-
const orphanPinpointApplicationsPromise = AWS_REGIONS_TO_RUN_TESTS_PINPOINT.map((region) =>
1109-
getOrphanPinpointApplications(account, region),
1110-
);
1111-
const orphanBucketPromise = getOrphanS3TestBuckets(account);
1112-
const orphanIamRolesPromise = getOrphanTestIamRoles(account);
1113-
const orphanAppSyncApisPromise = AWS_REGIONS_TO_RUN_TESTS.map((region) => getOrphanAppSyncApis(account, region));
1114-
const orphanUserPoolsPromise = AWS_REGIONS_TO_RUN_TESTS.map((region) => getOrphanUserPools(account, region));
1115-
1116-
const apps = (await Promise.all(appPromises)).flat();
1117-
const stacks = (await Promise.all(stackPromises)).flat();
1118-
const buckets = await bucketPromise;
1119-
const orphanBuckets = await orphanBucketPromise;
1120-
const orphanIamRoles = await orphanIamRolesPromise;
1121-
const orphanPinpointApplications = (await Promise.all(orphanPinpointApplicationsPromise)).flat();
1122-
const orphanAppSyncApis = (await Promise.all(orphanAppSyncApisPromise)).flat();
1123-
const orphanUserPools = (await Promise.all(orphanUserPoolsPromise)).flat();
1116+
1117+
// Process regions sequentially to reduce memory usage
1118+
const apps: AmplifyAppInfo[] = [];
1119+
const stacks: StackInfo[] = [];
1120+
const orphanPinpointApplications: PinpointAppInfo[] = [];
1121+
const orphanAppSyncApis: AppSyncApiInfo[] = [];
1122+
const orphanUserPools: UserPoolInfo[] = [];
1123+
1124+
for (const region of AWS_REGIONS_TO_RUN_TESTS) {
1125+
const regionApps = await getAmplifyApps(account, region, cbClient);
1126+
const regionStacks = await getStacks(account, region, cbClient);
1127+
const regionAppSyncApis = await getOrphanAppSyncApis(account, region);
1128+
const regionUserPools = await getOrphanUserPools(account, region);
1129+
1130+
apps.push(...regionApps);
1131+
stacks.push(...regionStacks);
1132+
orphanAppSyncApis.push(...regionAppSyncApis);
1133+
orphanUserPools.push(...regionUserPools);
1134+
1135+
if (AWS_REGIONS_TO_RUN_TESTS_PINPOINT.includes(region)) {
1136+
const regionPinpointApps = await getOrphanPinpointApplications(account, region);
1137+
orphanPinpointApplications.push(...regionPinpointApps);
1138+
}
1139+
}
1140+
1141+
const buckets = await getS3Buckets(account, cbClient);
1142+
const orphanBuckets = await getOrphanS3TestBuckets(account);
1143+
const orphanIamRoles = await getOrphanTestIamRoles(account);
11241144

11251145
const allResources = mergeResourcesByCIJob(
11261146
apps,
@@ -1148,6 +1168,13 @@ const cleanupAccount = async (account: AWSAccountInfo, accountIndex: number, fil
11481168
generateReport(staleResources);
11491169
await deleteResources(account, accountIndex, staleResources);
11501170
await deleteOrphanedOidcProviders(account);
1171+
1172+
// Clear client cache and force garbage collection
1173+
clientCache.clear();
1174+
if (global.gc) {
1175+
global.gc();
1176+
}
1177+
11511178
console.log(`[ACCOUNT ${accountIndex}] Cleanup done!`);
11521179
};
11531180

@@ -1163,11 +1190,10 @@ const cleanup = async (): Promise<void> => {
11631190
const accounts = await getAccountsToCleanup();
11641191
for (let i = 0; i < 3; ++i) {
11651192
console.log('CLEANUP ROUND: ', i + 1);
1166-
await Promise.all(
1167-
accounts.map((account, i) => {
1168-
return cleanupAccount(account, i, filterPredicateStaleResources);
1169-
}),
1170-
);
1193+
// Process accounts sequentially to reduce memory usage
1194+
for (let accountIndex = 0; accountIndex < accounts.length; accountIndex++) {
1195+
await cleanupAccount(accounts[accountIndex], accountIndex, filterPredicateStaleResources);
1196+
}
11711197
await sleep(60 * 1000); // run again after 60 seconds
11721198
}
11731199
console.log('Done cleaning all accounts!');

packages/amplify-e2e-tests/src/cleanup-e2e-resources.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,11 +1033,9 @@ const cleanup = async (): Promise<void> => {
10331033
const accounts = await getAccountsToCleanup();
10341034
for (let i = 0; i < 3; i++) {
10351035
console.log('CLEANUP ROUND: ', i + 1);
1036-
await Promise.all(
1037-
accounts.map((account, i) => {
1038-
return cleanupAccount(account, i, filterPredicate);
1039-
}),
1040-
);
1036+
for (let index = 0; index < accounts.length; index++) {
1037+
await cleanupAccount(accounts[index], index, filterPredicate);
1038+
}
10411039
await sleep(60 * 1000); // run again after 60 seconds
10421040
}
10431041
console.log('Done cleaning all accounts!');

0 commit comments

Comments
 (0)