Skip to content

Commit

Permalink
chore(release-check): add retries (#19336)
Browse files Browse the repository at this point in the history
The `find-latest-release` script occasionally times out on PR builds.

Add sleeps and retries. The retries are pretty hefty because the API
limits are quite low. We can't just toss a GitHub Token in there, as
anyone who submits a PR would have access to it. This is the cheapest
fix for now.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr authored Mar 11, 2022
1 parent 2e81891 commit d91b2e2
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions scripts/find-latest-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,33 @@ async function main(matchRange) {
throw new Error(`Not a valid range: ${matchRange}`);
}

const { stdout, error } = cp.spawnSync('curl', ['https://api.github.com/repos/aws/aws-cdk/releases?per_page=100'], { maxBuffer: 10_000_000 });
if (error) { throw error; }
const releases = JSON.parse(stdout);
let releases;
for (let attempt = 0; attempt < 10; attempt++) {
const { stdout, error } = cp.spawnSync('curl', ['https://api.github.com/repos/aws/aws-cdk/releases?per_page=100'], { maxBuffer: 10_000_000 });
if (error) { throw error; }

const response = JSON.parse(stdout);
if (response.message) {
// This is actually an error response. Only recover from throttling errors.
if (!response.message.includes('API rate limit')) {
throw new Error(response.message);
}

// 60 requests/hour, so we need to sleep for a full minute to get any kind of response
const sleepTime = Math.floor(Math.random() * 60 * Math.pow(2, attempt));
console.log('Sleeping for', sleepTime, 'seconds');
await sleep(sleepTime * 1000);
continue;
}

releases = response;
break;
}
if (!releases) {
throw new Error('Retries exhaused');
}


const versions = releases.map(r => r.name.replace(/^v/, '')); // v1.2.3 -> 1.2.3

const sat = semver.maxSatisfying(versions, range);
Expand All @@ -28,7 +52,11 @@ async function main(matchRange) {
console.log(sat);
}

function sleep(ms) {
return new Promise(ok => setTimeout(ok, ms));
}

main(process.argv[2]).catch(e => {
console.error(e);
process.exitCode = 1;
});
});

0 comments on commit d91b2e2

Please sign in to comment.