Path: blob/main/src/vs/workbench/contrib/logs/common/logsDataCleaner.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Disposable } from '../../../../base/common/lifecycle.js';6import { IFileService } from '../../../../platform/files/common/files.js';7import { basename, dirname } from '../../../../base/common/resources.js';8import { IWorkbenchEnvironmentService } from '../../../services/environment/common/environmentService.js';9import { ILifecycleService } from '../../../services/lifecycle/common/lifecycle.js';10import { Promises } from '../../../../base/common/async.js';1112export class LogsDataCleaner extends Disposable {1314constructor(15@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,16@IFileService private readonly fileService: IFileService,17@ILifecycleService private readonly lifecycleService: ILifecycleService,18) {19super();20this.cleanUpOldLogsSoon();21}2223private cleanUpOldLogsSoon(): void {24let handle: Timeout | undefined = setTimeout(async () => {25handle = undefined;26const stat = await this.fileService.resolve(dirname(this.environmentService.logsHome));27if (stat.children) {28const currentLog = basename(this.environmentService.logsHome);29const allSessions = stat.children.filter(stat => stat.isDirectory && /^\d{8}T\d{6}$/.test(stat.name));30const oldSessions = allSessions.sort().filter((d, i) => d.name !== currentLog);31const toDelete = oldSessions.slice(0, Math.max(0, oldSessions.length - 49));32Promises.settled(toDelete.map(stat => this.fileService.del(stat.resource, { recursive: true })));33}34}, 10 * 1000);35this.lifecycleService.onWillShutdown(() => {36if (handle) {37clearTimeout(handle);38handle = undefined;39}40});41}42}434445