|
| 1 | +import type { ExtensionContext } from "vscode"; |
| 2 | +import { getDirectoryNamesInsidePath, isIOError } from "../../common/files"; |
| 3 | +import { sleep } from "../../common/time"; |
| 4 | +import type { BaseLogger } from "../../common/logging"; |
| 5 | +import { join } from "path"; |
| 6 | +import { getErrorMessage } from "../../common/helpers-pure"; |
| 7 | +import { pathExists, remove } from "fs-extra"; |
| 8 | + |
| 9 | +interface ExtensionManagedDistributionManager { |
| 10 | + folderIndex: number; |
| 11 | + distributionFolderPrefix: string; |
| 12 | +} |
| 13 | + |
| 14 | +interface DistributionDirectory { |
| 15 | + directoryName: string; |
| 16 | + folderIndex: number; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * This class is responsible for cleaning up old distributions that are no longer needed. In normal operation, this |
| 21 | + * should not be necessary as the old distribution is deleted when the distribution is updated. However, in some cases |
| 22 | + * the extension may leave behind old distribution which can result in a significant amount of space (> 100 GB) being |
| 23 | + * taking up by unused distributions. |
| 24 | + */ |
| 25 | +export class ExtensionManagedDistributionCleaner { |
| 26 | + constructor( |
| 27 | + private readonly extensionContext: ExtensionContext, |
| 28 | + private readonly logger: BaseLogger, |
| 29 | + private readonly manager: ExtensionManagedDistributionManager, |
| 30 | + ) {} |
| 31 | + |
| 32 | + public start() { |
| 33 | + // Intentionally starting this without waiting for it |
| 34 | + void this.cleanup().catch((e: unknown) => { |
| 35 | + void this.logger.log( |
| 36 | + `Failed to clean up old versions of the CLI: ${getErrorMessage(e)}`, |
| 37 | + ); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + public async cleanup() { |
| 42 | + if (!(await pathExists(this.extensionContext.globalStorageUri.fsPath))) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const currentFolderIndex = this.manager.folderIndex; |
| 47 | + |
| 48 | + const distributionDirectoryRegex = new RegExp( |
| 49 | + `^${this.manager.distributionFolderPrefix}(\\d+)$`, |
| 50 | + ); |
| 51 | + |
| 52 | + const existingDirectories = await getDirectoryNamesInsidePath( |
| 53 | + this.extensionContext.globalStorageUri.fsPath, |
| 54 | + ); |
| 55 | + const distributionDirectories = existingDirectories |
| 56 | + .map((dir): DistributionDirectory | null => { |
| 57 | + const match = dir.match(distributionDirectoryRegex); |
| 58 | + if (!match) { |
| 59 | + // When the folderIndex is 0, the distributionFolderPrefix is used as the directory name |
| 60 | + if (dir === this.manager.distributionFolderPrefix) { |
| 61 | + return { |
| 62 | + directoryName: dir, |
| 63 | + folderIndex: 0, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + directoryName: dir, |
| 72 | + folderIndex: parseInt(match[1]), |
| 73 | + }; |
| 74 | + }) |
| 75 | + .filter((dir) => dir !== null); |
| 76 | + |
| 77 | + // Clean up all directories that are older than the current one |
| 78 | + const cleanableDirectories = distributionDirectories.filter( |
| 79 | + (dir) => dir.folderIndex < currentFolderIndex, |
| 80 | + ); |
| 81 | + |
| 82 | + if (cleanableDirectories.length === 0) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Shuffle the array so that multiple VS Code processes don't all try to clean up the same directory at the same time |
| 87 | + for (let i = cleanableDirectories.length - 1; i > 0; i--) { |
| 88 | + const j = Math.floor(Math.random() * (i + 1)); |
| 89 | + [cleanableDirectories[i], cleanableDirectories[j]] = [ |
| 90 | + cleanableDirectories[j], |
| 91 | + cleanableDirectories[i], |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + void this.logger.log( |
| 96 | + `Cleaning up ${cleanableDirectories.length} old versions of the CLI.`, |
| 97 | + ); |
| 98 | + |
| 99 | + for (const cleanableDirectory of cleanableDirectories) { |
| 100 | + // Wait 10 seconds between each cleanup to avoid overloading the system (even though the remove call should be async) |
| 101 | + await sleep(10_000); |
| 102 | + |
| 103 | + const path = join( |
| 104 | + this.extensionContext.globalStorageUri.fsPath, |
| 105 | + cleanableDirectory.directoryName, |
| 106 | + ); |
| 107 | + |
| 108 | + // Delete this directory |
| 109 | + try { |
| 110 | + await remove(path); |
| 111 | + } catch (e) { |
| 112 | + if (isIOError(e) && e.code === "ENOENT") { |
| 113 | + // If the directory doesn't exist, that's fine |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + void this.logger.log( |
| 118 | + `Tried to clean up an old version of the CLI at ${path} but encountered an error: ${getErrorMessage(e)}.`, |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + void this.logger.log( |
| 124 | + `Cleaned up ${cleanableDirectories.length} old versions of the CLI.`, |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments