session.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IIterator
  5. } from '@phosphor/algorithm';
  6. import {
  7. JSONObject
  8. } from '@phosphor/coreutils';
  9. import {
  10. IDisposable
  11. } from '@phosphor/disposable';
  12. import {
  13. ISignal
  14. } from '@phosphor/signaling';
  15. import {
  16. Kernel, KernelMessage
  17. } from '../kernel';
  18. import {
  19. ServerConnection
  20. } from '..';
  21. import {
  22. DefaultSession
  23. } from './default';
  24. /**
  25. * A namespace for session interfaces and factory functions.
  26. */
  27. export
  28. namespace Session {
  29. /**
  30. * Interface of a session object.
  31. */
  32. export
  33. interface ISession extends IDisposable {
  34. /**
  35. * A signal emitted when the session is shut down.
  36. */
  37. terminated: ISignal<this, void>;
  38. /**
  39. * A signal emitted when the kernel changes.
  40. */
  41. kernelChanged: ISignal<this, Kernel.IKernelConnection>;
  42. /**
  43. * A signal emitted when the session status changes.
  44. */
  45. statusChanged: ISignal<this, Kernel.Status>;
  46. /**
  47. * A signal emitted when a session property changes.
  48. */
  49. readonly propertyChanged: ISignal<this, 'path' | 'name' | 'type'>;
  50. /**
  51. * A signal emitted for iopub kernel messages.
  52. */
  53. iopubMessage: ISignal<this, KernelMessage.IIOPubMessage>;
  54. /**
  55. * A signal emitted for unhandled kernel message.
  56. */
  57. unhandledMessage: ISignal<this, KernelMessage.IMessage>;
  58. /**
  59. * A signal emitted for any kernel message.
  60. *
  61. * Note: The behavior is undefined if the message is modified
  62. * during message handling. As such, it should be treated as read-only.
  63. */
  64. anyMessage: ISignal<this, Kernel.IAnyMessageArgs>;
  65. /**
  66. * Unique id of the session.
  67. */
  68. readonly id: string;
  69. /**
  70. * The current path associated with the sesssion.
  71. */
  72. readonly path: string;
  73. /**
  74. * The current name associated with the sesssion.
  75. */
  76. readonly name: string;
  77. /**
  78. * The type of the session.
  79. */
  80. readonly type: string;
  81. /**
  82. * The server settings of the session.
  83. */
  84. readonly serverSettings: ServerConnection.ISettings;
  85. /**
  86. * The model associated with the session.
  87. */
  88. readonly model: Session.IModel;
  89. /**
  90. * The kernel.
  91. *
  92. * #### Notes
  93. * This is a read-only property, and can be altered by [changeKernel].
  94. */
  95. readonly kernel: Kernel.IKernelConnection;
  96. /**
  97. * The current status of the session.
  98. *
  99. * #### Notes
  100. * This is a delegate to the kernel status.
  101. */
  102. readonly status: Kernel.Status;
  103. /**
  104. * Change the session path.
  105. *
  106. * @param path - The new session path.
  107. *
  108. * @returns A promise that resolves when the session has renamed.
  109. *
  110. * #### Notes
  111. * This uses the Jupyter REST API, and the response is validated.
  112. * The promise is fulfilled on a valid response and rejected otherwise.
  113. */
  114. setPath(path: string): Promise<void>;
  115. /**
  116. * Change the session name.
  117. */
  118. setName(name: string): Promise<void>;
  119. /**
  120. * Change the session type.
  121. */
  122. setType(type: string): Promise<void>;
  123. /**
  124. * Change the kernel.
  125. *
  126. * @param options - The name or id of the new kernel.
  127. *
  128. * @returns A promise that resolves with the new kernel model.
  129. *
  130. * #### Notes
  131. * This shuts down the existing kernel and creates a new kernel,
  132. * keeping the existing session ID and path.
  133. */
  134. changeKernel(options: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection>;
  135. /**
  136. * Kill the kernel and shutdown the session.
  137. *
  138. * @returns A promise that resolves when the session is shut down.
  139. *
  140. * #### Notes
  141. * This uses the Jupyter REST API, and the response is validated.
  142. * The promise is fulfilled on a valid response and rejected otherwise.
  143. */
  144. shutdown(): Promise<void>;
  145. }
  146. /**
  147. * List the running sessions.
  148. *
  149. * @param settings - The server settings to use for the request.
  150. *
  151. * @returns A promise that resolves with the list of session models.
  152. *
  153. * #### Notes
  154. * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions), and validates the response.
  155. *
  156. * All client-side sessions are updated with current information.
  157. *
  158. * The promise is fulfilled on a valid response and rejected otherwise.
  159. */
  160. export
  161. function listRunning(settings?: ServerConnection.ISettings): Promise<Session.IModel[]> {
  162. return DefaultSession.listRunning(settings);
  163. }
  164. /**
  165. * Start a new session.
  166. *
  167. * @param options - The options used to start the session.
  168. *
  169. * @returns A promise that resolves with the session instance.
  170. *
  171. * #### Notes
  172. * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions), and validates the response.
  173. *
  174. * A path must be provided. If a kernel id is given, it will
  175. * connect to an existing kernel. If no kernel id or name is given,
  176. * the server will start the default kernel type.
  177. *
  178. * The promise is fulfilled on a valid response and rejected otherwise.
  179. *
  180. * Wrap the result in an Session object. The promise is fulfilled
  181. * when the session is created on the server, otherwise the promise is
  182. * rejected.
  183. */
  184. export
  185. function startNew(options: Session.IOptions): Promise<ISession> {
  186. return DefaultSession.startNew(options);
  187. }
  188. /**
  189. * Find a session by id.
  190. *
  191. * @param id - The id of the target session.
  192. *
  193. * @param settings - The server settings.
  194. *
  195. * @returns A promise that resolves with the session model.
  196. *
  197. * #### Notes
  198. * If the session was already started via `startNew`, the existing
  199. * Session object's information is used in the fulfillment value.
  200. *
  201. * Otherwise, we attempt to find to the existing session.
  202. * The promise is fulfilled when the session is found,
  203. * otherwise the promise is rejected.
  204. */
  205. export
  206. function findById(id: string, settings?: ServerConnection.ISettings): Promise<Session.IModel> {
  207. return DefaultSession.findById(id, settings);
  208. }
  209. /**
  210. * Find a session by path.
  211. *
  212. * @param path - The path of the target session.
  213. *
  214. * @param settings: The server settings.
  215. *
  216. * @returns A promise that resolves with the session model.
  217. *
  218. * #### Notes
  219. * If the session was already started via `startNewSession`, the existing
  220. * Session object's info is used in the fulfillment value.
  221. *
  222. * Otherwise, we attempt to find to the existing
  223. * session using [listRunningSessions].
  224. * The promise is fulfilled when the session is found,
  225. * otherwise the promise is rejected.
  226. *
  227. * If the session was not already started and no `options` are given,
  228. * the promise is rejected.
  229. */
  230. export
  231. function findByPath(path: string, settings?: ServerConnection.ISettings): Promise<Session.IModel> {
  232. return DefaultSession.findByPath(path, settings);
  233. }
  234. /**
  235. * Connect to a running session.
  236. *
  237. * @param model - The model of the target session.
  238. *
  239. * @param settigns - The server settings.
  240. *
  241. * @returns The session instance.
  242. *
  243. * #### Notes
  244. * If the session was already started via `startNew`, the existing
  245. * Session object is used as the fulfillment value.
  246. *
  247. * Otherwise, we attempt to connect to the existing session.
  248. */
  249. export
  250. function connectTo(model: Session.IModel, settings?: ServerConnection.ISettings): ISession {
  251. return DefaultSession.connectTo(model, settings);
  252. }
  253. /**
  254. * Shut down a session by id.
  255. *
  256. * @param id - The id of the target session.
  257. *
  258. * @param settings - The server settings.
  259. *
  260. * @returns A promise that resolves when the session is shut down.
  261. *
  262. */
  263. export
  264. function shutdown(id: string, settings?: ServerConnection.ISettings): Promise<void> {
  265. return DefaultSession.shutdown(id, settings);
  266. }
  267. /**
  268. * Shut down all sessions.
  269. *
  270. * @returns A promise that resolves when all of the sessions are shut down.
  271. */
  272. export
  273. function shutdownAll(settings?: ServerConnection.ISettings): Promise<void> {
  274. return DefaultSession.shutdownAll(settings);
  275. }
  276. /**
  277. * The session initialization options.
  278. */
  279. export
  280. interface IOptions {
  281. /**
  282. * The path (not including name) to the session.
  283. */
  284. path: string;
  285. /**
  286. * The name of the session.
  287. */
  288. name?: string;
  289. /**
  290. * The type of the session.
  291. */
  292. type?: string;
  293. /**
  294. * The type of kernel (e.g. python3).
  295. */
  296. kernelName?: string;
  297. /**
  298. * The id of an existing kernel.
  299. */
  300. kernelId?: string;
  301. /**
  302. * The server settings.
  303. */
  304. serverSettings?: ServerConnection.ISettings;
  305. /**
  306. * The username of the session client.
  307. */
  308. username?: string;
  309. /**
  310. * The unique identifier for the session client.
  311. */
  312. clientId?: string;
  313. }
  314. /**
  315. * Object which manages session instances.
  316. *
  317. * #### Notes
  318. * The manager is responsible for maintaining the state of running
  319. * sessions and the initial fetch of kernel specs.
  320. */
  321. export
  322. interface IManager extends IDisposable {
  323. /**
  324. * A signal emitted when the kernel specs change.
  325. */
  326. specsChanged: ISignal<this, Kernel.ISpecModels>;
  327. /**
  328. * A signal emitted when the running sessions change.
  329. */
  330. runningChanged: ISignal<this, IModel[]>;
  331. /**
  332. * The server settings for the manager.
  333. */
  334. serverSettings?: ServerConnection.ISettings;
  335. /**
  336. * The cached kernel specs.
  337. *
  338. * #### Notes
  339. * This value will be null until the manager is ready.
  340. */
  341. readonly specs: Kernel.ISpecModels | null;
  342. /**
  343. * Test whether the manager is ready.
  344. */
  345. readonly isReady: boolean;
  346. /**
  347. * A promise that is fulfilled when the manager is ready.
  348. */
  349. readonly ready: Promise<void>;
  350. /**
  351. * Create an iterator over the known running sessions.
  352. *
  353. * @returns A new iterator over the running sessions.
  354. */
  355. running(): IIterator<IModel>;
  356. /**
  357. * Start a new session.
  358. *
  359. * @param options - The session options to use.
  360. *
  361. * @returns A promise that resolves with the session instance.
  362. *
  363. * #### Notes
  364. * The `serverSettings` of the manager will be used.
  365. */
  366. startNew(options: IOptions): Promise<ISession>;
  367. /**
  368. * Find a session by id.
  369. *
  370. * @param id - The id of the target session.
  371. *
  372. * @returns A promise that resolves with the session's model.
  373. */
  374. findById(id: string): Promise<IModel>;
  375. /**
  376. * Find a session by path.
  377. *
  378. * @param path - The path of the target session.
  379. *
  380. * @returns A promise that resolves with the session's model.
  381. */
  382. findByPath(path: string): Promise<IModel>;
  383. /**
  384. * Connect to a running session.
  385. *
  386. * @param model - The model of the target session.
  387. *
  388. * @param options - The session options to use.
  389. *
  390. * @returns The new session instance.
  391. */
  392. connectTo(model: Session.IModel): ISession;
  393. /**
  394. * Shut down a session by id.
  395. *
  396. * @param id - The id of the target kernel.
  397. *
  398. * @returns A promise that resolves when the operation is complete.
  399. */
  400. shutdown(id: string): Promise<void>;
  401. /**
  402. * Shut down all sessions.
  403. *
  404. * @returns A promise that resolves when all of the sessions are shut down.
  405. */
  406. shutdownAll(): Promise<void>;
  407. /**
  408. * Force a refresh of the specs from the server.
  409. *
  410. * @returns A promise that resolves when the specs are fetched.
  411. *
  412. * #### Notes
  413. * This is intended to be called only in response to a user action,
  414. * since the manager maintains its internal state.
  415. */
  416. refreshSpecs(): Promise<void>;
  417. /**
  418. * Force a refresh of the running sessions.
  419. *
  420. * @returns A promise that resolves when the models are refreshed.
  421. *
  422. * #### Notes
  423. * This is intended to be called only in response to a user action,
  424. * since the manager maintains its internal state.
  425. */
  426. refreshRunning(): Promise<void>;
  427. /**
  428. * Find a session associated with a path and stop it is the only session
  429. * using that kernel.
  430. *
  431. * @param path - The path in question.
  432. *
  433. * @returns A promise that resolves when the relevant sessions are stopped.
  434. */
  435. stopIfNeeded(path: string): Promise<void>;
  436. }
  437. /**
  438. * The session model used by the server.
  439. *
  440. * #### Notes
  441. * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions).
  442. */
  443. export
  444. interface IModel extends JSONObject {
  445. /**
  446. * The unique identifier for the session client.
  447. */
  448. readonly id: string;
  449. readonly name: string;
  450. readonly path: string;
  451. readonly type: string;
  452. readonly kernel: Kernel.IModel;
  453. }
  454. }