Skip to content

Commit d91b2e2

Browse files
authored
chore(release-check): add retries (#19336)
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*
1 parent 2e81891 commit d91b2e2

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

scripts/find-latest-release.js

+32-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,33 @@ async function main(matchRange) {
1616
throw new Error(`Not a valid range: ${matchRange}`);
1717
}
1818

19-
const { stdout, error } = cp.spawnSync('curl', ['https://api.github.com/repos/aws/aws-cdk/releases?per_page=100'], { maxBuffer: 10_000_000 });
20-
if (error) { throw error; }
21-
const releases = JSON.parse(stdout);
19+
let releases;
20+
for (let attempt = 0; attempt < 10; attempt++) {
21+
const { stdout, error } = cp.spawnSync('curl', ['https://api.github.com/repos/aws/aws-cdk/releases?per_page=100'], { maxBuffer: 10_000_000 });
22+
if (error) { throw error; }
23+
24+
const response = JSON.parse(stdout);
25+
if (response.message) {
26+
// This is actually an error response. Only recover from throttling errors.
27+
if (!response.message.includes('API rate limit')) {
28+
throw new Error(response.message);
29+
}
30+
31+
// 60 requests/hour, so we need to sleep for a full minute to get any kind of response
32+
const sleepTime = Math.floor(Math.random() * 60 * Math.pow(2, attempt));
33+
console.log('Sleeping for', sleepTime, 'seconds');
34+
await sleep(sleepTime * 1000);
35+
continue;
36+
}
37+
38+
releases = response;
39+
break;
40+
}
41+
if (!releases) {
42+
throw new Error('Retries exhaused');
43+
}
44+
45+
2246
const versions = releases.map(r => r.name.replace(/^v/, '')); // v1.2.3 -> 1.2.3
2347

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

55+
function sleep(ms) {
56+
return new Promise(ok => setTimeout(ok, ms));
57+
}
58+
3159
main(process.argv[2]).catch(e => {
3260
console.error(e);
3361
process.exitCode = 1;
34-
});
62+
});

0 commit comments

Comments
 (0)