From 604c8eb3feaacb5d2e6bd7132ab41706862711e6 Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Mon, 1 Nov 2021 12:57:07 -0400 Subject: [PATCH] Clean unused Java workspace storage folders - Clean unused workspace storage folders on startup - Create setting "java.configuration.workspaceCacheLimit" (disabled by default) to allow users to configure maximum number of days to preserve cached data - Fixes #2110 Signed-off-by: Roland Grunberg --- README.md | 3 ++- package.json | 10 ++++++++++ src/extension.ts | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 10243ac4f6..e2abc355a8 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,8 @@ The following settings are supported: * `java.symbols.includeSourceMethodDeclarations` : Include method declarations from source files in symbol search. Defaults to `false`. New in 1.1.0: - * `java.quickfix.showAt"` : Show quickfixes at the problem or line level. + * `java.quickfix.showAt` : Show quickfixes at the problem or line level. + * `java.configuration.workspaceCacheLimit` : The number of days (if enabled) to keep unused workspace cache data. Beyond this limit, cached workspace data may be removed. Semantic Highlighting diff --git a/package.json b/package.json index 904b0043ae..ac54ab9a0c 100644 --- a/package.json +++ b/package.json @@ -343,6 +343,16 @@ "description": "Specifies severity if the plugin execution is not covered by Maven build lifecycle.", "scope": "window" }, + "java.configuration.workspaceCacheLimit": { + "type": [ + "null", + "integer" + ], + "default": null, + "minimum": 1, + "description": "The number of days (if enabled) to keep unused workspace cache data. Beyond this limit, cached workspace data may be removed.", + "scope": "application" + }, "java.format.enabled": { "type": "boolean", "default": true, diff --git a/src/extension.ts b/src/extension.ts index 32f1a29219..d014e2e6dd 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -32,7 +32,8 @@ const standardClient: StandardLanguageClient = new StandardLanguageClient(); const jdtEventEmitter = new EventEmitter(); const cleanWorkspaceFileName = '.cleanWorkspace'; const extensionName = 'Language Support for Java'; -let clientLogFile; +let storagePath: string; +let clientLogFile: string; export class ClientErrorHandler implements ErrorHandler { private restarts: number[]; @@ -122,7 +123,7 @@ export function activate(context: ExtensionContext): Promise { markdownPreviewProvider.show(context.asAbsolutePath(path.join('document', `_java.notCoveredExecution.md`)), 'Not Covered Maven Plugin Execution', "", context); })); - let storagePath = context.storagePath; + storagePath = context.storagePath; if (!storagePath) { storagePath = getTempWorkspace(); } @@ -133,6 +134,8 @@ export function activate(context: ExtensionContext): Promise { initializeRecommendation(context); + cleanJavaWorkspaceStorage(); + return requirements.resolveRequirements(context).catch(error => { // show error window.showErrorMessage(error.message, error.label).then((selection) => { @@ -925,3 +928,29 @@ function isPrefix(parentPath: string, childPath: string): boolean { const relative = path.relative(parentPath, childPath); return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative); } +async function cleanJavaWorkspaceStorage() { + const configCacheLimit = getJavaConfiguration().get("configuration.workspaceCacheLimit"); + + if (!storagePath || !configCacheLimit) { + return; + } + + const limit: number = configCacheLimit * 86400000; // days to ms + const currTime = new Date().valueOf(); // ms since Epoch + // storage path is Code/User/workspaceStorage/${id}/redhat.java/ + const wsRoot = path.dirname(path.dirname(storagePath)); + + // find all folders of the form "redhat.java/jdt_ws/" and delete "redhat.java/" + if (fs.existsSync(wsRoot)) { + new glob.Glob(`${wsRoot}/**/jdt_ws`, (_err, matches) => { + for (const javaWSCache of matches) { + const entry = path.dirname(javaWSCache); + const entryModTime = fs.statSync(entry).mtimeMs; + if ((currTime - entryModTime) > limit) { + logger.info(`Removing workspace storage folder : ${entry}`); + deleteDirectory(entry); + } + } + }); + } +}