-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
Copy pathkernel-lifecycle.ts
356 lines (320 loc) Β· 10.1 KB
/
kernel-lifecycle.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { ImmutableNotebook } from "@nteract/commutable";
import {
Channels,
childOf,
createMessage,
JupyterMessage,
ofMessageType
} from "@nteract/messaging";
import { AnyAction } from "redux";
import { ActionsObservable, ofType, StateObservable } from "redux-observable";
import { empty, merge, Observable, Observer, of } from "rxjs";
import {
catchError,
concatMap,
filter,
first,
map,
mergeMap,
switchMap,
take,
takeUntil,
timeout
} from "rxjs/operators";
import * as actions from "@nteract/actions";
import * as selectors from "@nteract/selectors";
import { AppState, ContentRef, KernelInfo, KernelRef } from "@nteract/types";
import { createKernelRef } from "@nteract/types";
const path = require("path");
/**
* Sets the execution state after a kernel has been launched.
*
* @oaram {ActionObservable} action$ ActionObservable for LAUNCH_KERNEL_SUCCESSFUL action
*/
export const watchExecutionStateEpic = (
action$: ActionsObservable<
actions.NewKernelAction | actions.KillKernelSuccessful
>
) =>
action$.pipe(
ofType(actions.LAUNCH_KERNEL_SUCCESSFUL),
switchMap(
(action: actions.NewKernelAction | actions.KillKernelSuccessful) =>
(action as actions.NewKernelAction).payload.kernel.channels.pipe(
filter((msg: JupyterMessage) => msg.header.msg_type === "status"),
map((msg: JupyterMessage) =>
actions.setExecutionState({
kernelStatus: msg.content.execution_state,
kernelRef: (action as actions.NewKernelAction).payload.kernelRef
})
),
takeUntil(
action$.pipe(
ofType(actions.KILL_KERNEL_SUCCESSFUL),
filter(
(
killAction:
| actions.KillKernelSuccessful
| actions.NewKernelAction
) => killAction.payload.kernelRef === action.payload.kernelRef
)
)
)
)
)
);
/**
* Send a kernel_info_request to the kernel.
*
* @param {Object} channels A object containing the kernel channels
* @returns {Observable} The reply from the server
*/
export function acquireKernelInfo(
channels: Channels,
kernelRef: KernelRef,
contentRef: ContentRef,
state: AppState
) {
const message = createMessage("kernel_info_request");
const obs = channels.pipe(
childOf(message),
ofMessageType("kernel_info_reply"),
first(),
mergeMap(msg => {
const c = msg.content;
const l = c.language_info;
const info: KernelInfo = {
protocolVersion: c.protocol_version,
implementation: c.implementation,
implementationVersion: c.implementation_version,
banner: c.banner,
helpLinks: c.help_links,
languageName: l.name,
languageVersion: l.version,
mimetype: l.mimetype,
fileExtension: l.file_extension,
pygmentsLexer: l.pygments_lexer,
codemirrorMode: l.codemirror_mode,
nbconvertExporter: l.nbconvert_exporter
};
let result: AnyAction[];
if (!c.protocol_version.startsWith("5")) {
result = [
actions.launchKernelFailed({
kernelRef,
contentRef,
error: new Error(
"The kernel that you are attempting to launch does not support the latest version (v5) of the messaging protocol."
)
})
];
} else {
result = [
// The original action we were using
actions.setLanguageInfo({
langInfo: msg.content.language_info,
kernelRef,
contentRef
}),
actions.setKernelInfo({
kernelRef,
info
})
];
const kernelspec = selectors.kernelspecByName(state, { name: l.name });
if (kernelspec) {
result.push(actions.setKernelMetadata({
contentRef,
kernelInfo: kernelspec
}));
}
}
return of(...result);
})
);
return Observable.create((observer: Observer<any>) => {
const subscription = obs.subscribe(observer);
channels.next(message);
return subscription;
});
}
/**
* Gets information about newly launched kernel.
*
* @param {ActionObservable} The action type
*/
export const acquireKernelInfoEpic = (
action$: ActionsObservable<actions.NewKernelAction>,
state$: StateObservable<AppState>
) =>
action$.pipe(
ofType(actions.LAUNCH_KERNEL_SUCCESSFUL),
switchMap((action: actions.NewKernelAction) => {
const {
payload: {
kernel: { channels },
kernelRef,
contentRef
}
} = action;
return acquireKernelInfo(channels, kernelRef, contentRef, state$.value);
})
);
export const extractNewKernel = (
filepath: string | null,
notebook: ImmutableNotebook
) => {
const cwd = (filepath && path.dirname(filepath)) || "/";
const kernelSpecName =
notebook.getIn(["metadata", "kernelspec", "name"]) ||
notebook.getIn(["metadata", "language_info", "name"]) ||
"python3";
return {
cwd,
kernelSpecName
};
};
/**
* NOTE: This function is _exactly_ the same as the desktop loading.js version
* with one strong exception -- extractNewKernel
* Can they be combined without incurring a penalty on the web app?
* The native functions used are `path.dirname`, `path.resolve`, and `process.cwd()`
* We could always inject those dependencies separately...
*/
export const launchKernelWhenNotebookSetEpic = (
action$: ActionsObservable<actions.FetchContentFulfilled>,
state$: any
) =>
action$.pipe(
ofType(actions.FETCH_CONTENT_FULFILLED),
mergeMap((action: actions.FetchContentFulfilled) => {
const state: AppState = state$.value;
const contentRef = action.payload.contentRef;
const content = selectors.content(state, { contentRef });
if (
!content ||
content.type !== "notebook" ||
content.model.type !== "notebook"
) {
// This epic only handles notebook content
return empty();
}
const filepath = content.filepath;
const notebook = content.model.notebook;
const { cwd, kernelSpecName } = extractNewKernel(filepath, notebook);
return of(
actions.launchKernelByName({
kernelSpecName,
cwd,
kernelRef: action.payload.kernelRef,
selectNextKernel: true,
contentRef: action.payload.contentRef
})
);
})
);
/**
* Restarts a Jupyter kernel in the local scenario, where a restart requires
* killing the existing kernel process and starting an ew one.
*/
export const restartKernelEpic = (
action$: ActionsObservable<actions.RestartKernel | actions.NewKernelAction>,
state$: any,
kernelRefGenerator: () => KernelRef = createKernelRef
) =>
action$.pipe(
ofType(actions.RESTART_KERNEL),
concatMap((action: actions.RestartKernel | actions.NewKernelAction) => {
const state = state$.value;
const oldKernelRef = selectors.kernelRefByContentRef(state$.value, {
contentRef: action.payload.contentRef
});
const notificationSystem = selectors.notificationSystem(state);
if (!oldKernelRef) {
notificationSystem.addNotification({
title: "Failure to Restart",
message: "Unable to restart kernel, please select a new kernel.",
dismissible: true,
position: "tr",
level: "error"
});
return empty();
}
const oldKernel = selectors.kernel(state, { kernelRef: oldKernelRef });
if (oldKernel && oldKernel.type === "websocket") {
return empty();
}
if (!oldKernelRef || !oldKernel) {
notificationSystem.addNotification({
title: "Failure to Restart",
message: "Unable to restart kernel, please select a new kernel.",
dismissible: true,
position: "tr",
level: "error"
});
// TODO: Wow do we need to send notifications through our store for
// consistency
return empty();
}
const newKernelRef = kernelRefGenerator();
const initiatingContentRef = action.payload.contentRef;
// TODO: Incorporate this into each of the launchKernelByName
// actions...
// This only mirrors the old behavior of restart kernel (for now)
notificationSystem.addNotification({
title: "Kernel Restarting...",
message: `Kernel ${oldKernel.kernelSpecName ||
"unknown"} is restarting.`,
dismissible: true,
position: "tr",
level: "success"
});
const kill = actions.killKernel({
restarting: true,
kernelRef: oldKernelRef
});
const relaunch = actions.launchKernelByName({
kernelSpecName: oldKernel.kernelSpecName,
cwd: oldKernel.cwd,
kernelRef: newKernelRef,
selectNextKernel: true,
contentRef: initiatingContentRef
});
const awaitKernelReady = action$.pipe(
ofType(actions.LAUNCH_KERNEL_SUCCESSFUL),
filter(
(action: actions.NewKernelAction | actions.RestartKernel) =>
action.payload.kernelRef === newKernelRef
),
take(1),
timeout(60000), // If kernel doesn't come up within this interval we will abort follow-on actions.
concatMap(() => {
const restartSuccess = actions.restartKernelSuccessful({
kernelRef: newKernelRef,
contentRef: initiatingContentRef
});
if (
(action as actions.RestartKernel).payload.outputHandling ===
"Run All"
) {
return of(
restartSuccess,
actions.executeAllCells({ contentRef: initiatingContentRef })
);
} else {
return of(restartSuccess);
}
}),
catchError(error => {
return of(
actions.restartKernelFailed({
error,
kernelRef: newKernelRef,
contentRef: initiatingContentRef
})
);
})
);
return merge(of(kill, relaunch), awaitKernelReady);
})
);