瀏覽代碼

Chore: cache GC

SukkaW 1 年之前
父節點
當前提交
9299bc6838
共有 5 個文件被更改,包括 47 次插入2 次删除
  1. 0 2
      Build/build-common.ts
  2. 2 0
      Build/index.ts
  3. 36 0
      Build/lib/make-fetch-happen.ts
  4. 1 0
      package.json
  5. 8 0
      pnpm-lock.yaml

+ 0 - 2
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}`);
       }

+ 2 - 0
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();
 

+ 36 - 0
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, /**

+ 1 - 0
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": {

+ 8 - 0
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: {}