Skip to content

Commit

Permalink
Add bandwidthSaveMode to ServiceManager, use it in hub-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
vkaidalov-rft committed Apr 26, 2021
1 parent 850445a commit 3d4cc15
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/docmanager/src/manager.ts
Expand Up @@ -485,7 +485,8 @@ export class DocumentManager implements IDocumentManager {
});
const handler = new SaveHandler({
context,
saveInterval: this.autosaveInterval
saveInterval: this.autosaveInterval,
services: this.services
});
Private.saveHandlerProperty.set(context, handler);
void context.ready.then(() => {
Expand Down
13 changes: 12 additions & 1 deletion packages/docmanager/src/savehandler.ts
Expand Up @@ -5,6 +5,8 @@ import { IDisposable } from '@lumino/disposable';

import { Signal } from '@lumino/signaling';

import { ServiceManager } from '@jupyterlab/services';

import { DocumentRegistry } from '@jupyterlab/docregistry';

/**
Expand All @@ -19,6 +21,7 @@ export class SaveHandler implements IDisposable {
*/
constructor(options: SaveHandler.IOptions) {
this._context = options.context;
this._services = options.services;
const interval = options.saveInterval || 120;
this._minInterval = interval * 1000;
this._interval = this._minInterval;
Expand Down Expand Up @@ -91,7 +94,9 @@ export class SaveHandler implements IDisposable {
return;
}
this._autosaveTimer = window.setTimeout(() => {
this._save();
if (!this._services.bandwidthSaveMode) {
this._save();
}
}, this._interval);
}

Expand Down Expand Up @@ -146,6 +151,7 @@ export class SaveHandler implements IDisposable {
private _minInterval = -1;
private _interval = -1;
private _context: DocumentRegistry.Context;
private _services: ServiceManager.IManager;
private _isActive = false;
private _inDialog = false;
private _isDisposed = false;
Expand All @@ -169,5 +175,10 @@ export namespace SaveHandler {
* The minimum save interval in seconds (default is two minutes).
*/
saveInterval?: number;

/**
* A service manager instance.
*/
services: ServiceManager.IManager;
}
}
2 changes: 1 addition & 1 deletion packages/docmanager/test/savehandler.spec.ts
Expand Up @@ -38,7 +38,7 @@ describe('docregistry/savehandler', () => {
factory,
path: UUID.uuid4() + '.txt'
});
handler = new SaveHandler({ context });
handler = new SaveHandler({ context, services: manager });
return context.initialize(true);
});

Expand Down
4 changes: 3 additions & 1 deletion packages/filebrowser/src/model.ts
Expand Up @@ -113,7 +113,9 @@ export class FileBrowserModel implements IDisposable {
backoff: true,
max: 300 * 1000
},
standby: 'when-hidden'
standby: () => {
return services.bandwidthSaveMode || 'when-hidden';
}
});
}

Expand Down
2 changes: 2 additions & 0 deletions packages/hub-extension/src/index.ts
Expand Up @@ -153,6 +153,7 @@ const connectionlost: JupyterFrontEndPlugin<IConnectionLost> = {
return;
}
showingError = true;
manager.bandwidthSaveMode = true;
const result = await showDialog({
title: trans.__('Server unavailable or unreachable'),
body: trans.__(
Expand All @@ -164,6 +165,7 @@ const connectionlost: JupyterFrontEndPlugin<IConnectionLost> = {
Dialog.cancelButton({ label: trans.__('Dismiss') })
]
});
manager.bandwidthSaveMode = false;
showingError = false;
if (result.button.accept) {
await app.commands.execute(CommandIDs.restart);
Expand Down
2 changes: 1 addition & 1 deletion packages/services/src/kernel/manager.ts
Expand Up @@ -337,6 +337,6 @@ export namespace KernelManager {
/**
* When the manager stops polling the API. Defaults to `when-hidden`.
*/
standby?: Poll.Standby;
standby?: Poll.Standby | (() => boolean | Poll.Standby);
}
}
2 changes: 1 addition & 1 deletion packages/services/src/kernelspec/manager.ts
Expand Up @@ -147,6 +147,6 @@ export namespace KernelSpecManager {
/**
* When the manager stops polling the API. Defaults to `when-hidden`.
*/
standby?: Poll.Standby;
standby?: Poll.Standby | (() => boolean | Poll.Standby);
}
}
41 changes: 38 additions & 3 deletions packages/services/src/manager.ts
Expand Up @@ -37,7 +37,12 @@ export class ServiceManager implements ServiceManager.IManager {
const defaultDrive = options.defaultDrive;
const serverSettings =
options.serverSettings ?? ServerConnection.makeSettings();
const standby = options.standby ?? 'when-hidden';

const standby =
options.standby ??
(() => {
return this.bandwidthSaveMode || 'when-hidden';
});
const normalized = { defaultDrive, serverSettings, standby };

const kernelManager = new KernelManager(normalized);
Expand Down Expand Up @@ -158,6 +163,23 @@ export class ServiceManager implements ServiceManager.IManager {
return this._readyPromise;
}

/**
* Every periodic network polling should be paused while
* this is set to true. Extensions should use this value
* to decide whether to proceed with the polling.
* The extensions may also set this value to true if there is no need
* to fetch anything from the server backend basing on some conditions
* (e.g. when an error message dialog is displayed).
* At the same time, the extensions are responsible for setting
* this value back to false.
*/
get bandwidthSaveMode(): boolean {
return this._bandwidthSaveMode;
}
set bandwidthSaveMode(value: boolean) {
this._bandwidthSaveMode = value;
}

private _onConnectionFailure(sender: any, err: Error): void {
this._connectionFailure.emit(err);
}
Expand All @@ -166,6 +188,7 @@ export class ServiceManager implements ServiceManager.IManager {
private _readyPromise: Promise<void>;
private _connectionFailure = new Signal<this, Error>(this);
private _isReady = false;
private _bandwidthSaveMode = false;
}

/**
Expand Down Expand Up @@ -235,6 +258,18 @@ export namespace ServiceManager {
* A signal emitted when there is a connection failure with the server.
*/
readonly connectionFailure: ISignal<IManager, Error>;

/**
* Every periodic network polling should be paused while
* this is set to true. Extensions should use this value
* to decide whether to proceed with the polling.
* The extensions may also set this value to true if there is no need
* to fetch anything from the server backend basing on some conditions
* (e.g. when an error message dialog is displayed).
* At the same time, the extensions are responsible for setting
* this value back to false.
*/
bandwidthSaveMode: boolean;
}

/**
Expand All @@ -252,8 +287,8 @@ export namespace ServiceManager {
readonly defaultDrive?: Contents.IDrive;

/**
* When the manager stops polling the API. Defaults to `when-hidden`.
* When the manager stops polling the API.
*/
standby?: Poll.Standby;
standby?: Poll.Standby | (() => boolean | Poll.Standby);
}
}
2 changes: 1 addition & 1 deletion packages/services/src/session/manager.ts
Expand Up @@ -355,7 +355,7 @@ export namespace SessionManager {
/**
* When the manager stops polling the API. Defaults to `when-hidden`.
*/
standby?: Poll.Standby;
standby?: Poll.Standby | (() => boolean | Poll.Standby);

/**
* Kernel Manager
Expand Down
2 changes: 1 addition & 1 deletion packages/services/src/terminal/manager.ts
Expand Up @@ -289,6 +289,6 @@ export namespace TerminalManager {
/**
* When the manager stops polling the API. Defaults to `when-hidden`.
*/
standby?: Poll.Standby;
standby?: Poll.Standby | (() => boolean | Poll.Standby);
}
}

0 comments on commit 3d4cc15

Please sign in to comment.