@@ -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,83 @@ 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,47 @@ 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+ + const checkTerminalActivity = (idleFilePath: string) => {
270+ + fs.readdir('/dev/pts', (err, files) => {
271+ + if (err) {
272+ + console.error('Error reading /dev/pts directory:', err);
273+ + return;
274+ + }
275+ +
276+ + const now = new Date();
277+ + const activityDetected = files.some((file) => {
278+ + const filePath = path.join('/dev/pts', file);
279+ + try {
280+ + const stats = fs.statSync(filePath);
281+ + const mtime = new Date(stats.mtime).getTime();
282+ + return now.getTime() - mtime < CHECK_INTERVAL;
283+ + } catch (error) {
284+ + console.error('Error reading file stats:', error);
285+ + return false;
286+ + }
287+ + });
288+ +
289+ + if (activityDetected) {
290+ + fs.writeFileSync(idleFilePath, now.toISOString());
291+ + }
292+ + });
293+ + };
294+ +
295+ export class WebClientServer {
296+
297+ private readonly _webExtensionResourceUrlTemplate: URI | undefined;
298+ @@ -100,6 +141,7 @@ export class WebClientServer {
303299 private readonly _staticRoute: string;
304300 private readonly _callbackRoute: string;
305301 private readonly _webExtensionRoute: string;
306302+ private readonly _idleRoute: string;
307303
308304 constructor(
309305 private readonly _connectionToken: ServerConnectionToken,
310- @@ -115,6 +117 ,7 @@ export class WebClientServer {
306+ @@ -115,6 +157 ,7 @@ export class WebClientServer {
311307 this._staticRoute = `${serverRootPath}/static`;
312308 this._callbackRoute = `${serverRootPath}/callback`;
313309 this._webExtensionRoute = `${serverRootPath}/web-extension-resource`;
314310+ this._idleRoute = '/api/idle';
315311 }
316312
317313 /**
318- @@ -132,6 +135 ,9 @@ export class WebClientServer {
314+ @@ -132,6 +175 ,9 @@ export class WebClientServer {
319315 if (pathname === this._basePath) {
320316 return this._handleRoot(req, res, parsedUrl);
321317 }
@@ -325,7 +321,7 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
325321 if (pathname === this._callbackRoute) {
326322 // callback support
327323 return this._handleCallback(res);
328- @@ -451,6 +457,31 @@ export class WebClientServer {
324+ @@ -451,6 +497,33 @@ export class WebClientServer {
329325 });
330326 return void res.end(data);
331327 }
@@ -345,6 +341,8 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
345341+ writeFileSync(idleFilePath, timestamp);
346342+ }
347343+
344+ + checkTerminalActivity(idleFilePath);
345+ +
348346+ const data = await readFile(idleFilePath, 'utf8');
349347+
350348+ res.statusCode = 200;
0 commit comments