build: retry hasher function if it fails first time (#30899)

* build: retry hasher function if it fails first time

* Update script/release/get-url-hash.js

Co-authored-by: Cheng Zhao <zcbenz@gmail.com>

Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
This commit is contained in:
Samuel Attard 2021-10-25 14:11:07 -07:00 committed by GitHub
parent 8f51d3e1bf
commit add94f5fe6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,24 +8,32 @@ const lambda = new AWS.Lambda({
region: 'us-east-1' region: 'us-east-1'
}); });
module.exports = function getUrlHash (targetUrl, algorithm = 'sha256') { module.exports = async function getUrlHash (targetUrl, algorithm = 'sha256', attempts = 3) {
return new Promise((resolve, reject) => { try {
lambda.invoke({ return new Promise((resolve, reject) => {
FunctionName: 'hasher', lambda.invoke({
Payload: JSON.stringify({ FunctionName: 'hasher',
targetUrl, Payload: JSON.stringify({
algorithm targetUrl,
}) algorithm
}, (err, data) => { })
if (err) return reject(err); }, (err, data) => {
try { if (err) return reject(err);
const response = JSON.parse(data.Payload); try {
if (response.statusCode !== 200) return reject(new Error('non-200 status code received from hasher function')); const response = JSON.parse(data.Payload);
if (!response.hash) return reject(new Error('Successful lambda call but failed to get valid hash')); if (response.statusCode !== 200) return reject(new Error('non-200 status code received from hasher function'));
resolve(response.hash); if (!response.hash) return reject(new Error('Successful lambda call but failed to get valid hash'));
} catch (err) { resolve(response.hash);
return reject(err); } catch (err) {
} return reject(err);
}
});
}); });
}); } catch (err) {
if (attempts > 1) {
console.error('Failed to get URL hash for', targetUrl, 'we will retry', err);
return getUrlHash(targetUrl, algorithm, attempts - 1);
}
throw err;
}
}; };