Skip to content

Commit cab905b

Browse files
blakeffacebook-github-bot
authored andcommitted
Allow cache reaper to skip failing cache removal request (facebook#45593)
Summary: Pull Request resolved: facebook#45593 I'm guessing either there is a race condition between Github removing cache entries when we're over budget OR there is an eventual consistency issue between reported cache entries and their removal. Either way, this job is best efforts. If a entry targetted for removal isn't there, great. This change prevents the job from stopping if an entry no longer exists. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D60106847 fbshipit-source-id: 252bba7bb0bbb91d279f06a39301491332cd5ace
1 parent d252494 commit cab905b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

scripts/clean-gha-cache.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {execSync} = require('node:child_process');
1010
const CACHE_LIMIT = (10 * 1024 ** 3) * 0.9;
1111
// Doing this to capture node-modules- and the pre-existing node-cache-<platform>-yarn-<sha> entries
1212
const NODE_CACHE_KEY = 'node-';
13+
const NODE_CACHE_KEY_FULL = 'node-modules-';
1314

1415
function cleanData(rawStr) {
1516
const now = new Date();
@@ -33,6 +34,19 @@ function cacheToString(entry) {
3334
return `[${hrs(entry.msSinceLastAccessed).padStart(7)}] ${mb(entry.sizeInBytes).padStart(7)} -> ${entry.key}`;
3435
}
3536

37+
function cleanCache(cmd) {
38+
try {
39+
const msg = execSync(cmd, 'utf8');
40+
return (msg.trim().length > 0) ? msg.trim() : '🪓';
41+
} catch (e) {
42+
// There can be race conditions between github cache cleanups and this script.
43+
if (/Could not find a cache matching/.test(e.message)) {
44+
return 'The cache entry no longer exists, skipping.';
45+
}
46+
return `Failed: '${e.message}', skipping.`;
47+
}
48+
}
49+
3650
function main() {
3751
const cacheUsage = cleanData(execSync(
3852
'gh cache list --sort last_accessed_at --json id,key,createdAt,lastAccessedAt,sizeInBytes --limit 1000',
@@ -51,8 +65,10 @@ function main() {
5165
key.startsWith(NODE_CACHE_KEY) && sizeInBytes > 1024 * 1024
5266
)
5367
.sort((a, b) => b.createdAt - a.createdAt);
54-
// Leave the latest entry only
55-
const keeping = nodeCacheUsage.pop();
68+
// Find the oldest version of node-modules-*. It's still possible that we have legacy node-yarn-*
69+
// entries if there are commits on branches of RN < 0.75-stable, so guard for this:
70+
const idx = nodeCacheUsage.findLastIndex(({key}) => key.startsWith(NODE_CACHE_KEY_FULL));
71+
const keeping = ((idx === -1) ? nodeCacheUsage : nodeCacheUsage.splice(idx, 1)).pop();
5672

5773
console.log('TASK: clean up old node_modules cache entries.', keeping ? `\nkeeping ${cacheToString(keeping)}` : ' Skipping, no cache entries.');
5874
for (const entry of nodeCacheUsage) {
@@ -83,7 +99,7 @@ function main() {
8399
if (dryRun) {
84100
console.warn(`Skip: ${cmd}`);
85101
} else {
86-
console.warn(`${cmd} 🪓 ${execSync(cmd, 'utf8').toString()}`);
102+
console.warn(`${cmd} ${cleanCache(cmd)}`);
87103
}
88104
}
89105
}

0 commit comments

Comments
 (0)