Skip to content

Commit cd2d6fa

Browse files
refactor: Add standby callback to lumino polling
* The callback will monitor the connection status and when connection is lost, it will put the poll in standby mode. * This ensures that on JupyterHub deployments, the terminated servers will not make any API requests to hub which can pollute the logs Signed-off-by: Mahendra Paipuri <[email protected]>
1 parent 97f8708 commit cd2d6fa

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

packages/labextension/src/index.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ILabShell,
33
JupyterFrontEnd,
44
JupyterFrontEndPlugin,
5+
JupyterLab,
56
} from '@jupyterlab/application';
67

78
import { IToolbarWidgetRegistry } from '@jupyterlab/apputils';
@@ -70,14 +71,25 @@ const resourceStatusPlugin: JupyterFrontEndPlugin<void> = {
7071
id: '@jupyter-server/resource-usage:status-item',
7172
autoStart: true,
7273
requires: [ITranslator],
73-
optional: [IStatusBar],
74+
optional: [IStatusBar, JupyterLab.IInfo],
7475
activate: (
7576
app: JupyterFrontEnd,
7677
translator: ITranslator,
77-
statusBar: IStatusBar | null
78+
statusBar: IStatusBar | null,
79+
info: JupyterLab.IInfo
7880
) => {
81+
const refreshRate = DEFAULT_REFRESH_RATE;
82+
7983
const trans = translator.load('jupyter-resource-usage');
80-
const item = new ResourceUsageStatus(trans);
84+
const item = new ResourceUsageStatus(trans, {
85+
refreshRate,
86+
refreshStandby: () => {
87+
if (info) {
88+
return !info.isConnected || 'when-hidden';
89+
}
90+
return 'when-hidden';
91+
},
92+
});
8193

8294
if (statusBar) {
8395
statusBar.registerStatusItem(resourceStatusPlugin.id, {
@@ -98,11 +110,12 @@ const systemMonitorPlugin: JupyterFrontEndPlugin<void> = {
98110
id: '@jupyter-server/resource-usage:topbar-item',
99111
autoStart: true,
100112
requires: [IToolbarWidgetRegistry],
101-
optional: [ISettingRegistry],
113+
optional: [ISettingRegistry, JupyterLab.IInfo],
102114
activate: async (
103115
app: JupyterFrontEnd,
104116
toolbarRegistry: IToolbarWidgetRegistry,
105-
settingRegistry: ISettingRegistry | null
117+
settingRegistry: ISettingRegistry | null,
118+
info: JupyterLab.IInfo | null
106119
) => {
107120
let enablePlugin = DEFAULT_ENABLE_SYSTEM_MONITOR;
108121
let refreshRate = DEFAULT_REFRESH_RATE;
@@ -126,7 +139,15 @@ const systemMonitorPlugin: JupyterFrontEndPlugin<void> = {
126139
diskLabel = diskSettings.label;
127140
}
128141

129-
const model = new ResourceUsage.Model({ refreshRate });
142+
const model = new ResourceUsage.Model({
143+
refreshRate,
144+
refreshStandby: () => {
145+
if (info) {
146+
return !info.isConnected || 'when-hidden';
147+
}
148+
return 'when-hidden';
149+
},
150+
});
130151
await model.refresh();
131152

132153
if (enablePlugin && model.cpuAvailable) {

packages/labextension/src/model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ export namespace ResourceUsage {
6464
frequency: {
6565
interval: options.refreshRate,
6666
backoff: true,
67+
max: 300 * 1000,
6768
},
6869
name: '@jupyterlab/statusbar:ResourceUsage#metrics',
70+
standby: options.refreshStandby || 'when-hidden',
6971
});
7072
this._poll.ticked.connect((poll) => {
7173
const { payload, phase } = poll.state;
@@ -326,6 +328,11 @@ export namespace ResourceUsage {
326328
* The refresh rate (in ms) for querying the server.
327329
*/
328330
refreshRate: number;
331+
332+
/**
333+
* When the model stops polling the API. Defaults to `when-hidden`.
334+
*/
335+
refreshStandby?: Poll.Standby | (() => boolean | Poll.Standby);
329336
}
330337

331338
/**

packages/labextension/src/resourceUsage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export class ResourceUsageStatus extends VDomRenderer<ResourceUsage.Model> {
2020
/**
2121
* Construct a new resource usage status item.
2222
*/
23-
constructor(trans: TranslationBundle) {
24-
super(new ResourceUsage.Model({ refreshRate: 5000 }));
23+
constructor(trans: TranslationBundle, options: ResourceUsage.Model.IOptions) {
24+
super(new ResourceUsage.Model(options));
2525
this._trans = trans;
2626
}
2727

0 commit comments

Comments
 (0)