123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- IIterator
- } from '@phosphor/algorithm';
- import {
- JSONObject
- } from '@phosphor/coreutils';
- import {
- IDisposable
- } from '@phosphor/disposable';
- import {
- ISignal
- } from '@phosphor/signaling';
- import {
- Kernel, KernelMessage
- } from '../kernel';
- import {
- ServerConnection
- } from '..';
- import {
- DefaultSession
- } from './default';
- /**
- * A namespace for session interfaces and factory functions.
- */
- export
- namespace Session {
- /**
- * Interface of a session object.
- */
- export
- interface ISession extends IDisposable {
- /**
- * A signal emitted when the session is shut down.
- */
- terminated: ISignal<this, void>;
- /**
- * A signal emitted when the kernel changes.
- */
- kernelChanged: ISignal<this, Kernel.IKernelConnection>;
- /**
- * A signal emitted when the session status changes.
- */
- statusChanged: ISignal<this, Kernel.Status>;
- /**
- * A signal emitted when a session property changes.
- */
- readonly propertyChanged: ISignal<this, 'path' | 'name' | 'type'>;
- /**
- * A signal emitted for iopub kernel messages.
- */
- iopubMessage: ISignal<this, KernelMessage.IIOPubMessage>;
- /**
- * A signal emitted for unhandled kernel message.
- */
- unhandledMessage: ISignal<this, KernelMessage.IMessage>;
- /**
- * A signal emitted for any kernel message.
- *
- * Note: The behavior is undefined if the message is modified
- * during message handling. As such, it should be treated as read-only.
- */
- anyMessage: ISignal<this, Kernel.IAnyMessageArgs>;
- /**
- * Unique id of the session.
- */
- readonly id: string;
- /**
- * The current path associated with the sesssion.
- */
- readonly path: string;
- /**
- * The current name associated with the sesssion.
- */
- readonly name: string;
- /**
- * The type of the session.
- */
- readonly type: string;
- /**
- * The server settings of the session.
- */
- readonly serverSettings: ServerConnection.ISettings;
- /**
- * The model associated with the session.
- */
- readonly model: Session.IModel;
- /**
- * The kernel.
- *
- * #### Notes
- * This is a read-only property, and can be altered by [changeKernel].
- */
- readonly kernel: Kernel.IKernelConnection;
- /**
- * The current status of the session.
- *
- * #### Notes
- * This is a delegate to the kernel status.
- */
- readonly status: Kernel.Status;
- /**
- * Change the session path.
- *
- * @param path - The new session path.
- *
- * @returns A promise that resolves when the session has renamed.
- *
- * #### Notes
- * This uses the Jupyter REST API, and the response is validated.
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- setPath(path: string): Promise<void>;
- /**
- * Change the session name.
- */
- setName(name: string): Promise<void>;
- /**
- * Change the session type.
- */
- setType(type: string): Promise<void>;
- /**
- * Change the kernel.
- *
- * @param options - The name or id of the new kernel.
- *
- * @returns A promise that resolves with the new kernel model.
- *
- * #### Notes
- * This shuts down the existing kernel and creates a new kernel,
- * keeping the existing session ID and path.
- */
- changeKernel(options: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection>;
- /**
- * Kill the kernel and shutdown the session.
- *
- * @returns A promise that resolves when the session is shut down.
- *
- * #### Notes
- * This uses the Jupyter REST API, and the response is validated.
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- shutdown(): Promise<void>;
- }
- /**
- * List the running sessions.
- *
- * @param settings - The server settings to use for the request.
- *
- * @returns A promise that resolves with the list of session models.
- *
- * #### Notes
- * 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.
- *
- * All client-side sessions are updated with current information.
- *
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- export
- function listRunning(settings?: ServerConnection.ISettings): Promise<Session.IModel[]> {
- return DefaultSession.listRunning(settings);
- }
- /**
- * Start a new session.
- *
- * @param options - The options used to start the session.
- *
- * @returns A promise that resolves with the session instance.
- *
- * #### Notes
- * 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.
- *
- * A path must be provided. If a kernel id is given, it will
- * connect to an existing kernel. If no kernel id or name is given,
- * the server will start the default kernel type.
- *
- * The promise is fulfilled on a valid response and rejected otherwise.
- *
- * Wrap the result in an Session object. The promise is fulfilled
- * when the session is created on the server, otherwise the promise is
- * rejected.
- */
- export
- function startNew(options: Session.IOptions): Promise<ISession> {
- return DefaultSession.startNew(options);
- }
- /**
- * Find a session by id.
- *
- * @param id - The id of the target session.
- *
- * @param settings - The server settings.
- *
- * @returns A promise that resolves with the session model.
- *
- * #### Notes
- * If the session was already started via `startNew`, the existing
- * Session object's information is used in the fulfillment value.
- *
- * Otherwise, we attempt to find to the existing session.
- * The promise is fulfilled when the session is found,
- * otherwise the promise is rejected.
- */
- export
- function findById(id: string, settings?: ServerConnection.ISettings): Promise<Session.IModel> {
- return DefaultSession.findById(id, settings);
- }
- /**
- * Find a session by path.
- *
- * @param path - The path of the target session.
- *
- * @param settings: The server settings.
- *
- * @returns A promise that resolves with the session model.
- *
- * #### Notes
- * If the session was already started via `startNewSession`, the existing
- * Session object's info is used in the fulfillment value.
- *
- * Otherwise, we attempt to find to the existing
- * session using [listRunningSessions].
- * The promise is fulfilled when the session is found,
- * otherwise the promise is rejected.
- *
- * If the session was not already started and no `options` are given,
- * the promise is rejected.
- */
- export
- function findByPath(path: string, settings?: ServerConnection.ISettings): Promise<Session.IModel> {
- return DefaultSession.findByPath(path, settings);
- }
- /**
- * Connect to a running session.
- *
- * @param model - The model of the target session.
- *
- * @param settigns - The server settings.
- *
- * @returns The session instance.
- *
- * #### Notes
- * If the session was already started via `startNew`, the existing
- * Session object is used as the fulfillment value.
- *
- * Otherwise, we attempt to connect to the existing session.
- */
- export
- function connectTo(model: Session.IModel, settings?: ServerConnection.ISettings): ISession {
- return DefaultSession.connectTo(model, settings);
- }
- /**
- * Shut down a session by id.
- *
- * @param id - The id of the target session.
- *
- * @param settings - The server settings.
- *
- * @returns A promise that resolves when the session is shut down.
- *
- */
- export
- function shutdown(id: string, settings?: ServerConnection.ISettings): Promise<void> {
- return DefaultSession.shutdown(id, settings);
- }
- /**
- * Shut down all sessions.
- *
- * @returns A promise that resolves when all of the sessions are shut down.
- */
- export
- function shutdownAll(settings?: ServerConnection.ISettings): Promise<void> {
- return DefaultSession.shutdownAll(settings);
- }
- /**
- * The session initialization options.
- */
- export
- interface IOptions {
- /**
- * The path (not including name) to the session.
- */
- path: string;
- /**
- * The name of the session.
- */
- name?: string;
- /**
- * The type of the session.
- */
- type?: string;
- /**
- * The type of kernel (e.g. python3).
- */
- kernelName?: string;
- /**
- * The id of an existing kernel.
- */
- kernelId?: string;
- /**
- * The server settings.
- */
- serverSettings?: ServerConnection.ISettings;
- /**
- * The username of the session client.
- */
- username?: string;
- /**
- * The unique identifier for the session client.
- */
- clientId?: string;
- }
- /**
- * Object which manages session instances.
- *
- * #### Notes
- * The manager is responsible for maintaining the state of running
- * sessions and the initial fetch of kernel specs.
- */
- export
- interface IManager extends IDisposable {
- /**
- * A signal emitted when the kernel specs change.
- */
- specsChanged: ISignal<this, Kernel.ISpecModels>;
- /**
- * A signal emitted when the running sessions change.
- */
- runningChanged: ISignal<this, IModel[]>;
- /**
- * The server settings for the manager.
- */
- serverSettings?: ServerConnection.ISettings;
- /**
- * The cached kernel specs.
- *
- * #### Notes
- * This value will be null until the manager is ready.
- */
- readonly specs: Kernel.ISpecModels | null;
- /**
- * Test whether the manager is ready.
- */
- readonly isReady: boolean;
- /**
- * A promise that is fulfilled when the manager is ready.
- */
- readonly ready: Promise<void>;
- /**
- * Create an iterator over the known running sessions.
- *
- * @returns A new iterator over the running sessions.
- */
- running(): IIterator<IModel>;
- /**
- * Start a new session.
- *
- * @param options - The session options to use.
- *
- * @returns A promise that resolves with the session instance.
- *
- * #### Notes
- * The `serverSettings` of the manager will be used.
- */
- startNew(options: IOptions): Promise<ISession>;
- /**
- * Find a session by id.
- *
- * @param id - The id of the target session.
- *
- * @returns A promise that resolves with the session's model.
- */
- findById(id: string): Promise<IModel>;
- /**
- * Find a session by path.
- *
- * @param path - The path of the target session.
- *
- * @returns A promise that resolves with the session's model.
- */
- findByPath(path: string): Promise<IModel>;
- /**
- * Connect to a running session.
- *
- * @param model - The model of the target session.
- *
- * @param options - The session options to use.
- *
- * @returns The new session instance.
- */
- connectTo(model: Session.IModel): ISession;
- /**
- * Shut down a session by id.
- *
- * @param id - The id of the target kernel.
- *
- * @returns A promise that resolves when the operation is complete.
- */
- shutdown(id: string): Promise<void>;
- /**
- * Shut down all sessions.
- *
- * @returns A promise that resolves when all of the sessions are shut down.
- */
- shutdownAll(): Promise<void>;
- /**
- * Force a refresh of the specs from the server.
- *
- * @returns A promise that resolves when the specs are fetched.
- *
- * #### Notes
- * This is intended to be called only in response to a user action,
- * since the manager maintains its internal state.
- */
- refreshSpecs(): Promise<void>;
- /**
- * Force a refresh of the running sessions.
- *
- * @returns A promise that resolves when the models are refreshed.
- *
- * #### Notes
- * This is intended to be called only in response to a user action,
- * since the manager maintains its internal state.
- */
- refreshRunning(): Promise<void>;
- /**
- * Find a session associated with a path and stop it is the only session
- * using that kernel.
- *
- * @param path - The path in question.
- *
- * @returns A promise that resolves when the relevant sessions are stopped.
- */
- stopIfNeeded(path: string): Promise<void>;
- }
- /**
- * The session model used by the server.
- *
- * #### Notes
- * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions).
- */
- export
- interface IModel extends JSONObject {
- /**
- * The unique identifier for the session client.
- */
- readonly id: string;
- readonly name: string;
- readonly path: string;
- readonly type: string;
- readonly kernel: Kernel.IModel;
- }
- }
|