UNPKG

robotics

Version:

Robotics.dev P2P ROS2 robot controller CLI with ROS telemetry and video streaming

118 lines (102 loc) 3.53 kB
#!/usr/bin/env node import { exec } from 'child_process'; import { spawn } from 'child_process'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); function checkCommsProcess() { return new Promise((resolve, reject) => { exec('ps -aux | grep comms.js | grep -v grep', (error, stdout, stderr) => { if (error) { resolve(false); // Process not found } else { resolve(stdout.trim().length > 0); // Process found } }); }); } function getCommsMemoryUsage() { return new Promise((resolve, reject) => { exec('ps -o pid,rss,vsz,comm -p $(pgrep -f comms.js)', (error, stdout, stderr) => { if (error) { reject(error); } else { const lines = stdout.trim().split('\n'); if (lines.length > 1) { const parts = lines[1].trim().split(/\s+/); resolve({ pid: parts[0], rss: parseInt(parts[1]) || 0, // RSS in KB vsz: parseInt(parts[2]) || 0 // VSZ in KB }); } else { resolve(null); } } }); }); } function killCommsProcess() { return new Promise((resolve, reject) => { exec('pkill -f comms.js', (error, stdout, stderr) => { if (error) { reject(error); } else { resolve(); } }); }); } function startCommsProcess() { const filePath = path.join(__dirname, 'comms.js'); const options = [ '--max-old-space-size=1024', '--expose-gc', filePath ]; const commsProcess = spawn('node', options, { detached: true, stdio: 'ignore' }); commsProcess.unref(); return commsProcess; } async function monitorComms() { console.log('Starting comms process monitor...'); setInterval(async () => { try { const isRunning = await checkCommsProcess(); if (!isRunning) { console.log('Comms process not found, restarting...'); startCommsProcess(); return; } const memUsage = await getCommsMemoryUsage(); if (memUsage) { const rssMB = Math.round(memUsage.rss / 1024); const vszMB = Math.round(memUsage.vsz / 1024); console.log(`Comms process (PID: ${memUsage.pid}) - RSS: ${rssMB}MB, VSZ: ${vszMB}MB`); // Restart if memory usage is too high if (rssMB > 800) { // 800MB threshold console.log(`High memory usage detected (${rssMB}MB), restarting comms process...`); await killCommsProcess(); setTimeout(() => { startCommsProcess(); }, 2000); } } } catch (error) { console.error('Monitor error:', error); } }, 30000); // Check every 30 seconds } // Handle graceful shutdown process.on('SIGINT', () => { console.log('Shutting down monitor...'); process.exit(0); }); monitorComms().catch(error => { console.error('Fatal monitor error:', error); process.exit(1); });