mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-04 17:23:45 -05:00
Merge from vscode e3c4990c67c40213af168300d1cfeb71d680f877 (#16569)
This commit is contained in:
@@ -3,13 +3,14 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { promises } from 'fs';
|
||||
import { INativeEnvironmentService } from 'vs/platform/environment/common/environment';
|
||||
import { join } from 'vs/base/common/path';
|
||||
import { readdir, rimraf } from 'vs/base/node/pfs';
|
||||
import { Promises } from 'vs/base/node/pfs';
|
||||
import { onUnexpectedError } from 'vs/base/common/errors';
|
||||
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { IBackupWorkspacesFormat } from 'vs/platform/backup/node/backup';
|
||||
import { RunOnceScheduler } from 'vs/base/common/async';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
|
||||
export class StorageDataCleaner extends Disposable {
|
||||
|
||||
@@ -18,52 +19,44 @@ export class StorageDataCleaner extends Disposable {
|
||||
|
||||
constructor(
|
||||
private readonly backupWorkspacesPath: string,
|
||||
@INativeEnvironmentService private readonly environmentService: INativeEnvironmentService
|
||||
@INativeEnvironmentService private readonly environmentService: INativeEnvironmentService,
|
||||
@ILogService private readonly logService: ILogService
|
||||
) {
|
||||
super();
|
||||
|
||||
this.cleanUpStorageSoon();
|
||||
const scheduler = this._register(new RunOnceScheduler(() => {
|
||||
this.cleanUpStorage();
|
||||
}, 30 * 1000 /* after 30s */));
|
||||
scheduler.schedule();
|
||||
}
|
||||
|
||||
private cleanUpStorageSoon(): void {
|
||||
let handle: NodeJS.Timeout | undefined = setTimeout(() => {
|
||||
handle = undefined;
|
||||
private async cleanUpStorage(): Promise<void> {
|
||||
this.logService.info('[storage cleanup]: Starting to clean up storage folders.');
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
// Leverage the backup workspace file to find out which empty workspace is currently in use to
|
||||
// determine which empty workspace storage can safely be deleted
|
||||
const contents = await promises.readFile(this.backupWorkspacesPath, 'utf8');
|
||||
try {
|
||||
|
||||
const workspaces = JSON.parse(contents) as IBackupWorkspacesFormat;
|
||||
const emptyWorkspaces = workspaces.emptyWorkspaceInfos.map(info => info.backupFolder);
|
||||
// Leverage the backup workspace file to find out which empty workspace is currently in use to
|
||||
// determine which empty workspace storage can safely be deleted
|
||||
const contents = await Promises.readFile(this.backupWorkspacesPath, 'utf8');
|
||||
|
||||
// Read all workspace storage folders that exist
|
||||
const storageFolders = await readdir(this.environmentService.workspaceStorageHome.fsPath);
|
||||
const deletes: Promise<void>[] = [];
|
||||
const workspaces = JSON.parse(contents) as IBackupWorkspacesFormat;
|
||||
const emptyWorkspaces = workspaces.emptyWorkspaceInfos.map(emptyWorkspace => emptyWorkspace.backupFolder);
|
||||
|
||||
storageFolders.forEach(storageFolder => {
|
||||
if (storageFolder.length === StorageDataCleaner.NON_EMPTY_WORKSPACE_ID_LENGTH) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (emptyWorkspaces.indexOf(storageFolder) === -1) {
|
||||
deletes.push(rimraf(join(this.environmentService.workspaceStorageHome.fsPath, storageFolder)));
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(deletes);
|
||||
} catch (error) {
|
||||
onUnexpectedError(error);
|
||||
// Read all workspace storage folders that exist
|
||||
const storageFolders = await Promises.readdir(this.environmentService.workspaceStorageHome.fsPath);
|
||||
await Promise.all(storageFolders.map(async storageFolder => {
|
||||
if (storageFolder.length === StorageDataCleaner.NON_EMPTY_WORKSPACE_ID_LENGTH) {
|
||||
return;
|
||||
}
|
||||
})();
|
||||
}, 30 * 1000);
|
||||
|
||||
this._register(toDisposable(() => {
|
||||
if (handle) {
|
||||
clearTimeout(handle);
|
||||
handle = undefined;
|
||||
}
|
||||
}));
|
||||
if (emptyWorkspaces.indexOf(storageFolder) === -1) {
|
||||
this.logService.info(`[storage cleanup]: Deleting storage folder ${storageFolder}.`);
|
||||
|
||||
await Promises.rm(join(this.environmentService.workspaceStorageHome.fsPath, storageFolder));
|
||||
}
|
||||
}));
|
||||
} catch (error) {
|
||||
onUnexpectedError(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user