Chore: cache GC

This commit is contained in:
SukkaW
2024-11-21 10:59:13 +08:00
parent abb32bf7c2
commit 9299bc6838
5 changed files with 47 additions and 2 deletions

View File

@@ -154,8 +154,6 @@ async function transformRuleset(parentSpan: Span, sourcePath: string, relativePa
const id = basename;
const [type] = relativePath.slice(0, -extname.length).split(path.sep);
console.log({ relativePath, basename, id, type });
if (type !== 'ip' && type !== 'non_ip') {
throw new TypeError(`Invalid type: ${type}`);
}

View File

@@ -27,6 +27,7 @@ import { buildCloudMounterRules } from './build-cloudmounter-rules';
import { createSpan, printTraceResult, whyIsNodeRunning } from './trace';
import { buildDeprecateFiles } from './build-deprecate-files';
import { cacheGc } from './lib/make-fetch-happen';
process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error);
@@ -115,6 +116,7 @@ process.on('unhandledRejection', (reason) => {
await buildDeprecateFiles(rootSpan);
await buildPublic(rootSpan);
await cacheGc(rootSpan);
rootSpan.stop();

View File

@@ -6,12 +6,48 @@ import cacache from 'cacache';
import picocolors from 'picocolors';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports -- type only
import type { Response as NodeFetchResponse } from 'node-fetch';
import { task } from '../trace';
import { bytes } from 'xbits';
export type { NodeFetchResponse };
const cachePath = path.resolve(__dirname, '../../.cache/__make_fetch_happen__');
fs.mkdirSync(cachePath, { recursive: true });
interface CacacheVerifyStats {
startTime: Date,
endTime: Date,
runTime: {
markStartTime: 0,
fixPerms: number,
garbageCollect: number,
rebuildIndex: number,
cleanTmp: number,
writeVerifile: number,
markEndTime: number,
total: number
},
verifiedContent: number,
reclaimedCount: number,
reclaimedSize: number,
badContentCount: number,
keptSize: number,
missingContent: number,
rejectedEntries: number,
totalEntries: number
}
export const cacheGc = task(require.main === module, __filename)(
(span) => span
.traceChildAsync('cacache gc', () => cacache.verify(cachePath, { concurrency: 64 }))
.then((stats: CacacheVerifyStats) => {
// console.log({ stats });
console.log(picocolors.green('[cacheGc] running gc on cache:'), cachePath);
console.log(picocolors.green('[cacheGc] content verified:'), stats.verifiedContent, '(' + bytes(stats.keptSize) + ')');
console.log(picocolors.green('[cacheGc] reclaimed:'), stats.reclaimedCount, '(' + bytes(stats.reclaimedSize) + ')');
})
);
const _fetch = makeFetchHappen.defaults({
cachePath,
maxSockets: 32, /**