Skip to content

Commit

Permalink
fix(build): more resilient S3 handling for JSON feed MONGOSH-1242 (#1570
Browse files Browse the repository at this point in the history
)

Account for `NoSuchKey` errors being thrown when downloading files
from AWS.
  • Loading branch information
addaleax authored Aug 14, 2023
1 parent 4c8eede commit bb658a0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/build/src/download-center/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ export async function createAndPublishDownloadCenterConfig(
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
});
const existingDownloadCenterConfig = await dlcenter.downloadConfig(
CONFIGURATION_KEY
);
let existingDownloadCenterConfig: DownloadCenterConfig | undefined;
try {
existingDownloadCenterConfig = await dlcenter.downloadConfig(
CONFIGURATION_KEY
);
} catch (err: any) {
console.warn('Failed to get existing download center config', err);
if (err?.code !== 'NoSuchKey') throw err;
}

const getVersionConfig = () =>
createVersionConfig(packageInformation, publicArtifactBaseUrl);
Expand All @@ -59,7 +65,7 @@ export async function createAndPublishDownloadCenterConfig(
: createDownloadCenterConfig(getVersionConfig);

console.warn('Created download center config:');
//console.dir(config, { depth: Infinity });
console.dir(config, { depth: Infinity });

validateConfigSchema(config);

Expand All @@ -69,13 +75,20 @@ export async function createAndPublishDownloadCenterConfig(
secretAccessKey: awsSecretAccessKey,
});
const jsonFeedArtifactkey = `${ARTIFACTS_FOLDER}/mongosh.json`;
const existingJsonFeedText = await dlcenterArtifacts.downloadAsset(
jsonFeedArtifactkey
);
const existingJsonFeed = existingJsonFeedText
let existingJsonFeedText;
try {
existingJsonFeedText = await dlcenterArtifacts.downloadAsset(
jsonFeedArtifactkey
);
} catch (err: any) {
console.warn('Failed to get existing JSON feed text', err);
if (err?.code !== 'NoSuchKey') throw err;
}

const existingJsonFeed: JsonFeed | undefined = existingJsonFeedText
? JSON.parse(existingJsonFeedText.toString())
: undefined;
const injectedJsonFeed = injectedJsonFeedFile
const injectedJsonFeed: JsonFeed | undefined = injectedJsonFeedFile
? JSON.parse(await fs.readFile(injectedJsonFeedFile, 'utf8'))
: undefined;
const currentJsonFeedEntry = await createJsonFeedEntry(
Expand All @@ -86,7 +99,7 @@ export async function createAndPublishDownloadCenterConfig(
versions: [currentJsonFeedEntry],
};
console.warn('Adding new JSON feed entry:');
//console.dir(currentJsonFeedEntry, { depth: Infinity });
console.dir(currentJsonFeedEntry, { depth: Infinity });

const newJsonFeed = mergeFeeds(
existingJsonFeed,
Expand Down Expand Up @@ -227,7 +240,7 @@ export async function createJsonFeedEntry(
};
}

function mergeFeeds(...args: JsonFeed[]): JsonFeed {
function mergeFeeds(...args: (JsonFeed | undefined)[]): JsonFeed {
const newFeed: JsonFeed = {
versions: [],
};
Expand Down
2 changes: 2 additions & 0 deletions packages/build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type { Config, PackageVariant } from './config';
export { getArtifactUrl, downloadMongoDb };

if (require.main === module) {
Error.stackTraceLimit = 200;

(async () => {
const command = process.argv[2];
if (
Expand Down

0 comments on commit bb658a0

Please sign in to comment.