Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/logs/common/logsDataCleaner.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 { Disposable } from '../../../../base/common/lifecycle.js';
7
import { IFileService } from '../../../../platform/files/common/files.js';
8
import { basename, dirname } from '../../../../base/common/resources.js';
9
import { IWorkbenchEnvironmentService } from '../../../services/environment/common/environmentService.js';
10
import { ILifecycleService } from '../../../services/lifecycle/common/lifecycle.js';
11
import { Promises } from '../../../../base/common/async.js';
12
13
export class LogsDataCleaner extends Disposable {
14
15
constructor(
16
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
17
@IFileService private readonly fileService: IFileService,
18
@ILifecycleService private readonly lifecycleService: ILifecycleService,
19
) {
20
super();
21
this.cleanUpOldLogsSoon();
22
}
23
24
private cleanUpOldLogsSoon(): void {
25
let handle: Timeout | undefined = setTimeout(async () => {
26
handle = undefined;
27
const stat = await this.fileService.resolve(dirname(this.environmentService.logsHome));
28
if (stat.children) {
29
const currentLog = basename(this.environmentService.logsHome);
30
const allSessions = stat.children.filter(stat => stat.isDirectory && /^\d{8}T\d{6}$/.test(stat.name));
31
const oldSessions = allSessions.sort().filter((d, i) => d.name !== currentLog);
32
const toDelete = oldSessions.slice(0, Math.max(0, oldSessions.length - 49));
33
Promises.settled(toDelete.map(stat => this.fileService.del(stat.resource, { recursive: true })));
34
}
35
}, 10 * 1000);
36
this.lifecycleService.onWillShutdown(() => {
37
if (handle) {
38
clearTimeout(handle);
39
handle = undefined;
40
}
41
});
42
}
43
}
44
45