@@ -147,27 +147,19 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/src/exte
147147===================================================================
148148--- /dev/null
149149+++ sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/src/extension.ts
150- @@ -0,0 +1,112 @@
150+ @@ -0,0 +1,58 @@
151151+ import * as vscode from "vscode";
152152+ import * as fs from "fs";
153153+ import * as path from "path";
154154+
155- + let idleFilePath: string
156- + let terminalActivityInterval: NodeJS.Timeout | undefined
157- + const LOG_PREFIX = "[sagemaker-idle-extension]"
158- + const CHECK_INTERVAL = 60000; // 60 seconds interval
155+ + let idleFilePath: string;
159156+
160157+ export function activate(context: vscode.ExtensionContext) {
161158+ initializeIdleFilePath();
162159+ registerEventListeners(context);
163- + startMonitoringTerminalActivity();
164160+ }
165161+
166- + export function deactivate() {
167- + if(terminalActivityInterval) {
168- + clearInterval(terminalActivityInterval)
169- + }
170- + }
162+ + export function deactivate() {}
171163+
172164+ /**
173165+ * Initializes the file path where the idle timestamp will be stored.
@@ -178,7 +170,7 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/src/exte
178170+ idleFilePath = path.join(tmpDirectory, ".sagemaker-last-active-timestamp");
179171+
180172+ // Set initial lastActivetimestamp
181- + updateLastActivityTimestamp()
173+ + updateLastActivityTimestamp();
182174+ }
183175+
184176+ /**
@@ -207,52 +199,6 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/src/exte
207199+ }
208200+
209201+ /**
210- + * Starts monitoring terminal activity by setting an interval to check for activity in the /dev/pts directory.
211- + */
212- + const startMonitoringTerminalActivity = () => {
213- + terminalActivityInterval = setInterval(checkTerminalActivity, CHECK_INTERVAL);
214- + };
215- +
216- +
217- + /**
218- + * Checks for terminal activity by reading the /dev/pts directory and comparing modification times of the files.
219- + *
220- + * The /dev/pts directory is used in Unix-like operating systems to represent pseudo-terminal (PTY) devices.
221- + * Each active terminal session is assigned a PTY device. These devices are represented as files within the /dev/pts directory.
222- + * When a terminal session has activity, such as when a user inputs commands or output is written to the terminal,
223- + * the modification time (mtime) of the corresponding PTY device file is updated. By monitoring the modification
224- + * times of the files in the /dev/pts directory, we can detect terminal activity.
225- + *
226- + * If activity is detected (i.e., if any PTY device file was modified within the CHECK_INTERVAL), this function
227- + * updates the last activity timestamp.
228- + */
229- + const checkTerminalActivity = () => {
230- + fs.readdir("/dev/pts", (err, files) => {
231- + if (err) {
232- + console.error(`${LOG_PREFIX} Error reading /dev/pts directory:`, err);
233- + return;
234- + }
235- +
236- + const now = Date.now();
237- + const activityDetected = files.some((file) => {
238- + const filePath = path.join("/dev/pts", file);
239- + try {
240- + const stats = fs.statSync(filePath);
241- + const mtime = new Date(stats.mtime).getTime();
242- + return now - mtime < CHECK_INTERVAL;
243- + } catch (error) {
244- + console.error(`${LOG_PREFIX} Error reading file stats:`, error);
245- + return false;
246- + }
247- + });
248- +
249- + if (activityDetected) {
250- + updateLastActivityTimestamp();
251- + }
252- + });
253- + };
254- +
255- + /**
256202+ * Updates the last activity timestamp by recording the current timestamp in the idle file and
257203+ * refreshing the status bar. The timestamp should be in ISO 8601 format and set to the UTC timezone.
258204+ */
@@ -289,33 +235,77 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
289235===================================================================
290236--- sagemaker-code-editor.orig/vscode/src/vs/server/node/webClientServer.ts
291237+++ sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
292- @@ -3,7 +3,8 @@
238+ @@ -3,8 +3,10 @@
293239 * Licensed under the MIT License. See License.txt in the project root for license information.
294240 *--------------------------------------------------------------------------------------------*/
295241
296242- import { createReadStream } from 'fs';
297243+ import { createReadStream, existsSync, writeFileSync } from 'fs';
298244+ import {readFile } from 'fs/promises';
299245 import { Promises } from 'vs/base/node/pfs';
246+ + import * as fs from 'fs';
300247 import * as path from 'path';
301248 import * as http from 'http';
302- @@ -100,6 +101,7 @@ export class WebClientServer {
249+ import * as url from 'url';
250+ @@ -91,8 +93,41 @@ export async function serveFile(filePath
251+ }
252+ }
253+
254+ + const CHECK_INTERVAL = 60000; // 60 seconds interval
255+ const APP_ROOT = dirname(FileAccess.asFileUri('').fsPath);
256+
257+ + /**
258+ + * Checks for terminal activity by reading the /dev/pts directory and comparing modification times of the files.
259+ + *
260+ + * The /dev/pts directory is used in Unix-like operating systems to represent pseudo-terminal (PTY) devices.
261+ + * Each active terminal session is assigned a PTY device. These devices are represented as files within the /dev/pts directory.
262+ + * When a terminal session has activity, such as when a user inputs commands or output is written to the terminal,
263+ + * the modification time (mtime) of the corresponding PTY device file is updated. By monitoring the modification
264+ + * times of the files in the /dev/pts directory, we can detect terminal activity.
265+ + *
266+ + * If activity is detected (i.e., if any PTY device file was modified within the CHECK_INTERVAL), this function
267+ + * updates the last activity timestamp.
268+ + */
269+ + function checkTerminalActivity(idleFilePath: string) {
270+ + try {
271+ + const files: string[] = fs.readdirSync('/dev/pts');
272+ + const now = new Date();
273+ +
274+ + const activityDetected = files.some((file: string) => {
275+ + const filePath = path.join('/dev/pts', file);
276+ + const stats = fs.statSync(filePath);
277+ + const mtime = new Date(stats.mtime).getTime();
278+ + return now.getTime() - mtime < CHECK_INTERVAL;
279+ + });
280+ +
281+ + if (activityDetected) {
282+ + fs.writeFileSync(idleFilePath, now.toISOString());
283+ + }
284+ + } catch (err) {
285+ + console.error('Error checking terminal activity:', err);
286+ + }
287+ + }
288+ +
289+ export class WebClientServer {
290+
291+ private readonly _webExtensionResourceUrlTemplate: URI | undefined;
292+ @@ -100,6 +135,7 @@ export class WebClientServer {
303293 private readonly _staticRoute: string;
304294 private readonly _callbackRoute: string;
305295 private readonly _webExtensionRoute: string;
306296+ private readonly _idleRoute: string;
307297
308298 constructor(
309299 private readonly _connectionToken: ServerConnectionToken,
310- @@ -115,6 +117 ,7 @@ export class WebClientServer {
300+ @@ -115,6 +151 ,7 @@ export class WebClientServer {
311301 this._staticRoute = `${serverRootPath}/static`;
312302 this._callbackRoute = `${serverRootPath}/callback`;
313303 this._webExtensionRoute = `${serverRootPath}/web-extension-resource`;
314304+ this._idleRoute = '/api/idle';
315305 }
316306
317307 /**
318- @@ -132,6 +135 ,9 @@ export class WebClientServer {
308+ @@ -132,6 +169 ,9 @@ export class WebClientServer {
319309 if (pathname === this._basePath) {
320310 return this._handleRoot(req, res, parsedUrl);
321311 }
@@ -325,7 +315,7 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
325315 if (pathname === this._callbackRoute) {
326316 // callback support
327317 return this._handleCallback(res);
328- @@ -451,6 +457,31 @@ export class WebClientServer {
318+ @@ -451,6 +491,33 @@ export class WebClientServer {
329319 });
330320 return void res.end(data);
331321 }
@@ -345,6 +335,8 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
345335+ writeFileSync(idleFilePath, timestamp);
346336+ }
347337+
338+ + checkTerminalActivity(idleFilePath);
339+ +
348340+ const data = await readFile(idleFilePath, 'utf8');
349341+
350342+ res.statusCode = 200;
0 commit comments