Repository navigation
Interaction-based idle culling for VS Code (code-server) #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
93c5140
ac3ff5a
c6a9ced
699b024
64573eb
bcc3a75
cb1ce5b
14aeb27
da331d2
2182330
ceed6bb
488e583
2561031
12d7f68
7540639
f67324b
7258d3b
d0383cb
950077d
ed0d261
f656491
43cae85
5994a32
74a24d8
bac334f
b9d8159
613a1f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| "use strict"; | ||
| // Reports real user interaction to the local Jupyter server. | ||
| // | ||
| // The vscode proxy route runs with update_last_activity=False (see | ||
| // images/nebi/jupyter_server_config.py), so VS Code traffic no longer | ||
| // counts as jupyter activity. Without this extension, actively working | ||
| // VS Code users would be idle-culled — this is the load-bearing half of | ||
| // nebari-dev/data-science-pack#208. | ||
| // | ||
| // Endpoint choice: /api/status and /api/ set _track_activity=False | ||
| // upstream (so pollers don't defeat culling); /api/contents/ is tracked. | ||
| const vscode = require("vscode"); | ||
| const http = require("http"); | ||
|
|
||
| const PING_INTERVAL_MS = 60 * 1000; | ||
|
|
||
| let lastPingMs = 0; | ||
| // Terminals with an in-flight shell execution. Tracked per terminal (not | ||
| // as a counter) because VS Code never fires onDidEndTerminalShellExecution | ||
| // for a terminal disposed mid-command — a counter would stay >0 forever | ||
| // and the busy timer would keep the pod alive (#208 through a different | ||
| // door). The ext host keeps at most one execution per terminal (a nested | ||
| // start ends the previous one first), so a Set is exactly equivalent, and | ||
| // onDidCloseTerminal self-heals the disposal case. | ||
| const busyTerminals = new Set(); | ||
| let output; | ||
|
|
||
| function pingUrl() { | ||
| let base = process.env.JUPYTERHUB_SERVICE_URL; | ||
| if (!base) { | ||
| return null; // not running under JupyterHub — nothing to report to | ||
| } | ||
| try { | ||
| const url = new URL(base); | ||
| if (url.hostname === "0.0.0.0" || url.hostname === "[::]") { | ||
| url.hostname = "127.0.0.1"; | ||
| } | ||
| url.pathname = url.pathname.replace(/\/?$/, "/") + "api/contents/"; | ||
| url.search = "?content=0"; | ||
| return url; | ||
| } catch (e) { | ||
| output.appendLine(`bad JUPYTERHUB_SERVICE_URL: ${e.message}`); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function ping(reason) { | ||
| const url = pingUrl(); | ||
| const token = process.env.JUPYTERHUB_API_TOKEN; | ||
| if (!url || !token) { | ||
| return; | ||
| } | ||
| try { | ||
| const req = http.get( | ||
| url, | ||
| { headers: { Authorization: `token ${token}` } }, | ||
| (res) => { | ||
| res.resume(); // drain — only the request itself matters | ||
| if (res.statusCode < 200 || res.statusCode >= 300) { | ||
| output.appendLine(`activity ping (${reason}): HTTP ${res.statusCode}`); | ||
| } | ||
| }, | ||
| ); | ||
| req.on("error", (e) => { | ||
| // Never throw out of an event handler; next interaction retries. | ||
| output.appendLine(`activity ping (${reason}) failed: ${e.message}`); | ||
| }); | ||
| } catch (e) { | ||
| output.appendLine(`activity ping (${reason}) failed: ${e.message}`); | ||
| } | ||
| } | ||
|
|
||
| function recordActivity(reason) { | ||
| const now = Date.now(); | ||
| if (now - lastPingMs < PING_INTERVAL_MS) { | ||
| return; | ||
| } | ||
| lastPingMs = now; | ||
| ping(reason); | ||
| } | ||
|
|
||
| function activate(context) { | ||
| output = vscode.window.createOutputChannel("Nebari Activity Reporter"); | ||
| output.appendLine("activated"); | ||
|
|
||
| const on = (event, reason) => { | ||
| context.subscriptions.push(event(() => recordActivity(reason))); | ||
| }; | ||
| on(vscode.workspace.onDidChangeTextDocument, "edit"); | ||
| on(vscode.window.onDidChangeTextEditorSelection, "selection"); | ||
| on(vscode.window.onDidChangeTextEditorVisibleRanges, "scroll"); | ||
| on(vscode.window.onDidChangeWindowState, "focus"); | ||
| on(vscode.window.onDidOpenTerminal, "terminal-open"); | ||
| // Not via on(): the close handler needs the terminal argument to clear | ||
| // its busy bit — disposal is the one end-of-execution path that never | ||
| // reaches onDidEndTerminalShellExecution. | ||
| context.subscriptions.push( | ||
| vscode.window.onDidCloseTerminal((t) => { | ||
| busyTerminals.delete(t); | ||
| recordActivity("terminal-close"); | ||
| }), | ||
| ); | ||
|
|
||
| // Busy = active: a running terminal command keeps the pod alive, like | ||
| // cullBusy=false does for kernels. Requires shell integration (auto- | ||
| // injected for bash/zsh). Guarded: API is stable since 1.93 but cheap | ||
| // to feature-detect. | ||
| if (vscode.window.onDidStartTerminalShellExecution) { | ||
| context.subscriptions.push( | ||
| vscode.window.onDidStartTerminalShellExecution((e) => { | ||
| busyTerminals.add(e.terminal); | ||
| recordActivity("exec-start"); | ||
| }), | ||
| ); | ||
| context.subscriptions.push( | ||
| vscode.window.onDidEndTerminalShellExecution((e) => { | ||
| busyTerminals.delete(e.terminal); | ||
| recordActivity("exec-end"); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| const busyTimer = setInterval(() => { | ||
| if (busyTerminals.size > 0) { | ||
| recordActivity("busy"); | ||
| } | ||
| }, PING_INTERVAL_MS); | ||
| context.subscriptions.push({ dispose: () => clearInterval(busyTimer) }); | ||
|
|
||
| // A user just opened/reconnected VS Code — that is activity. | ||
| recordActivity("startup"); | ||
| } | ||
|
|
||
| function deactivate() {} | ||
|
|
||
| module.exports = { activate, deactivate }; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||||||||
| { | ||||||||||||
| "name": "nebari-activity-reporter", | ||||||||||||
| "displayName": "Nebari Activity Reporter", | ||||||||||||
| "description": "Reports real user interaction to the Jupyter server so idle culling works correctly with VS Code (nebari-dev/data-science-pack#208).", | ||||||||||||
| "publisher": "nebari", | ||||||||||||
| "version": "0.1.0", | ||||||||||||
| "license": "BSD-3-Clause", | ||||||||||||
| "engines": { "vscode": "^1.93.0" }, | ||||||||||||
| "categories": ["Other"], | ||||||||||||
| "activationEvents": ["onStartupFinished"], | ||||||||||||
| "main": "./extension.js", | ||||||||||||
| "contributes": {} | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The extension only listens to editor events and makes one local HTTP request, so declaring untrusted support is safe:
Suggested change
|
||||||||||||
| } | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (non-blocking):this fires on every window state change, so losing focus and going inactive also count as activity. In my run a quick alt-tab through the VS Code window produced one ping on focus and a second one two minutes later on the way out, which pushed the cull back by a full timeout.Counting only transitions into focused/active keeps the intent: