Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/code/electron-utility/sharedProcess/contrib/codeCacheCleaner.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { promises } from 'fs';
7
import { RunOnceScheduler } from '../../../../base/common/async.js';
8
import { onUnexpectedError } from '../../../../base/common/errors.js';
9
import { Disposable } from '../../../../base/common/lifecycle.js';
10
import { basename, dirname, join } from '../../../../base/common/path.js';
11
import { Promises } from '../../../../base/node/pfs.js';
12
import { ILogService } from '../../../../platform/log/common/log.js';
13
import { IProductService } from '../../../../platform/product/common/productService.js';
14
15
export class CodeCacheCleaner extends Disposable {
16
17
private readonly dataMaxAge: number;
18
19
constructor(
20
currentCodeCachePath: string | undefined,
21
@IProductService productService: IProductService,
22
@ILogService private readonly logService: ILogService
23
) {
24
super();
25
26
this.dataMaxAge = productService.quality !== 'stable'
27
? 1000 * 60 * 60 * 24 * 7 // roughly 1 week (insiders)
28
: 1000 * 60 * 60 * 24 * 30 * 3; // roughly 3 months (stable)
29
30
// Cached data is stored as user data and we run a cleanup task every time
31
// the editor starts. The strategy is to delete all files that are older than
32
// 3 months (1 week respectively)
33
if (currentCodeCachePath) {
34
const scheduler = this._register(new RunOnceScheduler(() => {
35
this.cleanUpCodeCaches(currentCodeCachePath);
36
}, 30 * 1000 /* after 30s */));
37
scheduler.schedule();
38
}
39
}
40
41
private async cleanUpCodeCaches(currentCodeCachePath: string): Promise<void> {
42
this.logService.trace('[code cache cleanup]: Starting to clean up old code cache folders.');
43
44
try {
45
const now = Date.now();
46
47
// The folder which contains folders of cached data.
48
// Each of these folders is partioned per commit
49
const codeCacheRootPath = dirname(currentCodeCachePath);
50
const currentCodeCache = basename(currentCodeCachePath);
51
52
const codeCaches = await Promises.readdir(codeCacheRootPath);
53
await Promise.all(codeCaches.map(async codeCache => {
54
if (codeCache === currentCodeCache) {
55
return; // not the current cache folder
56
}
57
58
// Delete cache folder if old enough
59
const codeCacheEntryPath = join(codeCacheRootPath, codeCache);
60
const codeCacheEntryStat = await promises.stat(codeCacheEntryPath);
61
if (codeCacheEntryStat.isDirectory() && (now - codeCacheEntryStat.mtime.getTime()) > this.dataMaxAge) {
62
this.logService.trace(`[code cache cleanup]: Removing code cache folder ${codeCache}.`);
63
64
return Promises.rm(codeCacheEntryPath);
65
}
66
}));
67
} catch (error) {
68
onUnexpectedError(error);
69
}
70
}
71
}
72
73