/
manager.ts
311 lines (278 loc) · 8.07 KB
/
manager.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Poll } from '@jupyterlab/coreutils';
import { ArrayExt, IIterator, iter } from '@phosphor/algorithm';
import { JSONExt } from '@phosphor/coreutils';
import { ISignal, Signal } from '@phosphor/signaling';
import { ServerConnection } from '..';
import { Kernel } from './kernel';
import { BaseManager } from '../basemanager';
// TODO: Migrate kernel connection status etc. up to session
// TODO: move session management work up to session manager rather than session objects
// TODO: Get rid of ClientSession
// TODO: put session persistence in jlab server end (even if not in notebook)
/**
* An implementation of a kernel manager.
*/
export class KernelManager extends BaseManager implements Kernel.IManager {
/**
* Construct a new kernel manager.
*
* @param options - The default options for kernel.
*/
constructor(options: KernelManager.IOptions = {}) {
super(options);
// Initialize internal data.
this._ready = (async () => {
await this.requestRunning();
if (this.isDisposed) {
return;
}
this._isReady = true;
})();
// Start model and specs polling with exponential backoff.
this._pollModels = new Poll({
auto: false,
factory: () => this.requestRunning(),
frequency: {
interval: 10 * 1000,
backoff: true,
max: 300 * 1000
},
name: `@jupyterlab/services:KernelManager#models`,
standby: options.standby || 'when-hidden'
});
void this.ready.then(() => {
void this._pollModels.start();
});
}
/**
* The server settings for the manager.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* Test whether the manager is ready.
*/
get isReady(): boolean {
return this._isReady;
}
/**
* A promise that fulfills when the manager is ready.
*/
get ready(): Promise<void> {
return this._ready;
}
/**
* A signal emitted when the running kernels change.
*/
get runningChanged(): ISignal<this, Kernel.IModel[]> {
return this._runningChanged;
}
/**
* A signal emitted when there is a connection failure.
*/
get connectionFailure(): ISignal<this, Error> {
return this._connectionFailure;
}
/**
* Connect to an existing kernel.
*
* @param model - The model of the target kernel.
*
* @returns A promise that resolves with the new kernel instance.
*/
connectTo(model: Kernel.IModel): Kernel.IKernel {
let kernel = Kernel.connectTo(model, this.serverSettings);
this._onStarted(kernel);
return kernel;
}
/**
* Dispose of the resources used by the manager.
*/
dispose(): void {
this._models.length = 0;
this._pollModels.dispose();
super.dispose();
}
/**
* Find a kernel by id.
*
* @param id - The id of the target kernel.
*
* @returns A promise that resolves with the kernel's model.
*/
findById(id: string): Promise<Kernel.IModel> {
return Kernel.findById(id, this.serverSettings);
}
/**
* Force a refresh of the running kernels.
*
* @returns A promise that resolves when the running list has been refreshed.
*
* #### Notes
* This is not typically meant to be called by the user, since the
* manager maintains its own internal state.
*/
async refreshRunning(): Promise<void> {
await this._pollModels.refresh();
await this._pollModels.tick;
}
/**
* Create an iterator over the most recent running kernels.
*
* @returns A new iterator over the running kernels.
*/
running(): IIterator<Kernel.IModel> {
return iter(this._models);
}
/**
* Shut down a kernel by id.
*
* @param id - The id of the target kernel.
*
* @returns A promise that resolves when the operation is complete.
*
* #### Notes
* This will emit [[runningChanged]] if the running kernels list
* changes.
*/
async shutdown(id: string): Promise<void> {
const models = this._models;
const kernels = this._kernels;
const index = ArrayExt.findFirstIndex(models, value => value.id === id);
if (index === -1) {
return;
}
// Proactively remove the model.
models.splice(index, 1);
this._runningChanged.emit(models.slice());
// Delete and dispose the kernel locally.
kernels.forEach(kernel => {
if (kernel.id === id) {
kernels.delete(kernel);
kernel.dispose();
}
});
// Shut down the remote session.
await Kernel.shutdown(id, this.serverSettings);
}
/**
* Shut down all kernels.
*
* @returns A promise that resolves when all of the kernels are shut down.
*/
async shutdownAll(): Promise<void> {
// Update the list of models then shut down every session.
try {
await this.requestRunning();
await Promise.all(
this._models.map(({ id }) => Kernel.shutdown(id, this.serverSettings))
);
} finally {
// Dispose every kernel and clear the set.
this._kernels.forEach(kernel => {
kernel.dispose();
});
this._kernels.clear();
// Remove all models even if we had an error.
if (this._models.length) {
this._models.length = 0;
this._runningChanged.emit([]);
}
}
}
/**
* Start a new kernel.
*
* @param options - The kernel options to use.
*
* @returns A promise that resolves with the kernel instance.
*
* #### Notes
* The manager `serverSettings` will be always be used.
*/
async startNew(options: Kernel.IOptions = {}): Promise<Kernel.IKernel> {
const newOptions = { ...options, serverSettings: this.serverSettings };
const kernel = await Kernel.startNew(newOptions);
this._onStarted(kernel);
return kernel;
}
/**
* Execute a request to the server to poll running kernels and update state.
*/
protected async requestRunning(): Promise<void> {
const models = await Kernel.listRunning(this.serverSettings).catch(err => {
// Check for a network error, or a 503 error, which is returned
// by a JupyterHub when a server is shut down.
if (
err instanceof ServerConnection.NetworkError ||
(err.response && err.response.status === 503)
) {
this._connectionFailure.emit(err);
return [] as Kernel.IModel[];
}
throw err;
});
if (this.isDisposed) {
return;
}
if (!JSONExt.deepEqual(models, this._models)) {
const ids = models.map(({ id }) => id);
const kernels = this._kernels;
kernels.forEach(kernel => {
if (ids.indexOf(kernel.id) === -1) {
kernel.dispose();
kernels.delete(kernel);
}
});
this._models = models.slice();
this._runningChanged.emit(models);
}
}
/**
* Handle a kernel starting.
*/
private _onStarted(kernel: Kernel.IKernel): void {
let id = kernel.id;
this._kernels.add(kernel);
let index = ArrayExt.findFirstIndex(this._models, value => value.id === id);
if (index === -1) {
this._models.push(kernel.model);
this._runningChanged.emit(this._models.slice());
}
kernel.disposed.connect(() => {
this._onTerminated(id);
});
}
/**
* Handle a kernel terminating.
*/
private _onTerminated(id: string): void {
let index = ArrayExt.findFirstIndex(this._models, value => value.id === id);
if (index !== -1) {
this._models.splice(index, 1);
this._runningChanged.emit(this._models.slice());
}
}
private _isReady = false;
private _kernels = new Set<Kernel.IKernel>();
private _models: Kernel.IModel[] = [];
private _pollModels: Poll;
private _ready: Promise<void>;
private _runningChanged = new Signal<this, Kernel.IModel[]>(this);
private _connectionFailure = new Signal<this, Error>(this);
}
/**
* The namespace for `KernelManager` class statics.
*/
export namespace KernelManager {
/**
* The options used to initialize a KernelManager.
*/
export interface IOptions extends BaseManager.IOptions {
/**
* When the manager stops polling the API. Defaults to `when-hidden`.
*/
standby?: Poll.Standby;
}
}