Skip to content

Commit 6e8ad91

Browse files
committed
address byk's comments
1 parent bef41c2 commit 6e8ad91

File tree

2 files changed

+17
-60
lines changed

2 files changed

+17
-60
lines changed

scripts/generate-md-exports.mjs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ async function uploadToCFR2(s3Client, relativePath, data) {
5858
return;
5959
}
6060

61-
function taskFinishHandler(
62-
{id, success, failedTasks, usedCacheFiles},
63-
allUsedCacheFiles
64-
) {
65-
// Collect cache files used by this worker
66-
if (usedCacheFiles) {
67-
usedCacheFiles.forEach(file => allUsedCacheFiles.add(file));
61+
// Global set to track which cache files are used across all workers
62+
let globalUsedCacheFiles = null;
63+
64+
function taskFinishHandler({id, success, failedTasks, usedCacheFiles}) {
65+
// Collect cache files used by this worker into the global set
66+
if (usedCacheFiles && globalUsedCacheFiles) {
67+
usedCacheFiles.forEach(file => globalUsedCacheFiles.add(file));
6868
}
6969

7070
if (failedTasks.length === 0) {
@@ -104,7 +104,7 @@ async function createWork() {
104104
}
105105

106106
// Track which cache files are used during this build
107-
const usedCacheFiles = new Set();
107+
globalUsedCacheFiles = new Set();
108108

109109
// On a 16-core machine, 8 workers were optimal (and slightly faster than 16)
110110
const numWorkers = Math.max(Math.floor(cpus().length / 2), 2);
@@ -174,7 +174,7 @@ async function createWork() {
174174
},
175175
});
176176
let hasErrors = false;
177-
worker.on('message', data => (hasErrors = taskFinishHandler(data, usedCacheFiles)));
177+
worker.on('message', data => (hasErrors = taskFinishHandler(data)));
178178
worker.on('error', reject);
179179
worker.on('exit', code => {
180180
if (code !== 0) {
@@ -195,7 +195,7 @@ async function createWork() {
195195
noCache,
196196
usedCacheFiles: mainThreadUsedFiles,
197197
}).then(data => {
198-
if (taskFinishHandler(data, usedCacheFiles)) {
198+
if (taskFinishHandler(data)) {
199199
throw new Error(`Worker[${data.id}] had some errors.`);
200200
}
201201
})
@@ -207,17 +207,13 @@ async function createWork() {
207207
if (!noCache) {
208208
try {
209209
const allFiles = await readdir(CACHE_DIR);
210-
let cleanedCount = 0;
211-
212-
for (const file of allFiles) {
213-
if (!usedCacheFiles.has(file)) {
214-
await rm(path.join(CACHE_DIR, file), {force: true});
215-
cleanedCount++;
216-
}
217-
}
210+
const filesToDelete = allFiles.filter(file => !globalUsedCacheFiles.has(file));
218211

219-
if (cleanedCount > 0) {
220-
console.log(`🧹 Cleaned up ${cleanedCount} unused cache files`);
212+
if (filesToDelete.length > 0) {
213+
await Promise.all(
214+
filesToDelete.map(file => rm(path.join(CACHE_DIR, file), {force: true}))
215+
);
216+
console.log(`🧹 Cleaned up ${filesToDelete.length} unused cache files`);
221217
}
222218
} catch (err) {
223219
console.warn('Failed to clean unused cache files:', err);

src/mdx.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import yaml from 'js-yaml';
44
import {bundleMDX} from 'mdx-bundler';
55
import {BinaryLike, createHash} from 'node:crypto';
66
import {createReadStream, createWriteStream, mkdirSync} from 'node:fs';
7-
import {access, cp, mkdir, opendir, readFile, rm, stat} from 'node:fs/promises';
7+
import {access, cp, mkdir, opendir, readFile} from 'node:fs/promises';
88
import path from 'node:path';
99
// @ts-expect-error ts(2305) -- For some reason "compose" is not recognized in the types
1010
import {compose, Readable} from 'node:stream';
@@ -64,45 +64,6 @@ const CACHE_COMPRESS_LEVEL = 4;
6464
const CACHE_DIR = path.join(root, '.next', 'cache', 'mdx-bundler');
6565
if (process.env.CI) {
6666
mkdirSync(CACHE_DIR, {recursive: true});
67-
68-
// Clean up old cache files in background to prevent unbounded growth
69-
// Delete any file not accessed in the last 24 hours (meaning it wasn't used in recent builds)
70-
// This runs once per worker process and doesn't block the build
71-
(async () => {
72-
try {
73-
const MAX_CACHE_AGE_MS = 24 * 60 * 60 * 1000; // 24 hours
74-
const now = Date.now();
75-
let cleanedCount = 0;
76-
77-
const dir = await opendir(CACHE_DIR);
78-
79-
for await (const dirent of dir) {
80-
if (!dirent.isFile() && !dirent.isDirectory()) {
81-
continue;
82-
}
83-
84-
const itemPath = path.join(CACHE_DIR, dirent.name);
85-
try {
86-
const stats = await stat(itemPath);
87-
const age = now - stats.atimeMs; // Time since last access
88-
89-
if (age > MAX_CACHE_AGE_MS) {
90-
await rm(itemPath, {recursive: true, force: true});
91-
cleanedCount++;
92-
}
93-
} catch (err) {
94-
// Skip items we can't stat/delete
95-
}
96-
}
97-
98-
if (cleanedCount > 0) {
99-
// eslint-disable-next-line no-console
100-
console.log(`🧹 MDX cache: Cleaned up ${cleanedCount} unused items (>24h)`);
101-
}
102-
} catch (err) {
103-
// Silently fail - cache cleanup is not critical
104-
}
105-
})();
10667
}
10768

10869
const md5 = (data: BinaryLike) => createHash('md5').update(data).digest('hex');

0 commit comments

Comments
 (0)