-
Notifications
You must be signed in to change notification settings - Fork 286
/
sessions.ts
196 lines (176 loc) · 4.84 KB
/
sessions.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
import { Session } from '@jupyterlab/services';
import { PathExt } from '@jupyterlab/coreutils';
import { IKernels } from '@jupyterlite/kernel';
import { ArrayExt } from '@lumino/algorithm';
import { UUID } from '@lumino/coreutils';
import { ISessions } from './tokens';
/**
* A class to handle requests to /api/sessions
*/
export class Sessions implements ISessions {
/**
* Construct a new Sessions.
*
* @param options The instantiation options for a Sessions.
*/
constructor(options: Sessions.IOptions) {
this._kernels = options.kernels;
}
/**
* Get a session by id.
*
* @param id The id of the session.
*/
async get(id: string): Promise<Session.IModel> {
const session = this._sessions.find((s) => s.id === id);
if (!session) {
throw Error(`Session ${id} not found`);
}
return session;
}
/**
* List the running sessions
*/
async list(): Promise<Session.IModel[]> {
return this._sessions;
}
/**
* Path an existing session.
* This can be used to rename a session.
*
* - path updates session to track renamed paths
* - kernel.name starts a new kernel with a given kernelspec
*
* @param options The options to patch the session.
*/
async patch(options: Session.IModel): Promise<Session.IModel> {
const { id, path, name, kernel } = options;
const index = this._sessions.findIndex((s) => s.id === id);
const session = this._sessions[index];
if (!session) {
throw Error(`Session ${id} not found`);
}
const patched = {
...session,
path: path ?? session.path,
name: name ?? session.name,
};
if (kernel) {
// Kernel id takes precedence over name.
if (kernel.id) {
const session = this._sessions.find(
(session) => session.kernel?.id === kernel?.id,
);
if (session) {
patched.kernel = session.kernel;
}
} else if (kernel.name) {
const newKernel = await this._kernels.startNew({
id: UUID.uuid4(),
name: kernel.name,
location: PathExt.dirname(patched.path),
});
if (newKernel) {
patched.kernel = newKernel;
}
// clean up the session on kernel shutdown
void this._handleKernelShutdown({
kernelId: newKernel.id,
sessionId: session.id,
});
}
}
this._sessions[index] = patched;
return patched;
}
/**
* Start a new session
* TODO: read path and name
*
* @param options The options to start a new session.
*/
async startNew(options: Session.IModel): Promise<Session.IModel> {
const { path, name } = options;
const running = this._sessions.find((s) => s.name === name);
if (running) {
return running;
}
const kernelName = options.kernel?.name ?? '';
const id = options.id ?? UUID.uuid4();
const nameOrPath = options.name ?? options.path;
const dirname = PathExt.dirname(options.name) || PathExt.dirname(options.path);
const hasDrive = nameOrPath.includes(':');
const driveName = hasDrive ? nameOrPath.split(':')[0] : '';
// add drive name if missing (top level directory)
const location = dirname.includes(driveName) ? dirname : `${driveName}:${dirname}`;
const kernel = await this._kernels.startNew({
id,
name: kernelName,
location,
});
const session: Session.IModel = {
id,
path,
name: name ?? path,
type: 'notebook',
kernel: {
id: kernel.id,
name: kernel.name,
},
};
this._sessions.push(session);
// clean up the session on kernel shutdown
void this._handleKernelShutdown({ kernelId: id, sessionId: session.id });
return session;
}
/**
* Shut down a session.
*
* @param id The id of the session to shut down.
*/
async shutdown(id: string): Promise<void> {
const session = this._sessions.find((s) => s.id === id);
if (!session) {
throw Error(`Session ${id} not found`);
}
const kernelId = session.kernel?.id;
if (kernelId) {
await this._kernels.shutdown(kernelId);
}
ArrayExt.removeFirstOf(this._sessions, session);
}
/**
* Handle kernel shutdown
*/
private async _handleKernelShutdown({
kernelId,
sessionId,
}: {
kernelId: string;
sessionId: string;
}): Promise<void> {
const runningKernel = await this._kernels.get(kernelId);
if (runningKernel) {
runningKernel.disposed.connect(() => {
this.shutdown(sessionId);
});
}
}
private _kernels: IKernels;
// TODO: offload to a database
private _sessions: Session.IModel[] = [];
}
/**
* A namespace for sessions statics.
*/
export namespace Sessions {
/**
* The instantiation options for the sessions.
*/
export interface IOptions {
/**
* A reference to the kernels service.
*/
kernels: IKernels;
}
}