Repository navigation
Expand file tree
/
Copy pathrawSession.ts
More file actions
162 lines (149 loc) · 5.98 KB
/
Copy pathrawSession.ts
File metadata and controls
162 lines (149 loc) · 5.98 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
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import type { Kernel, KernelMessage, ServerConnection, Session } from '@jupyterlab/services';
import type { ISignal, Signal } from '@phosphor/signaling';
import * as uuid from 'uuid/v4';
import '../../common/extensions';
import { traceError } from '../../common/logger';
import { IDisposable } from '../../common/types';
import { IKernelProcess } from '../kernel-launcher/types';
import { ISessionWithSocket, KernelSocketInformation } from '../types';
import { createRawKernel, RawKernel } from './rawKernel';
/*
RawSession class implements a jupyterlab ISession object
This provides enough of the ISession interface so that our direct
ZMQ Kernel connection can pretend to be a jupyterlab Session
*/
export class RawSession implements ISessionWithSocket {
public isDisposed: boolean = false;
private isDisposing?: boolean;
// Note, ID is the ID of this session
// ClientID is the ID that we pass in messages to the kernel
// and is also the clientID of the active kernel
private _id: string;
private _clientID: string;
private _kernel: RawKernel;
private readonly _statusChanged: Signal<this, Kernel.Status>;
private readonly _kernelChanged: Signal<this, Session.IKernelChangedArgs>;
private readonly _ioPubMessage: Signal<this, KernelMessage.IIOPubMessage>;
private readonly exitHandler: IDisposable;
// RawSession owns the lifetime of the kernel process and will dispose it
constructor(public kernelProcess: IKernelProcess) {
// tslint:disable-next-line: no-require-imports
const signaling = require('@phosphor/signaling') as typeof import('@phosphor/signaling');
this._statusChanged = new signaling.Signal<this, Kernel.Status>(this);
this._kernelChanged = new signaling.Signal<this, Session.IKernelChangedArgs>(this);
this._ioPubMessage = new signaling.Signal<this, KernelMessage.IIOPubMessage>(this);
// Unique ID for this session instance
this._id = uuid();
// ID for our client JMP connection
this._clientID = uuid();
// Connect our kernel and hook up status changes
this._kernel = createRawKernel(kernelProcess, this._clientID);
this._kernel.statusChanged.connect(this.onKernelStatus, this);
this._kernel.iopubMessage.connect(this.onIOPubMessage, this);
this.exitHandler = kernelProcess.exited(this.handleUnhandledExitingOfKernelProcess, this);
}
public async dispose() {
this.isDisposing = true;
if (!this.isDisposed) {
this.exitHandler.dispose();
await this._kernel.shutdown();
this._kernel.dispose();
this.kernelProcess.dispose().ignoreErrors();
}
this.isDisposed = true;
}
// Return the ID, this is session's ID, not clientID for messages
get id(): string {
return this._id;
}
// Return the current kernel for this session
get kernel(): Kernel.IKernelConnection {
return this._kernel;
}
get kernelSocketInformation(): KernelSocketInformation | undefined {
return {
socket: this._kernel.socket,
options: {
id: this._kernel.id,
clientId: this._clientID,
userName: '',
model: this._kernel.model
}
};
}
// Provide status changes for the attached kernel
get statusChanged(): ISignal<this, Kernel.Status> {
return this._statusChanged;
}
// Shutdown our session and kernel
public shutdown(): Promise<void> {
return this.dispose();
}
// Not Implemented ISession
get terminated(): ISignal<this, void> {
throw new Error('Not yet implemented');
}
get kernelChanged(): ISignal<this, Session.IKernelChangedArgs> {
return this._kernelChanged;
}
get propertyChanged(): ISignal<this, 'path' | 'name' | 'type'> {
throw new Error('Not yet implemented');
}
get iopubMessage(): ISignal<this, KernelMessage.IIOPubMessage> {
return this._ioPubMessage;
}
get unhandledMessage(): ISignal<this, KernelMessage.IMessage> {
throw new Error('Not yet implemented');
}
get anyMessage(): ISignal<this, Kernel.IAnyMessageArgs> {
throw new Error('Not yet implemented');
}
get path(): string {
throw new Error('Not yet implemented');
}
get name(): string {
throw new Error('Not yet implemented');
}
get type(): string {
throw new Error('Not yet implemented');
}
get serverSettings(): ServerConnection.ISettings {
throw new Error('Not yet implemented');
}
get model(): Session.IModel {
throw new Error('Not yet implemented');
}
get status(): Kernel.Status {
throw new Error('Not yet implemented');
}
public setPath(_path: string): Promise<void> {
throw new Error('Not yet implemented');
}
public setName(_name: string): Promise<void> {
throw new Error('Not yet implemented');
}
public setType(_type: string): Promise<void> {
throw new Error('Not yet implemented');
}
public changeKernel(_options: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection> {
throw new Error('Not yet implemented');
}
// Private
// Send out a message when our kernel changes state
private onKernelStatus(_sender: Kernel.IKernelConnection, state: Kernel.Status) {
this._statusChanged.emit(state);
}
private onIOPubMessage(_sender: Kernel.IKernelConnection, msg: KernelMessage.IIOPubMessage) {
this._ioPubMessage.emit(msg);
}
private handleUnhandledExitingOfKernelProcess(e: { exitCode?: number | undefined; reason?: string | undefined }) {
if (this.isDisposing) {
return;
}
traceError(`Disposing session as kernel process died ExitCode: ${e.exitCode}, Reason: ${e.reason}`);
// Just kill the session.
this.dispose().ignoreErrors();
}
}