|
| 1 | +import { Signal, ISignal } from '@lumino/signaling'; |
| 2 | +import { ServerConnection } from '@jupyterlab/services'; |
| 3 | +import { listRunning, shutdown } from './restapi'; |
| 4 | +import * as ServerProxyApp from './serverproxy'; |
| 5 | + |
| 6 | +/** |
| 7 | + * A server proxy manager. |
| 8 | + */ |
| 9 | +export class ServerProxyManager implements ServerProxyApp.IManager { |
| 10 | + /** |
| 11 | + * Construct a new server proxy manager. |
| 12 | + */ |
| 13 | + constructor(options: ServerProxyManager.IOptions = {}) { |
| 14 | + this.serverSettings = options.serverSettings || ServerConnection.makeSettings(); |
| 15 | + this._refreshTimer = (setInterval as any)(() => { |
| 16 | + if (typeof document !== 'undefined' && document.hidden) { |
| 17 | + return; |
| 18 | + } |
| 19 | + this._refreshRunning(); |
| 20 | + }, 10000); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * The server settings of the manager. |
| 25 | + */ |
| 26 | + readonly serverSettings: ServerConnection.ISettings; |
| 27 | + |
| 28 | + /** |
| 29 | + * A signal emitted when the running server proxies change. |
| 30 | + */ |
| 31 | + get runningChanged(): ISignal<this, ServerProxyApp.IModel[]> { |
| 32 | + return this._runningChanged; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * A signal emitted when there is a connection failure. |
| 37 | + */ |
| 38 | + get connectionFailure(): ISignal<this, Error> { |
| 39 | + return this._connectionFailure; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test whether the delegate has been disposed. |
| 44 | + */ |
| 45 | + get isDisposed(): boolean { |
| 46 | + return this._isDisposed; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Dispose of the resources used by the manager. |
| 51 | + */ |
| 52 | + dispose(): void { |
| 53 | + if (this.isDisposed) { |
| 54 | + return; |
| 55 | + } |
| 56 | + this._isDisposed = true; |
| 57 | + clearInterval(this._refreshTimer); |
| 58 | + Signal.clearData(this); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Create an iterator over the most recent running proxy apps. |
| 63 | + * |
| 64 | + * @returns A new iterator over the running proxy apps. |
| 65 | + */ |
| 66 | + running(): IterableIterator<ServerProxyApp.IModel> { |
| 67 | + return this._models[Symbol.iterator](); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Shut down a server proxy app by name. |
| 72 | + */ |
| 73 | + async shutdown(name: string): Promise<void> { |
| 74 | + await shutdown(name, this.serverSettings); |
| 75 | + await this.refreshRunning(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Shut down all server proxy apps. |
| 80 | + * |
| 81 | + * @returns A promise that resolves when all of the apps are shut down. |
| 82 | + */ |
| 83 | + async shutdownAll(): Promise<void> { |
| 84 | + // Update the list of models to make sure our list is current. |
| 85 | + await this.refreshRunning(); |
| 86 | + |
| 87 | + // Shut down all models. |
| 88 | + await Promise.all( |
| 89 | + this._names.map(name => shutdown(name, this.serverSettings)) |
| 90 | + ); |
| 91 | + |
| 92 | + // Update the list of models to clear out our state. |
| 93 | + await this.refreshRunning(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Force a refresh of the running server proxy apps. |
| 98 | + * |
| 99 | + * @returns A promise that with the list of running proxy apps. |
| 100 | + */ |
| 101 | + async refreshRunning(): Promise<void> { |
| 102 | + return this._refreshRunning(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Refresh the running proxy apps. |
| 107 | + */ |
| 108 | + private async _refreshRunning(): Promise<void> { |
| 109 | + let models: ServerProxyApp.IModel[]; |
| 110 | + try { |
| 111 | + models = await listRunning(this.serverSettings); |
| 112 | + } catch (err: any) { |
| 113 | + // Handle network errors, as well as cases where we are on a |
| 114 | + // JupyterHub and the server is not running. JupyterHub returns a |
| 115 | + // 503 (<2.0) or 424 (>2.0) in that case. |
| 116 | + if ( |
| 117 | + err instanceof ServerConnection.NetworkError || |
| 118 | + err.response?.status === 503 || |
| 119 | + err.response?.status === 424 |
| 120 | + ) { |
| 121 | + this._connectionFailure.emit(err); |
| 122 | + } |
| 123 | + throw err; |
| 124 | + } |
| 125 | + |
| 126 | + if (this.isDisposed) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const names = models.map(({ name }) => name).sort(); |
| 131 | + if (names === this._names) { |
| 132 | + // Identical models list, so just return |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + this._names = names; |
| 137 | + this._models = models; |
| 138 | + this._runningChanged.emit(this._models); |
| 139 | + } |
| 140 | + |
| 141 | + private _names: string[] = []; |
| 142 | + private _models: ServerProxyApp.IModel[] = []; |
| 143 | + |
| 144 | + private _isDisposed = false; |
| 145 | + private _refreshTimer = -1; |
| 146 | + private _runningChanged = new Signal<this, ServerProxyApp.IModel[]>(this); |
| 147 | + private _connectionFailure = new Signal<this, Error>(this); |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * The namespace for `BaseManager` class statics. |
| 152 | + */ |
| 153 | +export namespace ServerProxyManager { |
| 154 | + /** |
| 155 | + * The options used to initialize a SessionManager. |
| 156 | + */ |
| 157 | + export interface IOptions { |
| 158 | + /** |
| 159 | + * The server settings for the manager. |
| 160 | + */ |
| 161 | + serverSettings?: ServerConnection.ISettings; |
| 162 | + } |
| 163 | +} |
0 commit comments