Repository navigation
Expand file tree
/
Copy pathhostRawNotebookProvider.ts
More file actions
151 lines (133 loc) · 5.85 KB
/
Copy pathhostRawNotebookProvider.ts
File metadata and controls
151 lines (133 loc) · 5.85 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import '../../../common/extensions';
import * as vscode from 'vscode';
import { CancellationToken } from 'vscode-jsonrpc';
import * as vsls from 'vsls/vscode';
import type { nbformat } from '@jupyterlab/coreutils';
import { IApplicationShell, ILiveShareApi, IWorkspaceService } from '../../../common/application/types';
import { traceError, traceInfo } from '../../../common/logger';
import { IFileSystem } from '../../../common/platform/types';
import { IAsyncDisposableRegistry, IConfigurationService, IDisposableRegistry, Resource } from '../../../common/types';
import { createDeferred } from '../../../common/utils/async';
import { IServiceContainer } from '../../../ioc/types';
import { Identifiers, LiveShare, Settings } from '../../constants';
import { KernelSelector } from '../../jupyter/kernels/kernelSelector';
import { HostJupyterNotebook } from '../../jupyter/liveshare/hostJupyterNotebook';
import { LiveShareParticipantHost } from '../../jupyter/liveshare/liveShareParticipantMixin';
import { IRoleBasedObject } from '../../jupyter/liveshare/roleBasedFactory';
import { IKernelLauncher } from '../../kernel-launcher/types';
import {
IJupyterKernelSpec,
INotebook,
INotebookExecutionInfo,
INotebookExecutionLogger,
IRawNotebookProvider
} from '../../types';
import { calculateWorkingDirectory } from '../../utils';
import { RawJupyterSession } from '../rawJupyterSession';
import { RawNotebookProviderBase } from '../rawNotebookProvider';
// tslint:disable-next-line: no-require-imports
// tslint:disable:no-any
export class HostRawNotebookProvider
extends LiveShareParticipantHost(RawNotebookProviderBase, LiveShare.RawNotebookProviderService)
implements IRoleBasedObject, IRawNotebookProvider {
private disposed = false;
constructor(
private liveShare: ILiveShareApi,
private disposableRegistry: IDisposableRegistry,
asyncRegistry: IAsyncDisposableRegistry,
private configService: IConfigurationService,
private workspaceService: IWorkspaceService,
private appShell: IApplicationShell,
private fs: IFileSystem,
private serviceContainer: IServiceContainer,
private kernelLauncher: IKernelLauncher,
private kernelSelector: KernelSelector
) {
super(liveShare, asyncRegistry);
}
public async dispose(): Promise<void> {
if (!this.disposed) {
this.disposed = true;
await super.dispose();
}
}
public async onAttach(_api: vsls.LiveShare | null): Promise<void> {
// Not implemented yet
}
public async onSessionChange(_api: vsls.LiveShare | null): Promise<void> {
// Not implemented yet
}
public async onDetach(_api: vsls.LiveShare | null): Promise<void> {
// Not implemented yet
}
public async waitForServiceName(): Promise<string> {
return 'Not implemented';
}
protected async createNotebookInstance(
resource: Resource,
identity: vscode.Uri,
notebookMetadata?: nbformat.INotebookMetadata,
cancelToken?: CancellationToken
): Promise<INotebook> {
const notebookPromise = createDeferred<INotebook>();
this.setNotebook(identity, notebookPromise.promise);
const rawSession = new RawJupyterSession(this.kernelLauncher, this.serviceContainer, this.kernelSelector);
try {
const launchTimeout = this.configService.getSettings().datascience.jupyterLaunchTimeout;
const launchedKernelSpec = await rawSession.connect(
resource,
launchTimeout,
notebookMetadata?.kernelspec?.name,
cancelToken
);
// Get the execution info for our notebook
const info = await this.getExecutionInfo(launchedKernelSpec);
if (rawSession.isConnected) {
// Create our notebook
const notebook = new HostJupyterNotebook(
this.liveShare,
rawSession,
this.configService,
this.disposableRegistry,
info,
this.serviceContainer.getAll<INotebookExecutionLogger>(INotebookExecutionLogger),
resource,
identity,
this.getDisposedError.bind(this),
this.workspaceService,
this.appShell,
this.fs
);
// Run initial setup
await notebook.initialize(cancelToken);
traceInfo(`Finished connecting ${this.id}`);
notebookPromise.resolve(notebook);
} else {
notebookPromise.reject(this.getDisposedError());
}
} catch (ex) {
// Make sure we shut down our session in case we started a process
rawSession.dispose().catch((error) => {
traceError(`Failed to dispose of raw session on launch error: ${error} `);
});
// If there's an error, then reject the promise that is returned.
// This original promise must be rejected as it is cached (check `setNotebook`).
notebookPromise.reject(ex);
}
return notebookPromise.promise;
}
// Get the notebook execution info for this raw session instance
private async getExecutionInfo(kernelSpec?: IJupyterKernelSpec): Promise<INotebookExecutionInfo> {
return {
connectionInfo: this.getConnection(),
uri: Settings.JupyterServerLocalLaunch,
interpreter: undefined,
kernelSpec: kernelSpec,
workingDir: await calculateWorkingDirectory(this.configService, this.workspaceService, this.fs),
purpose: Identifiers.RawPurpose
};
}
}