|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { nbformat } from '@jupyterlab/coreutils'; |
| 7 | +import type { KernelMessage } from '@jupyterlab/services'; |
| 8 | +import { Observable } from 'rxjs/Observable'; |
| 9 | +import { Subject } from 'rxjs/Subject'; |
| 10 | +import { CancellationToken, Event, EventEmitter, Uri } from 'vscode'; |
| 11 | +import { ServerStatus } from '../../../../datascience-ui/interactive-common/mainState'; |
| 12 | +import { traceError } from '../../../common/logger'; |
| 13 | +import { IDisposableRegistry } from '../../../common/types'; |
| 14 | +import { createDeferred, Deferred } from '../../../common/utils/async'; |
| 15 | +import { getDefaultNotebookContent, updateNotebookMetadata } from '../../notebookStorage/baseModel'; |
| 16 | +import type { |
| 17 | + ICell, |
| 18 | + IJupyterKernelSpec, |
| 19 | + INotebook, |
| 20 | + INotebookProvider, |
| 21 | + INotebookProviderConnection, |
| 22 | + InterruptResult, |
| 23 | + KernelSocketInformation |
| 24 | +} from '../../types'; |
| 25 | +import type { IKernel, KernelSelection, LiveKernelModel } from './types'; |
| 26 | + |
| 27 | +export class Kernel implements IKernel { |
| 28 | + get connection(): INotebookProviderConnection | undefined { |
| 29 | + return this._notebook?.connection; |
| 30 | + } |
| 31 | + get kernelSpec(): IJupyterKernelSpec | LiveKernelModel | undefined { |
| 32 | + if (this._notebook) { |
| 33 | + return this._notebook.getKernelSpec(); |
| 34 | + } |
| 35 | + return this._metadata.kernelSpec || this._metadata.kernelModel; |
| 36 | + } |
| 37 | + get onStatusChanged(): Event<ServerStatus> { |
| 38 | + return this._onStatusChanged.event; |
| 39 | + } |
| 40 | + get onRestarted(): Event<void> { |
| 41 | + return this._onRestarted.event; |
| 42 | + } |
| 43 | + get onDisposed(): Event<void> { |
| 44 | + return this._onDisposed.event; |
| 45 | + } |
| 46 | + get status(): ServerStatus { |
| 47 | + return this._notebook?.status ?? ServerStatus.NotStarted; |
| 48 | + } |
| 49 | + get disposed(): boolean { |
| 50 | + return this._disposed === true || this._notebook?.disposed === true; |
| 51 | + } |
| 52 | + get kernelSocket(): Observable<KernelSocketInformation | undefined> { |
| 53 | + return this._kernelSocket.asObservable(); |
| 54 | + } |
| 55 | + private _disposed?: boolean; |
| 56 | + private readonly _kernelSocket = new Subject<KernelSocketInformation | undefined>(); |
| 57 | + private readonly _onStatusChanged = new EventEmitter<ServerStatus>(); |
| 58 | + private readonly _onRestarted = new EventEmitter<void>(); |
| 59 | + private readonly _onDisposed = new EventEmitter<void>(); |
| 60 | + private _notebook?: INotebook; |
| 61 | + private _notebookPromise?: Promise<INotebook | undefined>; |
| 62 | + private readonly hookedNotebookForEvents = new WeakSet<INotebook>(); |
| 63 | + private restarting?: Deferred<void>; |
| 64 | + constructor( |
| 65 | + public readonly uri: Uri, |
| 66 | + private readonly _metadata: KernelSelection, |
| 67 | + private readonly notebookProvider: INotebookProvider, |
| 68 | + private readonly disposables: IDisposableRegistry, |
| 69 | + private readonly waitForIdleTimeoutMs: number, |
| 70 | + private readonly launchingFile?: string |
| 71 | + ) {} |
| 72 | + public executeObservable( |
| 73 | + code: string, |
| 74 | + file: string, |
| 75 | + line: number, |
| 76 | + id: string, |
| 77 | + silent: boolean |
| 78 | + ): Observable<ICell[]> { |
| 79 | + if (!this._notebook) { |
| 80 | + throw new Error('executeObservable cannot be called if kernel has not been started!'); |
| 81 | + } |
| 82 | + this._notebook.clear(id); |
| 83 | + return this._notebook.executeObservable(code, file, line, id, silent); |
| 84 | + } |
| 85 | + public async start(options?: { disableUI?: boolean; token?: CancellationToken }): Promise<void> { |
| 86 | + if (this.restarting) { |
| 87 | + await this.restarting.promise; |
| 88 | + } |
| 89 | + if (this._notebookPromise) { |
| 90 | + await this._notebookPromise; |
| 91 | + return; |
| 92 | + } else { |
| 93 | + const metadata = ((getDefaultNotebookContent().metadata || {}) as unknown) as nbformat.INotebookMetadata; |
| 94 | + updateNotebookMetadata( |
| 95 | + metadata, |
| 96 | + this._metadata.interpreter, |
| 97 | + this._metadata.kernelSpec || this._metadata.kernelModel |
| 98 | + ); |
| 99 | + |
| 100 | + this._notebookPromise = this.notebookProvider.getOrCreateNotebook({ |
| 101 | + identity: this.uri, |
| 102 | + resource: this.uri, |
| 103 | + disableUI: options?.disableUI, |
| 104 | + getOnly: false, |
| 105 | + metadata, |
| 106 | + token: options?.token |
| 107 | + }); |
| 108 | + |
| 109 | + this._notebookPromise |
| 110 | + .then((nb) => (this._notebook = nb)) |
| 111 | + .catch((ex) => traceError('failed to create INotebook in kernel', ex)); |
| 112 | + await this._notebookPromise; |
| 113 | + await this.initializeAfterStart(); |
| 114 | + } |
| 115 | + } |
| 116 | + public async interrupt(timeoutInMs: number): Promise<InterruptResult> { |
| 117 | + if (this.restarting) { |
| 118 | + await this.restarting.promise; |
| 119 | + } |
| 120 | + if (!this._notebook) { |
| 121 | + throw new Error('No notebook to interrupt'); |
| 122 | + } |
| 123 | + return this._notebook.interruptKernel(timeoutInMs); |
| 124 | + } |
| 125 | + public async dispose(): Promise<void> { |
| 126 | + this.restarting = undefined; |
| 127 | + if (this._notebook) { |
| 128 | + await this._notebook.dispose(); |
| 129 | + this._disposed = true; |
| 130 | + this._onDisposed.fire(); |
| 131 | + this._onStatusChanged.fire(ServerStatus.Dead); |
| 132 | + this._notebook = undefined; |
| 133 | + } |
| 134 | + } |
| 135 | + public async restart(timeoutInMs: number): Promise<void> { |
| 136 | + if (this.restarting) { |
| 137 | + return this.restarting.promise; |
| 138 | + } |
| 139 | + if (this._notebook) { |
| 140 | + this.restarting = createDeferred<void>(); |
| 141 | + try { |
| 142 | + await this._notebook.restartKernel(timeoutInMs); |
| 143 | + await this.initializeAfterStart(); |
| 144 | + this.restarting.resolve(); |
| 145 | + } catch (ex) { |
| 146 | + this.restarting.reject(ex); |
| 147 | + } finally { |
| 148 | + this.restarting = undefined; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + public registerIOPubListener(listener: (msg: KernelMessage.IIOPubMessage, requestId: string) => void): void { |
| 153 | + if (!this._notebook) { |
| 154 | + throw new Error('Notebook not defined'); |
| 155 | + } |
| 156 | + this._notebook.registerIOPubListener(listener); |
| 157 | + } |
| 158 | + private async initializeAfterStart() { |
| 159 | + if (!this._notebook) { |
| 160 | + return; |
| 161 | + } |
| 162 | + if (!this.hookedNotebookForEvents.has(this._notebook)) { |
| 163 | + this.hookedNotebookForEvents.add(this._notebook); |
| 164 | + this._notebook.kernelSocket.subscribe(this._kernelSocket); |
| 165 | + this._notebook.onDisposed(() => { |
| 166 | + this._onDisposed.fire(); |
| 167 | + }); |
| 168 | + this._notebook.onKernelRestarted(() => { |
| 169 | + this._onRestarted.fire(); |
| 170 | + }); |
| 171 | + this._notebook.onSessionStatusChanged((e) => this._onStatusChanged.fire(e), this, this.disposables); |
| 172 | + } |
| 173 | + if (this.launchingFile) { |
| 174 | + await this._notebook.setLaunchingFile(this.launchingFile); |
| 175 | + } |
| 176 | + await this._notebook.waitForIdle(this.waitForIdleTimeoutMs); |
| 177 | + } |
| 178 | +} |
0 commit comments