pm2 kinda sucks tbh

This commit is contained in:
Xargana 2025-04-08 16:45:42 +03:00
parent d293e637f6
commit 0b78444cab

View file

@ -1,6 +1,6 @@
const express = require("express"); const express = require("express");
const ping = require("ping"); const ping = require("ping");
const pm2 = require("pm2"); const { execSync } = require("child_process");
const router = express.Router(); const router = express.Router();
@ -45,26 +45,18 @@ async function checkServers() {
} }
} }
async function checkPM2Services() { function checkPM2Services() {
return new Promise((resolve, reject) => { try {
pm2.connect(function(err) { // Use execSync to ensure we get the output immediately
if (err) { const output = execSync('pm2 jlist', { encoding: 'utf8' });
console.error('Error connecting to PM2:', err);
pm2.disconnect();
resolve();
return;
}
pm2.list((err, list) => { try {
if (err) { const processList = JSON.parse(output);
console.error('Error getting PM2 process list:', err);
pm2.disconnect();
resolve();
return;
}
// Update PM2 services status // Clear previous status
list.forEach(process => { pm2ServicesStatus = {};
processList.forEach(process => {
pm2ServicesStatus[process.name] = { pm2ServicesStatus[process.name] = {
name: process.name, name: process.name,
id: process.pm_id, id: process.pm_id,
@ -72,27 +64,71 @@ async function checkPM2Services() {
cpu: process.monit ? process.monit.cpu : null, cpu: process.monit ? process.monit.cpu : null,
memory: process.monit ? process.monit.memory : null, memory: process.monit ? process.monit.memory : null,
uptime: process.pm2_env.pm_uptime ? uptime: process.pm2_env.pm_uptime ?
Date.now() - process.pm2_env.pm_uptime : Math.floor((Date.now() - process.pm2_env.pm_uptime) / 1000) :
null, null,
restarts: process.pm2_env.restart_time, restarts: process.pm2_env.restart_time,
lastChecked: new Date().toISOString() lastChecked: new Date().toISOString()
}; };
}); });
} catch (parseError) {
pm2.disconnect(); console.error(`Error parsing PM2 output: ${parseError.message}`);
resolve();
});
});
});
} }
async function checkAll() {
try {
await checkServers();
await checkPM2Services();
} catch (error) { } catch (error) {
console.error("Error in checkAll function:", error); console.error(`Error executing pm2 jlist: ${error.message}`);
// Try an alternative approach using pm2 list
try {
const output = execSync('pm2 list --format json', { encoding: 'utf8' });
try {
const processList = JSON.parse(output);
// Clear previous status
pm2ServicesStatus = {};
processList.forEach(process => {
pm2ServicesStatus[process.name] = {
name: process.name,
id: process.pm_id,
status: process.status || 'unknown',
cpu: process.cpu || null,
memory: process.memory || null,
uptime: null, // Not available in this format
restarts: process.restart || 0,
lastChecked: new Date().toISOString()
};
});
} catch (parseError) {
console.error(`Error parsing PM2 list output: ${parseError.message}`);
} }
} catch (fallbackError) {
console.error(`Error with fallback PM2 command: ${fallbackError.message}`);
// Last resort: just check if processes are running using ps
try {
const output = execSync('ps aux | grep pm2', { encoding: 'utf8' });
const lines = output.split('\n').filter(line =>
line.includes('PM2') && !line.includes('grep')
);
pm2ServicesStatus = {
'pm2-daemon': {
name: 'pm2-daemon',
status: lines.length > 0 ? 'online' : 'stopped',
lastChecked: new Date().toISOString()
}
};
} catch (psError) {
console.error(`Error checking processes: ${psError.message}`);
pm2ServicesStatus = { error: 'Unable to check PM2 processes' };
}
}
}
}
function checkAll() {
checkServers();
checkPM2Services();
} }
// Initial check with error handling // Initial check with error handling