-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathjupyterExecution.ts
242 lines (222 loc) · 11.2 KB
/
jupyterExecution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as urlPath from '../../../platform/vscode-path/resources';
import * as uuid from 'uuid/v4';
import { CancellationToken, Uri } from 'vscode';
import { IWorkspaceService } from '../../../platform/common/application/types';
import { Cancellation } from '../../../platform/common/cancellation';
import { traceInfo } from '../../../platform/logging';
import { IDisposableRegistry, IConfigurationService, Resource } from '../../../platform/common/types';
import { DataScience } from '../../../platform/common/utils/localize';
import { JupyterSelfCertsError } from '../../../platform/errors/jupyterSelfCertsError';
import { JupyterWaitForIdleError } from '../../../platform/errors/jupyterWaitForIdleError';
import { IInterpreterService } from '../../../platform/interpreter/contracts';
import { PythonEnvironment } from '../../../platform/pythonEnvironments/info';
import { sendTelemetryEvent, captureTelemetry } from '../../../telemetry';
import { Telemetry } from '../../../webviews/webview-side/common/constants';
import { expandWorkingDir } from '../jupyterUtils';
import { IJupyterConnection } from '../../types';
import {
IJupyterExecution,
INotebookServerOptions,
INotebookServer,
INotebookStarter,
INotebookServerFactory
} from '../types';
import { IJupyterSubCommandExecutionService } from '../types.node';
import { JupyterConnection } from '../jupyterConnection';
import { RemoteJupyterServerConnectionError } from '../../../platform/errors/remoteJupyterServerConnectionError';
import { LocalJupyterServerConnectionError } from '../../../platform/errors/localJupyterServerConnectionError';
import { JupyterSelfCertsExpiredError } from '../../../platform/errors/jupyterSelfCertsExpiredError';
const LocalHosts = ['localhost', '127.0.0.1', '::1'];
export class JupyterExecutionBase implements IJupyterExecution {
private usablePythonInterpreter: PythonEnvironment | undefined;
private disposed: boolean = false;
constructor(
private readonly interpreterService: IInterpreterService,
private readonly disposableRegistry: IDisposableRegistry,
private readonly workspace: IWorkspaceService,
private readonly configuration: IConfigurationService,
private readonly notebookStarter: INotebookStarter | undefined,
private readonly jupyterInterpreterService: IJupyterSubCommandExecutionService | undefined,
private readonly notebookServerFactory: INotebookServerFactory,
private readonly jupyterConnection: JupyterConnection
) {
this.disposableRegistry.push(this.interpreterService.onDidChangeInterpreter(() => this.onSettingsChanged()));
this.disposableRegistry.push(this);
if (workspace) {
workspace.onDidChangeConfiguration(
(e) => {
if (e.affectsConfiguration('python.dataScience', undefined)) {
// When config changes happen, recreate our commands.
this.onSettingsChanged();
}
},
this,
this.disposableRegistry
);
}
}
public dispose(): Promise<void> {
this.disposed = true;
return Promise.resolve();
}
public async refreshCommands(): Promise<void> {
await this.jupyterInterpreterService?.refreshCommands();
}
public async isNotebookSupported(cancelToken?: CancellationToken): Promise<boolean> {
// See if we can find the command notebook
return this.jupyterInterpreterService ? this.jupyterInterpreterService.isNotebookSupported(cancelToken) : false;
}
public async getNotebookError(): Promise<string> {
return this.jupyterInterpreterService
? this.jupyterInterpreterService.getReasonForJupyterNotebookNotBeingSupported()
: DataScience.webNotSupported();
}
public async getUsableJupyterPython(cancelToken?: CancellationToken): Promise<PythonEnvironment | undefined> {
// Only try to compute this once.
if (!this.usablePythonInterpreter && !this.disposed && this.jupyterInterpreterService) {
this.usablePythonInterpreter = await Cancellation.race(
() => this.jupyterInterpreterService!.getSelectedInterpreter(cancelToken),
cancelToken
);
}
return this.usablePythonInterpreter;
}
/* eslint-disable complexity, */
public connectToNotebookServer(
options: INotebookServerOptions,
cancelToken: CancellationToken
): Promise<INotebookServer> {
// Return nothing if we cancel
// eslint-disable-next-line
return Cancellation.race(async () => {
let result: INotebookServer | undefined;
let connection: IJupyterConnection | undefined;
traceInfo(`Connecting to server`);
// Try to connect to our jupyter process. Check our setting for the number of tries
let tryCount = 1;
const maxTries = Math.max(1, this.configuration.getSettings(undefined).jupyterLaunchRetries);
let lastTryError: Error;
while (tryCount <= maxTries && !this.disposed) {
try {
// Start or connect to the process
connection = await this.startOrConnect(options, cancelToken);
if (!connection.localLaunch && LocalHosts.includes(connection.hostName.toLowerCase())) {
sendTelemetryEvent(Telemetry.ConnectRemoteJupyterViaLocalHost);
}
// eslint-disable-next-line no-constant-condition
traceInfo(`Connecting to process server`);
// Create a server tha t we will then attempt to connect to.
result = await this.notebookServerFactory.createNotebookServer(connection);
traceInfo(`Connection complete server`);
sendTelemetryEvent(
options.localJupyter ? Telemetry.ConnectLocalJupyter : Telemetry.ConnectRemoteJupyter
);
return result;
} catch (err) {
lastTryError = err;
// Cleanup after ourselves. server may be running partially.
if (result) {
traceInfo(`Killing server because of error ${err}`);
await result.dispose();
}
if (err instanceof JupyterWaitForIdleError && tryCount < maxTries) {
// Special case. This sometimes happens where jupyter doesn't ever connect. Cleanup after
// ourselves and propagate the failure outwards.
traceInfo('Retry because of wait for idle problem.');
// Close existing connection.
connection?.dispose();
tryCount += 1;
} else if (connection) {
// If this is occurring during shutdown, don't worry about it.
if (this.disposed) {
throw err;
}
// Something else went wrong
if (!options.localJupyter) {
sendTelemetryEvent(Telemetry.ConnectRemoteFailedJupyter, undefined, undefined, err, true);
// Check for the self signed certs error specifically
if (JupyterSelfCertsError.isSelfCertsError(err)) {
sendTelemetryEvent(Telemetry.ConnectRemoteSelfCertFailedJupyter);
throw new JupyterSelfCertsError(connection.baseUrl);
} else if (JupyterSelfCertsExpiredError.isSelfCertsExpiredError(err)) {
sendTelemetryEvent(Telemetry.ConnectRemoteExpiredCertFailedJupyter);
throw new JupyterSelfCertsExpiredError(connection.baseUrl);
} else {
throw new RemoteJupyterServerConnectionError(connection.baseUrl, options.serverId, err);
}
} else {
sendTelemetryEvent(Telemetry.ConnectFailedJupyter, undefined, undefined, err, true);
throw new LocalJupyterServerConnectionError(err);
}
} else {
throw err;
}
}
throw lastTryError;
}
throw new Error('Max number of attempts reached');
}, cancelToken);
}
public getServer(_options: INotebookServerOptions): Promise<INotebookServer | undefined> {
// This is cached at the host or guest level
return Promise.resolve(undefined);
}
private async startOrConnect(
options: INotebookServerOptions,
cancelToken: CancellationToken
): Promise<IJupyterConnection> {
// If our uri is undefined or if it's set to local launch we need to launch a server locally
if (options.localJupyter) {
// If that works, then attempt to start the server
traceInfo(`Launching server`);
const settings = this.configuration.getSettings(options.resource);
const useDefaultConfig = settings.useDefaultConfigForJupyter;
const workingDir = await this.workspace.computeWorkingDirectory(options.resource);
// Expand the working directory. Create a dummy launching file in the root path (so we expand correctly)
const workingDirectory = expandWorkingDir(
workingDir,
this.workspace.rootFolder ? urlPath.joinPath(this.workspace.rootFolder, `${uuid()}.txt`) : undefined,
this.workspace,
settings
);
return this.startNotebookServer(
options.resource,
useDefaultConfig,
this.configuration.getSettings(undefined).jupyterCommandLineArguments,
Uri.file(workingDirectory),
cancelToken
);
} else {
// If we have a URI spec up a connection info for it
return this.jupyterConnection.createConnectionInfo(options.serverId);
}
}
// eslint-disable-next-line
@captureTelemetry(Telemetry.StartJupyter)
private async startNotebookServer(
resource: Resource,
useDefaultConfig: boolean,
customCommandLine: string[],
workingDirectory: Uri,
cancelToken: CancellationToken
): Promise<IJupyterConnection> {
if (!this.notebookStarter) {
// In desktop mode this must be defined, in web this code path never gets executed.
throw new Error('Notebook Starter cannot be undefined');
}
return this.notebookStarter!.start(
resource,
useDefaultConfig,
customCommandLine,
workingDirectory,
cancelToken
);
}
private onSettingsChanged() {
// Clear our usableJupyterInterpreter so that we recompute our values
this.usablePythonInterpreter = undefined;
}
}