fix(monitor): fix RAM and Swap rendering, optimize Node process memory to 64M with auto-trim, and document disk/swap usage

This commit is contained in:
GeronimoGc 2026-11-20 11:53:14 -05:00
parent 28a5c814f8
commit 1632acb14c
4 changed files with 45 additions and 16 deletions

View File

@ -6,6 +6,7 @@ module.exports = {
autorestart: true, autorestart: true,
watch: false, watch: false,
max_memory_restart: '120M', max_memory_restart: '120M',
node_args: '--max-old-space-size=64 --optimize-for-size --expose-gc',
env_production: { env_production: {
NODE_ENV: 'production', NODE_ENV: 'production',
PORT: 3005, PORT: 3005,

View File

@ -2294,16 +2294,22 @@ function updateMonitorUI(stats) {
if (stats.memory) { if (stats.memory) {
const mem = stats.memory; const mem = stats.memory;
if (ramUsedEl) ramUsedEl.textContent = `${mem.usedFormatted || '0 B'} / ${mem.totalFormatted || '1 GB'}`; const usedText = mem.hostUsedFormatted || mem.usedFormatted || '0 B';
if (ramPercentEl) ramPercentEl.textContent = `${mem.usedPercent || 0}%`; const totalText = mem.hostTotalFormatted || mem.totalFormatted || '1 GB';
const percent = mem.hostUsedPercent ?? mem.usedPercent ?? 0;
const freeText = mem.hostAvailableFormatted || mem.hostFreeFormatted || mem.freeFormatted || '0 B';
const cacheText = mem.buffersCacheFormatted || mem.cachedFormatted || '—';
if (ramUsedEl) ramUsedEl.textContent = `${usedText} / ${totalText}`;
if (ramPercentEl) ramPercentEl.textContent = `${percent}%`;
if (ramBarEl) { if (ramBarEl) {
ramBarEl.style.width = `${Math.min(100, mem.usedPercent || 0)}%`; ramBarEl.style.width = `${Math.min(100, Math.max(0, percent))}%`;
ramBarEl.className = 'metric-progress-fill fill-lime'; ramBarEl.className = 'metric-progress-fill fill-lime';
if (mem.usedPercent > 85) ramBarEl.classList.add('fill-danger'); if (percent > 85) ramBarEl.classList.add('fill-danger');
else if (mem.usedPercent > 65) ramBarEl.classList.add('fill-warning'); else if (percent > 65) ramBarEl.classList.add('fill-warning');
} }
if (ramFreeEl) ramFreeEl.textContent = mem.freeFormatted || '0 B'; if (ramFreeEl) ramFreeEl.textContent = freeText;
if (ramCacheEl) ramCacheEl.textContent = mem.buffersCacheFormatted || mem.cachedFormatted || '—'; if (ramCacheEl) ramCacheEl.textContent = cacheText;
} }
// 3. Swap // 3. Swap
@ -2313,14 +2319,21 @@ function updateMonitorUI(stats) {
const swapFreeEl = document.getElementById('monitor-swap-free'); const swapFreeEl = document.getElementById('monitor-swap-free');
const swapTotalEl = document.getElementById('monitor-swap-total'); const swapTotalEl = document.getElementById('monitor-swap-total');
if (stats.memory) { const swapObj = stats.swap || stats.memory?.swap || {};
const mem = stats.memory; const swapUsedText = swapObj.usedFormatted || stats.memory?.swapUsedFormatted || '0 B';
if (swapUsedEl) swapUsedEl.textContent = `${mem.swapUsedFormatted || '0 B'} / ${mem.swapTotalFormatted || '0 B'}`; const swapTotalText = swapObj.totalFormatted || stats.memory?.swapTotalFormatted || '0 B';
if (swapPercentEl) swapPercentEl.textContent = `${mem.swapUsedPercent || 0}%`; const swapPercent = swapObj.usedPercent ?? stats.memory?.swapUsedPercent ?? 0;
if (swapBarEl) swapBarEl.style.width = `${Math.min(100, mem.swapUsedPercent || 0)}%`; const swapFreeText = swapObj.freeFormatted || stats.memory?.swapFreeFormatted || '—';
if (swapFreeEl) swapFreeEl.textContent = mem.swapFreeFormatted || '—';
if (swapTotalEl) swapTotalEl.textContent = mem.swapTotalFormatted || '—'; if (swapUsedEl) swapUsedEl.textContent = `${swapUsedText} / ${swapTotalText}`;
if (swapPercentEl) swapPercentEl.textContent = `${swapPercent}%`;
if (swapBarEl) {
swapBarEl.style.width = `${Math.min(100, Math.max(0, swapPercent))}%`;
swapBarEl.className = 'metric-progress-fill fill-purple';
if (swapPercent > 80) swapBarEl.classList.add('fill-danger');
} }
if (swapFreeEl) swapFreeEl.textContent = swapFreeText;
if (swapTotalEl) swapTotalEl.textContent = swapTotalText;
// 4. Disco / // 4. Disco /
const diskUsedEl = document.getElementById('monitor-disk-used-text'); const diskUsedEl = document.getElementById('monitor-disk-used-text');

View File

@ -23,7 +23,14 @@ async function bootstrap() {
console.log(`⚡ Dominio base: ${config.baseDomain}`); console.log(`⚡ Dominio base: ${config.baseDomain}`);
}); });
// 4. Apagado elegante (Graceful Shutdown) // 4. Optimizador proactivo de memoria para mantener RSS mínimo (< 50MB)
if (typeof global.gc === 'function') {
setInterval(() => {
try { global.gc(); } catch {}
}, 45000).unref();
}
// 5. Apagado elegante (Graceful Shutdown)
const shutdown = (signal) => { const shutdown = (signal) => {
console.log(`\n🛑 Señal ${signal} recibida. Cerrando servidor ordenadamente...`); console.log(`\n🛑 Señal ${signal} recibida. Cerrando servidor ordenadamente...`);
server.close(() => { server.close(() => {

View File

@ -166,22 +166,30 @@ export class SystemService {
memory: { memory: {
hostTotalBytes, hostTotalBytes,
hostTotalFormatted: formatBytes(hostTotalBytes), hostTotalFormatted: formatBytes(hostTotalBytes),
totalFormatted: formatBytes(hostTotalBytes),
hostFreeBytes, hostFreeBytes,
hostFreeFormatted: formatBytes(hostFreeBytes), hostFreeFormatted: formatBytes(hostFreeBytes),
freeFormatted: formatBytes(hostAvailableBytes || hostFreeBytes),
hostAvailableBytes, hostAvailableBytes,
hostAvailableFormatted: formatBytes(hostAvailableBytes), hostAvailableFormatted: formatBytes(hostAvailableBytes),
buffersCacheBytes, buffersCacheBytes,
buffersCacheFormatted: formatBytes(buffersCacheBytes), buffersCacheFormatted: formatBytes(buffersCacheBytes),
hostUsedBytes, hostUsedBytes,
hostUsedFormatted: formatBytes(hostUsedBytes), hostUsedFormatted: formatBytes(hostUsedBytes),
usedFormatted: formatBytes(hostUsedBytes),
hostUsedPercent, hostUsedPercent,
usedPercent: hostUsedPercent,
processRssBytes: memUsage.rss, processRssBytes: memUsage.rss,
processRssFormatted: formatBytes(memUsage.rss), processRssFormatted: formatBytes(memUsage.rss),
processHeapUsedBytes: memUsage.heapUsed, processHeapUsedBytes: memUsage.heapUsed,
processHeapUsedFormatted: formatBytes(memUsage.heapUsed), processHeapUsedFormatted: formatBytes(memUsage.heapUsed),
pm2LimitBytes, pm2LimitBytes,
pm2LimitFormatted: '120 MB', pm2LimitFormatted: '120 MB',
rssPercentageOfLimit: parseFloat(((memUsage.rss / pm2LimitBytes) * 100).toFixed(1)) rssPercentageOfLimit: parseFloat(((memUsage.rss / pm2LimitBytes) * 100).toFixed(1)),
swapUsedFormatted: formatBytes(swapUsedBytes),
swapTotalFormatted: formatBytes(swapTotalBytes),
swapFreeFormatted: formatBytes(swapFreeBytes),
swapUsedPercent
}, },
swap: { swap: {
totalBytes: swapTotalBytes, totalBytes: swapTotalBytes,