From add94f5fe610b717fa973371ce8d4105ecdee8d3 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Mon, 25 Oct 2021 14:11:07 -0700 Subject: [PATCH] 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 Co-authored-by: Cheng Zhao --- script/release/get-url-hash.js | 46 ++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/script/release/get-url-hash.js b/script/release/get-url-hash.js index 3193eec51e60..0633567a7dd9 100644 --- a/script/release/get-url-hash.js +++ b/script/release/get-url-hash.js @@ -8,24 +8,32 @@ const lambda = new AWS.Lambda({ region: 'us-east-1' }); -module.exports = function getUrlHash (targetUrl, algorithm = 'sha256') { - return new Promise((resolve, reject) => { - lambda.invoke({ - FunctionName: 'hasher', - Payload: JSON.stringify({ - targetUrl, - algorithm - }) - }, (err, data) => { - if (err) return reject(err); - try { - const response = JSON.parse(data.Payload); - if (response.statusCode !== 200) return reject(new Error('non-200 status code received from hasher function')); - if (!response.hash) return reject(new Error('Successful lambda call but failed to get valid hash')); - resolve(response.hash); - } catch (err) { - return reject(err); - } +module.exports = async function getUrlHash (targetUrl, algorithm = 'sha256', attempts = 3) { + try { + return new Promise((resolve, reject) => { + lambda.invoke({ + FunctionName: 'hasher', + Payload: JSON.stringify({ + targetUrl, + algorithm + }) + }, (err, data) => { + if (err) return reject(err); + try { + const response = JSON.parse(data.Payload); + if (response.statusCode !== 200) return reject(new Error('non-200 status code received from hasher function')); + if (!response.hash) return reject(new Error('Successful lambda call but failed to get valid hash')); + resolve(response.hash); + } 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; + } };