bada-terminal / src /utils /process-helper.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
9.55 kB
/**
* ๊ฒฝ๋กœ: /Users/jungkibong/Projects/langfuse/claude-desktop-cli/src/utils/process-helper.js
* ํŒŒ์ผ: process-helper.js
* ๋ฒ„์ „: v1.1.0
* Rationale:
* - Electron PTY ๋ฐ child_process์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ  ์ข€๋น„ ํ”„๋กœ์„ธ์Šค ๋ˆ„์ถœ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ์„ธ์Šค ํŠธ๋ฆฌ ์ข…๋ฃŒ ๋ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์œ ํ‹ธ๋ฆฌํ‹ฐ.
* - Unix/macOS์—์„œ ์Œ์ˆ˜ PID(-pid)๋ฅผ ์ด์šฉํ•œ SIGKILL ์‹œ ๋ฉ”์ธ ์ผ๋ ‰ํŠธ๋ก  ํ”„๋กœ์„ธ์Šค ๊ทธ๋ฃน ์ „์ฒด๊ฐ€ ์ฆ‰์‚ฌํ•˜๋Š” ๊ฒฐํ•จ์„ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
* - ๋™๊ธฐ์‹ execSync('lsof') ํ˜ธ์ถœ๋กœ ์ธํ•œ UI ํ”„๋ฆฌ์ง•์„ ์ฐจ๋‹จํ•˜๊ธฐ ์œ„ํ•ด ๋น„๋™๊ธฐ getProcessCwdAsync์™€ 1์ดˆ ์บ์‹ฑ TTL ๋ ˆ์ด์–ด๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
*/
const { exec, execSync } = require('child_process');
// CWD ์บ์‹œ ๋งต ๋ฐ TTL ์„ค์ •
const cwdCache = new Map();
const CACHE_TTL_MS = 1000; // 1์ดˆ ์บ์‹œ ๋งŒ๋ฃŒ ์‹œ๊ฐ„
/**
* ํŠน์ • PID๋ฅผ ๊ฐ€์ง„ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ™œ์„ฑํ™” ์ƒํƒœ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
* @param {number} pid - ํ”„๋กœ์„ธ์Šค ID
* @returns {boolean}
*/
function isProcessAlive(pid) {
if (!pid) return false;
const numericPid = parseInt(pid, 10);
if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return false;
try {
process.kill(numericPid, 0);
return true;
} catch (e) {
return false;
}
}
/**
* ์ง€์ •๋œ PID๋ฅผ ๊ฐ€์ง„ ํ”„๋กœ์„ธ์Šค์™€ ๊ทธ ํ•˜์œ„์˜ ๋ชจ๋“  ์ž์‹ ํ”„๋กœ์„ธ์Šค ํŠธ๋ฆฌ๋ฅผ ๊ฐ•์ œ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
* @param {number} pid - ํ”„๋กœ์„ธ์Šค ID
* @param {boolean} isSync - ๋™๊ธฐ ์‹คํ–‰ ์—ฌ๋ถ€
*/
function killProcessTree(pid, isSync = false) {
if (!pid) return;
const numericPid = parseInt(pid, 10);
if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return;
const isWin = process.platform === 'win32';
if (isWin) {
const cmd = `taskkill /F /T /PID ${numericPid}`;
if (isSync) {
try {
execSync(cmd, { stdio: 'ignore', windowsHide: true });
} catch (err) {
// ์ด๋ฏธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ๊ฑฐ๋‚˜ ์ ‘๊ทผ์ด ๊ฑฐ๋ถ€๋œ ๊ฒฝ์šฐ ๋ฌด์‹œ
}
} else {
exec(cmd, { windowsHide: true }, (err) => {
// ๋ฐฑ๊ทธ๋ผ์šด๋“œ ํƒœ์Šคํฌํ‚ฌ ์˜ค๋ฅ˜ ๋ฌด์‹œ
});
}
} else {
// Unix/macOS: ์Œ์ˆ˜ PID(-pid)๋กœ SIGKILL์„ ๋‚ ๋ฆฌ๋ฉด ๋ฉ”์ธ Electron ํ”„๋กœ์„ธ์Šค ๊ทธ๋ฃน์ด ๋™๋ฐ˜ ํญ์‚ฌํ•  ์œ„ํ—˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.
// ๋Œ€์‹  pgrep -P๋ฅผ ํ™œ์šฉํ•ด ์ง์ ‘ ํ•˜์œ„ ์ž์‹ PIDs๋ฅผ ์กฐํšŒํ•˜๊ณ  ๋ฃจํ”„๋ฅผ ๋Œ๋ ค ๊ฐœ๋ณ„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
try {
let childrenRaw = '';
try {
// pgrep -P๋ฅผ ๋™๊ธฐ/๋™์  ์‹คํ–‰ํ•˜์—ฌ ํ•˜์œ„ ์ž์‹ ๋ชฉ๋ก์„ ์ˆ˜์ง‘
childrenRaw = execSync(`pgrep -P ${numericPid}`, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
timeout: 1000
});
} catch (e) {
// ์ž์‹ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์—†๊ฑฐ๋‚˜ pgrep ์‹คํŒจ ์‹œ ๋ฌด์‹œ
}
if (childrenRaw) {
const childPids = childrenRaw.split('\n')
.map(p => parseInt(p.trim(), 10))
.filter(p => !isNaN(p) && p > 0);
for (const childPid of childPids) {
killProcessTree(childPid, isSync); // ์žฌ๊ท€์  ์ž์‹ ์ •๋ฆฌ
}
}
// ์ตœ์ข…์ ์œผ๋กœ ์ž๊ธฐ ์ž์‹  ์•ˆ์ „ํ•˜๊ฒŒ SIGKILL
process.kill(numericPid, 'SIGKILL');
} catch (err) {
// ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ด๋ฏธ ์ข…๋ฃŒ๋œ ๊ฒฝ์šฐ ๋ฌด์‹œ
}
}
}
/**
* ํŠน์ • PID๋ฅผ ๊ฐ€์ง„ ํ”„๋กœ์„ธ์Šค์˜ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ(CWD)๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค (๋™๊ธฐ์‹ - ํ•˜์œ„ ํ˜ธํ™˜์„ฑ ์ „์šฉ).
* @param {number} pid - ํ”„๋กœ์„ธ์Šค ID
* @returns {string|null}
*/
function getProcessCwd(pid) {
if (!pid) return null;
const numericPid = parseInt(pid, 10);
if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return null;
const isWin = process.platform === 'win32';
try {
if (isWin) {
const cmd = `powershell.exe -NoProfile -Command "(Get-CimInstance Win32_Process -Filter \\"ProcessId = ${numericPid}\\").CurrentWorkingDirectory"`;
const output = execSync(cmd, { encoding: 'utf8', timeout: 2000, windowsHide: true }).trim();
return output || null;
} else {
// Linux ์ „์šฉ ์ตœ์ ํ™”: ์™ธ๋ถ€ ํ”„๋กœ์„ธ์Šค ์Šคํฐ ์—†์ด /proc/<pid>/cwd ๋งํฌ ์ง์ ‘ ํ•ด์„
if (process.platform === 'linux') {
const fs = require('fs');
const procPath = `/proc/${numericPid}/cwd`;
if (fs.existsSync(procPath)) {
return fs.readlinkSync(procPath);
}
}
const cmd = `lsof -a -d cwd -p ${numericPid} -Fn`;
const output = execSync(cmd, { encoding: 'utf8', timeout: 2000 });
const lines = output.split('\n');
for (const line of lines) {
if (line.startsWith('n')) {
return line.slice(1).trim();
}
}
}
} catch (e) {
// ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ๋ฌด์‹œ
}
return null;
}
/**
* ํŠน์ • PID ํ”„๋กœ์„ธ์Šค์˜ CWD๋ฅผ ๋น„๋™๊ธฐ์‹์œผ๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค (๋น„๋ธ”๋กœํ‚น - ๊ถŒ์žฅ).
* @param {number} pid - ํ”„๋กœ์„ธ์Šค ID
* @returns {Promise<string|null>}
*/
function getProcessCwdAsync(pid) {
if (!pid) return Promise.resolve(null);
const numericPid = parseInt(pid, 10);
if (isNaN(numericPid) || !Number.isInteger(numericPid) || numericPid <= 0) return Promise.resolve(null);
const now = Date.now();
const cached = cwdCache.get(numericPid);
if (cached && (now - cached.timestamp < CACHE_TTL_MS)) {
return Promise.resolve(cached.path);
}
const isWin = process.platform === 'win32';
return new Promise((resolve) => {
if (isWin) {
const cmd = `powershell.exe -NoProfile -Command "(Get-CimInstance Win32_Process -Filter \\"ProcessId = ${numericPid}\\").CurrentWorkingDirectory"`;
exec(cmd, { timeout: 1500, windowsHide: true }, (err, stdout) => {
if (err || !stdout) {
resolve(null);
} else {
const resolvedPath = stdout.trim();
cwdCache.set(numericPid, { path: resolvedPath, timestamp: Date.now() });
resolve(resolvedPath);
}
});
} else {
// Linux ์ „์šฉ ์ตœ์ ํ™”: /proc/<pid>/cwd ๋งํฌ ๋น„๋™๊ธฐ/๋™๊ธฐ ๊ฒฐํ•ฉ ํ•ด์„
if (process.platform === 'linux') {
const fs = require('fs');
const procPath = `/proc/${pid}/cwd`;
try {
if (fs.existsSync(procPath)) {
const resolvedPath = fs.readlinkSync(procPath);
cwdCache.set(pid, { path: resolvedPath, timestamp: Date.now() });
resolve(resolvedPath);
return;
}
} catch (procErr) {
// proc ์‹คํŒจ ์‹œ lsof ํด๋ฐฑ ๊ฐ€๋™
}
}
const cmd = `lsof -a -d cwd -p ${pid} -Fn`;
exec(cmd, { timeout: 1500 }, (err, stdout) => {
if (err || !stdout) {
resolve(null);
} else {
const lines = stdout.split('\n');
for (const line of lines) {
if (line.startsWith('n')) {
const resolvedPath = line.slice(1).trim();
cwdCache.set(pid, { path: resolvedPath, timestamp: Date.now() });
resolve(resolvedPath);
return;
}
}
resolve(null);
}
});
}
});
}
/**
* ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋‚˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜, ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ๊ธฐ์ค€์—์„œ ํŒŒ์ด์ฌ ๊ฐ€์ƒํ™˜๊ฒฝ(venv)์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ
* ๊ฐ€์ƒํ™˜๊ฒฝ ๋‚ด์˜ Python ์‹คํ–‰ ํŒŒ์ผ ๊ฒฝ๋กœ์™€ ํ•„์ˆ˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜(PATH, VIRTUAL_ENV)๋ฅผ ์„ค์ •ํ•œ env ๋ณต์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
* ๊ฐ€์ƒํ™˜๊ฒฝ์ด ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์œผ๋ฉด ์‹œ์Šคํ…œ ๊ธฐ๋ณธ python3 ๋˜๋Š” python์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
* @param {string} projectDir - ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ (์˜ต์…˜)
* @returns {{pythonPath: string, env: Object}}
*/
function getPythonEnvConfig(projectDir = null) {
const fs = require('fs');
const path = require('path');
const isWin = process.platform === 'win32';
const binDir = isWin ? 'Scripts' : 'bin';
const pythonExe = isWin ? 'python.exe' : 'python3';
let detectedVenv = null;
// 1. process.env.VIRTUAL_ENV ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๊ฒ€์‚ฌ
if (process.env.VIRTUAL_ENV && fs.existsSync(path.join(process.env.VIRTUAL_ENV, binDir, pythonExe))) {
detectedVenv = process.env.VIRTUAL_ENV;
}
// 2. ์ง€์ •๋œ projectDir ํ•˜์œ„ .venv ๋˜๋Š” venv ๋””๋ ‰ํ† ๋ฆฌ ๊ฒ€์‚ฌ
if (!detectedVenv && projectDir) {
const candidates = [path.join(projectDir, '.venv'), path.join(projectDir, 'venv')];
for (const cand of candidates) {
if (fs.existsSync(path.join(cand, binDir, pythonExe))) {
detectedVenv = cand;
break;
}
}
}
// 3. ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ(process.cwd()) ํ•˜์œ„ .venv ๋˜๋Š” venv ๋””๋ ‰ํ† ๋ฆฌ ๊ฒ€์‚ฌ
if (!detectedVenv) {
const candidates = [path.join(process.cwd(), '.venv'), path.join(process.cwd(), 'venv')];
for (const cand of candidates) {
if (fs.existsSync(path.join(cand, binDir, pythonExe))) {
detectedVenv = cand;
break;
}
}
}
const env = Object.assign({}, process.env);
let pythonPath = isWin ? 'python' : 'python3';
// ๊ฐ€์ƒํ™˜๊ฒฝ์ด ๊ฐ์ง€๋œ ๊ฒฝ์šฐ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๊ฒฉ๋ฆฌ ๋ฐ ๋ณด์ • ์ฒ˜๋ฆฌ
if (detectedVenv) {
const venvBin = path.join(detectedVenv, binDir);
pythonPath = path.join(venvBin, pythonExe);
env.VIRTUAL_ENV = detectedVenv;
// PATH ํ™˜๊ฒฝ๋ณ€์ˆ˜์˜ ๊ฐ€์žฅ ๋งจ ์•ž์— ๊ฐ€์ƒํ™˜๊ฒฝ bin ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ฃผ์ž…ํ•˜์—ฌ ์‹คํ–‰ ํŒŒ์ผ ์šฐ์„ ์ˆœ์œ„๋ฅผ ํ™•๋ณด
env.PATH = venvBin + (isWin ? ';' : ':') + (process.env.PATH || '');
}
return { pythonPath, env };
}
module.exports = {
isProcessAlive,
killProcessTree,
getProcessCwd,
getProcessCwdAsync,
getPythonEnvConfig
};