From 9299bc6838300e92ef86e1d511055026fc7ba14a Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 21 Nov 2024 10:59:13 +0800 Subject: [PATCH] Chore: cache GC --- Build/build-common.ts | 2 -- Build/index.ts | 2 ++ Build/lib/make-fetch-happen.ts | 36 ++++++++++++++++++++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 8 ++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Build/build-common.ts b/Build/build-common.ts index 0d76a821..393f9f8f 100644 --- a/Build/build-common.ts +++ b/Build/build-common.ts @@ -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}`); } diff --git a/Build/index.ts b/Build/index.ts index 136eaba0..fccdea59 100644 --- a/Build/index.ts +++ b/Build/index.ts @@ -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(); diff --git a/Build/lib/make-fetch-happen.ts b/Build/lib/make-fetch-happen.ts index 9e975501..d5529729 100644 --- a/Build/lib/make-fetch-happen.ts +++ b/Build/lib/make-fetch-happen.ts @@ -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, /** diff --git a/package.json b/package.json index 8a425f56..451ca18c 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "undici": "^6.21.0", "whoiser": "^1.18.0", "why-is-node-running": "^3.2.1", + "xbits": "^0.2.0", "yaml": "^2.6.1" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 912883a1..9eabc23a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -100,6 +100,9 @@ importers: why-is-node-running: specifier: ^3.2.1 version: 3.2.1 + xbits: + specifier: ^0.2.0 + version: 0.2.0 yaml: specifier: ^2.6.1 version: 2.6.1 @@ -1899,6 +1902,9 @@ packages: wrappy@1.0.2: resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==} + xbits@0.2.0: + resolution: {integrity: sha512-JMd+tT2WwmbQZxIE9lQ3WoziWgTngKwdMbuk1CBqjos2zn9y5LYYSGRkYqzsFlwITHJehpdHgDdTOf4Qls/Q+w==} + y18n@5.0.8: resolution: {integrity: sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==} engines: {node: '>=10'} @@ -3786,6 +3792,8 @@ snapshots: wrappy@1.0.2: {} + xbits@0.2.0: {} + y18n@5.0.8: {} yallist@4.0.0: {}