-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
kernel-lifecycle.js
289 lines (257 loc) Β· 8.09 KB
/
kernel-lifecycle.js
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
/* @flow */
import { Observable, of, empty, merge } from "rxjs";
import { createMessage, childOf, ofMessageType } from "@nteract/messaging";
import type { Channels } from "@nteract/messaging";
import type { ImmutableNotebook } from "@nteract/commutable";
import {
filter,
map,
mergeMap,
concatMap,
catchError,
first,
switchMap,
take,
timeout
} from "rxjs/operators";
import { ActionsObservable, ofType } from "redux-observable";
import type { ContentRef, KernelRef } from "../state/refs";
import { createKernelRef } from "../state/refs";
import * as selectors from "../selectors";
import * as actions from "../actions";
import * as actionTypes from "../actionTypes";
import type { AppState, KernelInfo } from "../state";
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<redux$Action>
) =>
action$.pipe(
ofType(actionTypes.LAUNCH_KERNEL_SUCCESSFUL),
switchMap((action: actionTypes.NewKernelAction) =>
action.payload.kernel.channels.pipe(
filter(msg => msg.header.msg_type === "status"),
map(msg =>
actions.setExecutionState({
kernelStatus: msg.content.execution_state,
kernelRef: action.payload.kernelRef,
contentRef: action.payload.contentRef
})
)
)
)
);
/**
* 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
) {
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
};
return of(
// The original action we were using
actions.setLanguageInfo({
langInfo: msg.content.language_info,
kernelRef,
contentRef
}),
actions.setKernelInfo({
kernelRef,
info
})
);
})
);
return Observable.create(observer => {
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<redux$Action>
) =>
action$.pipe(
ofType(actionTypes.LAUNCH_KERNEL_SUCCESSFUL),
switchMap((action: actionTypes.NewKernelAction) => {
const {
payload: {
kernel: { channels },
kernelRef,
contentRef
}
} = action;
return acquireKernelInfo(channels, kernelRef, contentRef);
})
);
export const extractNewKernel = (
filepath: ?string,
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<redux$Action>,
state$: any
) =>
action$.pipe(
ofType(actionTypes.FETCH_CONTENT_FULFILLED),
mergeMap((action: actionTypes.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
})
);
})
);
export const restartKernelEpic = (
action$: ActionsObservable<redux$Action>,
state$: any,
kernelRefGenerator: () => KernelRef = createKernelRef
) =>
action$.pipe(
ofType(actionTypes.RESTART_KERNEL),
concatMap((action: actionTypes.RestartKernel) => {
const state = state$.value;
const oldKernelRef = action.payload.kernelRef;
const oldKernel = selectors.kernel(state, { kernelRef: oldKernelRef });
const notificationSystem = selectors.notificationSystem(state);
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(actionTypes.LAUNCH_KERNEL_SUCCESSFUL),
filter(
(action: actionTypes.NewKernelAction) =>
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.payload.outputHandling === "Run All") {
return of(
restartSuccess,
actions.executeAllCells({ contentRef: initiatingContentRef })
);
} else {
return of(restartSuccess);
}
}),
catchError(error => {
return of(
actions.restartKernelFailed({
error: error,
kernelRef: newKernelRef,
contentRef: initiatingContentRef
})
);
})
);
return merge(of(kill, relaunch), awaitKernelReady);
})
);