session.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { IIterator } from '@lumino/algorithm';
  4. import { IDisposable, IObservableDisposable } from '@lumino/disposable';
  5. import { ISignal } from '@lumino/signaling';
  6. import { Kernel, KernelMessage } from '../kernel';
  7. import { ServerConnection } from '..';
  8. import { IChangedArgs } from '@jupyterlab/coreutils';
  9. /**
  10. * Interface of a session object.
  11. *
  12. * A session object represents a live connection to a session kernel.
  13. *
  14. * This represents a persistent kernel connection with a particular key,
  15. * that persists across changing kernels and kernels getting terminated. As
  16. * such, a number of signals are proxied from the current kernel for
  17. * convenience.
  18. *
  19. * The kernel is owned by the session, in that the session creates the
  20. * kernel and manages its lifecycle.
  21. */
  22. export interface ISessionConnection extends IObservableDisposable {
  23. /**
  24. * A signal emitted when a session property changes.
  25. */
  26. readonly propertyChanged: ISignal<this, 'path' | 'name' | 'type'>;
  27. /**
  28. * A signal emitted when the kernel changes.
  29. */
  30. kernelChanged: ISignal<
  31. this,
  32. IChangedArgs<
  33. Kernel.IKernelConnection | null,
  34. Kernel.IKernelConnection | null,
  35. 'kernel'
  36. >
  37. >;
  38. /**
  39. * The kernel statusChanged signal, proxied from the current kernel.
  40. */
  41. statusChanged: ISignal<this, Kernel.Status>;
  42. /**
  43. * The kernel connectionStatusChanged signal, proxied from the current
  44. * kernel.
  45. */
  46. connectionStatusChanged: ISignal<this, Kernel.ConnectionStatus>;
  47. /**
  48. * The kernel pendingInput signal, proxied from the current
  49. * kernel.
  50. */
  51. pendingInput: ISignal<this, boolean>;
  52. /**
  53. * The kernel iopubMessage signal, proxied from the current kernel.
  54. */
  55. iopubMessage: ISignal<this, KernelMessage.IIOPubMessage>;
  56. /**
  57. * The kernel unhandledMessage signal, proxied from the current kernel.
  58. */
  59. unhandledMessage: ISignal<this, KernelMessage.IMessage>;
  60. /**
  61. * The kernel anyMessage signal, proxied from the current kernel.
  62. */
  63. anyMessage: ISignal<this, Kernel.IAnyMessageArgs>;
  64. /**
  65. * Unique id of the session.
  66. */
  67. readonly id: string;
  68. /**
  69. * The current path associated with the session.
  70. */
  71. readonly path: string;
  72. /**
  73. * The current name associated with the session.
  74. */
  75. readonly name: string;
  76. /**
  77. * The type of the session.
  78. */
  79. readonly type: string;
  80. /**
  81. * The server settings of the session.
  82. */
  83. readonly serverSettings: ServerConnection.ISettings;
  84. /**
  85. * The model associated with the session.
  86. */
  87. readonly model: IModel;
  88. /**
  89. * The kernel.
  90. *
  91. * #### Notes
  92. * This is a read-only property, and can be altered by [changeKernel].
  93. *
  94. * A number of kernel signals are proxied through the session from
  95. * whatever the current kernel is for convenience.
  96. */
  97. readonly kernel: Kernel.IKernelConnection | null;
  98. /**
  99. * Change the session path.
  100. *
  101. * @param path - The new session path.
  102. *
  103. * @returns A promise that resolves when the session has renamed.
  104. *
  105. * #### Notes
  106. * This uses the Jupyter REST API, and the response is validated.
  107. * The promise is fulfilled on a valid response and rejected otherwise.
  108. */
  109. setPath(path: string): Promise<void>;
  110. /**
  111. * Change the session name.
  112. *
  113. * @returns A promise that resolves when the session has renamed.
  114. *
  115. * #### Notes
  116. * This uses the Jupyter REST API, and the response is validated.
  117. * The promise is fulfilled on a valid response and rejected otherwise.
  118. */
  119. setName(name: string): Promise<void>;
  120. /**
  121. * Change the session type.
  122. *
  123. * @returns A promise that resolves when the session has renamed.
  124. *
  125. * #### Notes
  126. * This uses the Jupyter REST API, and the response is validated.
  127. * The promise is fulfilled on a valid response and rejected otherwise.
  128. */
  129. setType(type: string): Promise<void>;
  130. /**
  131. * Change the kernel.
  132. *
  133. * @param options - The name or id of the new kernel.
  134. *
  135. * @returns A promise that resolves with the new kernel model.
  136. *
  137. * #### Notes
  138. * This shuts down the existing kernel and creates a new kernel, keeping
  139. * the existing session ID and path. The session assumes it owns the
  140. * kernel.
  141. *
  142. * To start now kernel, pass an empty dictionary.
  143. */
  144. changeKernel(
  145. options: Partial<Kernel.IModel>
  146. ): Promise<Kernel.IKernelConnection | null>;
  147. /**
  148. * Kill the kernel and shutdown the session.
  149. *
  150. * @returns A promise that resolves when the session is shut down.
  151. *
  152. * #### Notes
  153. * This uses the Jupyter REST API, and the response is validated.
  154. * The promise is fulfilled on a valid response and rejected otherwise.
  155. */
  156. shutdown(): Promise<void>;
  157. }
  158. export namespace ISessionConnection {
  159. /**
  160. * The session initialization options.
  161. */
  162. export interface IOptions {
  163. /**
  164. * Session model.
  165. */
  166. model: IModel;
  167. /**
  168. * Connects to an existing kernel
  169. */
  170. connectToKernel(
  171. options: Kernel.IKernelConnection.IOptions
  172. ): Kernel.IKernelConnection;
  173. /**
  174. * The server settings.
  175. */
  176. serverSettings?: ServerConnection.ISettings;
  177. /**
  178. * The username of the session client.
  179. */
  180. username?: string;
  181. /**
  182. * The unique identifier for the session client.
  183. */
  184. clientId?: string;
  185. /**
  186. * Kernel connection options
  187. */
  188. kernelConnectionOptions?: Omit<
  189. Kernel.IKernelConnection.IOptions,
  190. 'model' | 'username' | 'clientId' | 'serverSettings'
  191. >;
  192. }
  193. /**
  194. * An arguments object for the kernel changed signal.
  195. */
  196. export type IKernelChangedArgs = IChangedArgs<
  197. Kernel.IKernelConnection | null,
  198. Kernel.IKernelConnection | null,
  199. 'kernel'
  200. >;
  201. }
  202. /**
  203. * Object which manages session instances.
  204. *
  205. * #### Notes
  206. * The manager is responsible for maintaining the state of running
  207. * sessions.
  208. */
  209. export interface IManager extends IDisposable {
  210. /**
  211. * A signal emitted when the running sessions change.
  212. */
  213. runningChanged: ISignal<this, IModel[]>;
  214. /**
  215. * A signal emitted when there is a connection failure.
  216. */
  217. connectionFailure: ISignal<IManager, ServerConnection.NetworkError>;
  218. /**
  219. * The server settings for the manager.
  220. */
  221. serverSettings?: ServerConnection.ISettings;
  222. /**
  223. * Test whether the manager is ready.
  224. */
  225. readonly isReady: boolean;
  226. /**
  227. * A promise that is fulfilled when the manager is ready.
  228. */
  229. readonly ready: Promise<void>;
  230. /**
  231. * Create an iterator over the known running sessions.
  232. *
  233. * @returns A new iterator over the running sessions.
  234. */
  235. running(): IIterator<IModel>;
  236. /**
  237. * Start a new session.
  238. *
  239. * @param createOptions - Options for creating the session
  240. *
  241. * @param connectOptions - Options for connecting to the session
  242. *
  243. * @returns A promise that resolves with a session connection instance.
  244. *
  245. * #### Notes
  246. * The `serverSettings` and `connectToKernel` options of the manager will be used.
  247. */
  248. startNew(
  249. createOptions: ISessionOptions,
  250. connectOptions?: Omit<
  251. ISessionConnection.IOptions,
  252. 'model' | 'connectToKernel' | 'serverSettings'
  253. >
  254. ): Promise<ISessionConnection>;
  255. /**
  256. * Find a session by id.
  257. *
  258. * @param id - The id of the target session.
  259. *
  260. * @returns A promise that resolves with the session's model.
  261. */
  262. findById(id: string): Promise<IModel | undefined>;
  263. /**
  264. * Find a session by path.
  265. *
  266. * @param path - The path of the target session.
  267. *
  268. * @returns A promise that resolves with the session's model.
  269. */
  270. findByPath(path: string): Promise<IModel | undefined>;
  271. /**
  272. * Connect to a running session.
  273. *
  274. * @param model - The model of the target session.
  275. *
  276. * @param options - The session options to use.
  277. *
  278. * @returns The new session instance.
  279. */
  280. connectTo(
  281. options: Omit<
  282. ISessionConnection.IOptions,
  283. 'connectToKernel' | 'serverSettings'
  284. >
  285. ): ISessionConnection;
  286. /**
  287. * Shut down a session by id.
  288. *
  289. * @param id - The id of the target kernel.
  290. *
  291. * @returns A promise that resolves when the operation is complete.
  292. */
  293. shutdown(id: string): Promise<void>;
  294. /**
  295. * Shut down all sessions.
  296. *
  297. * @returns A promise that resolves when all of the sessions are shut down.
  298. */
  299. shutdownAll(): Promise<void>;
  300. /**
  301. * Force a refresh of the running sessions.
  302. *
  303. * @returns A promise that resolves when the models are refreshed.
  304. *
  305. * #### Notes
  306. * This is intended to be called only in response to a user action,
  307. * since the manager maintains its internal state.
  308. */
  309. refreshRunning(): Promise<void>;
  310. /**
  311. * Find a session associated with a path and stop it is the only session
  312. * using that kernel.
  313. *
  314. * @param path - The path in question.
  315. *
  316. * @returns A promise that resolves when the relevant sessions are stopped.
  317. */
  318. stopIfNeeded(path: string): Promise<void>;
  319. }
  320. /**
  321. * The session model returned by the server.
  322. *
  323. * #### Notes
  324. * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions).
  325. */
  326. export interface IModel {
  327. /**
  328. * The unique identifier for the session client.
  329. */
  330. readonly id: string;
  331. readonly name: string;
  332. readonly path: string;
  333. readonly type: string;
  334. readonly kernel: Kernel.IModel | null;
  335. }
  336. /**
  337. * A session request.
  338. *
  339. * #### Notes
  340. * The `path` and `type` session model parameters are required. The `name`
  341. * parameter is not technically required, but is often assumed to be nonempty,
  342. * so we require it too.
  343. *
  344. * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions).
  345. */
  346. export type ISessionOptions = Pick<IModel, 'path' | 'type' | 'name'> & {
  347. kernel?: Partial<Pick<Kernel.IModel, 'name'>>;
  348. };