|
| 1 | +import { injectable, inject } from 'inversify'; |
| 2 | +import { EventEmitter, Uri } from 'vscode'; |
| 3 | +import { v4 as uuid } from 'uuid'; |
| 4 | +import { IExtensionContext } from '../../../platform/common/types'; |
| 5 | +import { IExtensionSyncActivationService } from '../../../platform/activation/types'; |
| 6 | +import { logger } from '../../../platform/logging'; |
| 7 | +import { DeepnoteConfigurationStorage } from './deepnoteConfigurationStorage'; |
| 8 | +import { |
| 9 | + CreateKernelConfigurationOptions, |
| 10 | + DeepnoteKernelConfiguration, |
| 11 | + DeepnoteKernelConfigurationWithStatus, |
| 12 | + KernelConfigurationStatus |
| 13 | +} from './deepnoteKernelConfiguration'; |
| 14 | +import { IDeepnoteServerStarter, IDeepnoteToolkitInstaller } from '../types'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Manager for Deepnote kernel configurations. |
| 18 | + * Handles CRUD operations and server lifecycle management. |
| 19 | + */ |
| 20 | +@injectable() |
| 21 | +export class DeepnoteConfigurationManager implements IExtensionSyncActivationService { |
| 22 | + private configurations: Map<string, DeepnoteKernelConfiguration> = new Map(); |
| 23 | + private readonly _onDidChangeConfigurations = new EventEmitter<void>(); |
| 24 | + public readonly onDidChangeConfigurations = this._onDidChangeConfigurations.event; |
| 25 | + |
| 26 | + constructor( |
| 27 | + @inject(IExtensionContext) private readonly context: IExtensionContext, |
| 28 | + @inject(DeepnoteConfigurationStorage) private readonly storage: DeepnoteConfigurationStorage, |
| 29 | + @inject(IDeepnoteToolkitInstaller) private readonly toolkitInstaller: IDeepnoteToolkitInstaller, |
| 30 | + @inject(IDeepnoteServerStarter) private readonly serverStarter: IDeepnoteServerStarter |
| 31 | + ) {} |
| 32 | + |
| 33 | + /** |
| 34 | + * Activate the service (called by VS Code on extension activation) |
| 35 | + */ |
| 36 | + public activate(): void { |
| 37 | + this.initialize().catch((error) => { |
| 38 | + logger.error('Failed to activate configuration manager', error); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Initialize the manager by loading configurations from storage |
| 44 | + */ |
| 45 | + private async initialize(): Promise<void> { |
| 46 | + try { |
| 47 | + const configs = await this.storage.loadConfigurations(); |
| 48 | + this.configurations.clear(); |
| 49 | + |
| 50 | + for (const config of configs) { |
| 51 | + this.configurations.set(config.id, config); |
| 52 | + } |
| 53 | + |
| 54 | + logger.info(`Initialized configuration manager with ${this.configurations.size} configurations`); |
| 55 | + } catch (error) { |
| 56 | + logger.error('Failed to initialize configuration manager', error); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a new kernel configuration |
| 62 | + */ |
| 63 | + public async createConfiguration(options: CreateKernelConfigurationOptions): Promise<DeepnoteKernelConfiguration> { |
| 64 | + const id = uuid(); |
| 65 | + const venvPath = Uri.joinPath(this.context.globalStorageUri, 'deepnote-venvs', id); |
| 66 | + |
| 67 | + const configuration: DeepnoteKernelConfiguration = { |
| 68 | + id, |
| 69 | + name: options.name, |
| 70 | + pythonInterpreter: options.pythonInterpreter, |
| 71 | + venvPath, |
| 72 | + createdAt: new Date(), |
| 73 | + lastUsedAt: new Date(), |
| 74 | + packages: options.packages, |
| 75 | + description: options.description |
| 76 | + }; |
| 77 | + |
| 78 | + this.configurations.set(id, configuration); |
| 79 | + await this.persistConfigurations(); |
| 80 | + this._onDidChangeConfigurations.fire(); |
| 81 | + |
| 82 | + logger.info(`Created new kernel configuration: ${configuration.name} (${id})`); |
| 83 | + return configuration; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get all configurations |
| 88 | + */ |
| 89 | + public listConfigurations(): DeepnoteKernelConfiguration[] { |
| 90 | + return Array.from(this.configurations.values()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get a specific configuration by ID |
| 95 | + */ |
| 96 | + public getConfiguration(id: string): DeepnoteKernelConfiguration | undefined { |
| 97 | + return this.configurations.get(id); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get configuration with status information |
| 102 | + */ |
| 103 | + public getConfigurationWithStatus(id: string): DeepnoteKernelConfigurationWithStatus | undefined { |
| 104 | + const config = this.configurations.get(id); |
| 105 | + if (!config) { |
| 106 | + return undefined; |
| 107 | + } |
| 108 | + |
| 109 | + let status: KernelConfigurationStatus; |
| 110 | + if (config.serverInfo) { |
| 111 | + status = KernelConfigurationStatus.Running; |
| 112 | + } else { |
| 113 | + status = KernelConfigurationStatus.Stopped; |
| 114 | + } |
| 115 | + |
| 116 | + return { |
| 117 | + ...config, |
| 118 | + status |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Update a configuration's metadata |
| 124 | + */ |
| 125 | + public async updateConfiguration( |
| 126 | + id: string, |
| 127 | + updates: Partial<Pick<DeepnoteKernelConfiguration, 'name' | 'packages' | 'description'>> |
| 128 | + ): Promise<void> { |
| 129 | + const config = this.configurations.get(id); |
| 130 | + if (!config) { |
| 131 | + throw new Error(`Configuration not found: ${id}`); |
| 132 | + } |
| 133 | + |
| 134 | + if (updates.name !== undefined) { |
| 135 | + config.name = updates.name; |
| 136 | + } |
| 137 | + if (updates.packages !== undefined) { |
| 138 | + config.packages = updates.packages; |
| 139 | + } |
| 140 | + if (updates.description !== undefined) { |
| 141 | + config.description = updates.description; |
| 142 | + } |
| 143 | + |
| 144 | + await this.persistConfigurations(); |
| 145 | + this._onDidChangeConfigurations.fire(); |
| 146 | + |
| 147 | + logger.info(`Updated configuration: ${config.name} (${id})`); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Delete a configuration |
| 152 | + */ |
| 153 | + public async deleteConfiguration(id: string): Promise<void> { |
| 154 | + const config = this.configurations.get(id); |
| 155 | + if (!config) { |
| 156 | + throw new Error(`Configuration not found: ${id}`); |
| 157 | + } |
| 158 | + |
| 159 | + // Stop the server if running |
| 160 | + if (config.serverInfo) { |
| 161 | + await this.stopServer(id); |
| 162 | + } |
| 163 | + |
| 164 | + this.configurations.delete(id); |
| 165 | + await this.persistConfigurations(); |
| 166 | + this._onDidChangeConfigurations.fire(); |
| 167 | + |
| 168 | + logger.info(`Deleted configuration: ${config.name} (${id})`); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Start the Jupyter server for a configuration |
| 173 | + */ |
| 174 | + public async startServer(id: string): Promise<void> { |
| 175 | + const config = this.configurations.get(id); |
| 176 | + if (!config) { |
| 177 | + throw new Error(`Configuration not found: ${id}`); |
| 178 | + } |
| 179 | + |
| 180 | + if (config.serverInfo) { |
| 181 | + logger.info(`Server already running for configuration: ${config.name} (${id})`); |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + try { |
| 186 | + logger.info(`Starting server for configuration: ${config.name} (${id})`); |
| 187 | + |
| 188 | + // First ensure venv is created and toolkit is installed |
| 189 | + await this.toolkitInstaller.ensureVenvAndToolkit(config.pythonInterpreter, config.venvPath); |
| 190 | + |
| 191 | + // Install additional packages if specified |
| 192 | + if (config.packages && config.packages.length > 0) { |
| 193 | + await this.toolkitInstaller.installAdditionalPackages(config.venvPath, config.packages); |
| 194 | + } |
| 195 | + |
| 196 | + // Start the Jupyter server |
| 197 | + const serverInfo = await this.serverStarter.startServer(config.pythonInterpreter, config.venvPath, id); |
| 198 | + |
| 199 | + config.serverInfo = serverInfo; |
| 200 | + config.lastUsedAt = new Date(); |
| 201 | + |
| 202 | + await this.persistConfigurations(); |
| 203 | + this._onDidChangeConfigurations.fire(); |
| 204 | + |
| 205 | + logger.info(`Server started successfully for configuration: ${config.name} (${id})`); |
| 206 | + } catch (error) { |
| 207 | + logger.error(`Failed to start server for configuration: ${config.name} (${id})`, error); |
| 208 | + throw error; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Stop the Jupyter server for a configuration |
| 214 | + */ |
| 215 | + public async stopServer(id: string): Promise<void> { |
| 216 | + const config = this.configurations.get(id); |
| 217 | + if (!config) { |
| 218 | + throw new Error(`Configuration not found: ${id}`); |
| 219 | + } |
| 220 | + |
| 221 | + if (!config.serverInfo) { |
| 222 | + logger.info(`No server running for configuration: ${config.name} (${id})`); |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + try { |
| 227 | + logger.info(`Stopping server for configuration: ${config.name} (${id})`); |
| 228 | + |
| 229 | + await this.serverStarter.stopServer(id); |
| 230 | + |
| 231 | + config.serverInfo = undefined; |
| 232 | + |
| 233 | + await this.persistConfigurations(); |
| 234 | + this._onDidChangeConfigurations.fire(); |
| 235 | + |
| 236 | + logger.info(`Server stopped successfully for configuration: ${config.name} (${id})`); |
| 237 | + } catch (error) { |
| 238 | + logger.error(`Failed to stop server for configuration: ${config.name} (${id})`, error); |
| 239 | + throw error; |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Restart the Jupyter server for a configuration |
| 245 | + */ |
| 246 | + public async restartServer(id: string): Promise<void> { |
| 247 | + logger.info(`Restarting server for configuration: ${id}`); |
| 248 | + await this.stopServer(id); |
| 249 | + await this.startServer(id); |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Update the last used timestamp for a configuration |
| 254 | + */ |
| 255 | + public async updateLastUsed(id: string): Promise<void> { |
| 256 | + const config = this.configurations.get(id); |
| 257 | + if (!config) { |
| 258 | + return; |
| 259 | + } |
| 260 | + |
| 261 | + config.lastUsedAt = new Date(); |
| 262 | + await this.persistConfigurations(); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Persist all configurations to storage |
| 267 | + */ |
| 268 | + private async persistConfigurations(): Promise<void> { |
| 269 | + const configs = Array.from(this.configurations.values()); |
| 270 | + await this.storage.saveConfigurations(configs); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * Dispose of all resources |
| 275 | + */ |
| 276 | + public dispose(): void { |
| 277 | + this._onDidChangeConfigurations.dispose(); |
| 278 | + } |
| 279 | +} |
0 commit comments