diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs index 1442c53..38fba29 100644 --- a/ecosystem.config.cjs +++ b/ecosystem.config.cjs @@ -6,6 +6,7 @@ module.exports = { autorestart: true, watch: false, max_memory_restart: '120M', + node_args: '--max-old-space-size=64 --optimize-for-size --expose-gc', env_production: { NODE_ENV: 'production', PORT: 3005, diff --git a/public/js/app.js b/public/js/app.js index b0fe14b..3b53e63 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -2294,16 +2294,22 @@ function updateMonitorUI(stats) { if (stats.memory) { const mem = stats.memory; - if (ramUsedEl) ramUsedEl.textContent = `${mem.usedFormatted || '0 B'} / ${mem.totalFormatted || '1 GB'}`; - if (ramPercentEl) ramPercentEl.textContent = `${mem.usedPercent || 0}%`; + const usedText = mem.hostUsedFormatted || mem.usedFormatted || '0 B'; + 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) { - 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'; - if (mem.usedPercent > 85) ramBarEl.classList.add('fill-danger'); - else if (mem.usedPercent > 65) ramBarEl.classList.add('fill-warning'); + if (percent > 85) ramBarEl.classList.add('fill-danger'); + else if (percent > 65) ramBarEl.classList.add('fill-warning'); } - if (ramFreeEl) ramFreeEl.textContent = mem.freeFormatted || '0 B'; - if (ramCacheEl) ramCacheEl.textContent = mem.buffersCacheFormatted || mem.cachedFormatted || '—'; + if (ramFreeEl) ramFreeEl.textContent = freeText; + if (ramCacheEl) ramCacheEl.textContent = cacheText; } // 3. Swap @@ -2313,14 +2319,21 @@ function updateMonitorUI(stats) { const swapFreeEl = document.getElementById('monitor-swap-free'); const swapTotalEl = document.getElementById('monitor-swap-total'); - if (stats.memory) { - const mem = stats.memory; - if (swapUsedEl) swapUsedEl.textContent = `${mem.swapUsedFormatted || '0 B'} / ${mem.swapTotalFormatted || '0 B'}`; - if (swapPercentEl) swapPercentEl.textContent = `${mem.swapUsedPercent || 0}%`; - if (swapBarEl) swapBarEl.style.width = `${Math.min(100, mem.swapUsedPercent || 0)}%`; - if (swapFreeEl) swapFreeEl.textContent = mem.swapFreeFormatted || '—'; - if (swapTotalEl) swapTotalEl.textContent = mem.swapTotalFormatted || '—'; + const swapObj = stats.swap || stats.memory?.swap || {}; + const swapUsedText = swapObj.usedFormatted || stats.memory?.swapUsedFormatted || '0 B'; + const swapTotalText = swapObj.totalFormatted || stats.memory?.swapTotalFormatted || '0 B'; + const swapPercent = swapObj.usedPercent ?? stats.memory?.swapUsedPercent ?? 0; + const swapFreeText = swapObj.freeFormatted || stats.memory?.swapFreeFormatted || '—'; + + 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 / const diskUsedEl = document.getElementById('monitor-disk-used-text'); diff --git a/src/index.js b/src/index.js index 9d79ae8..575f73c 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,14 @@ async function bootstrap() { 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) => { console.log(`\n🛑 Señal ${signal} recibida. Cerrando servidor ordenadamente...`); server.close(() => { diff --git a/src/services/system.service.js b/src/services/system.service.js index 47bcfa0..0d94351 100644 --- a/src/services/system.service.js +++ b/src/services/system.service.js @@ -166,22 +166,30 @@ export class SystemService { memory: { hostTotalBytes, hostTotalFormatted: formatBytes(hostTotalBytes), + totalFormatted: formatBytes(hostTotalBytes), hostFreeBytes, hostFreeFormatted: formatBytes(hostFreeBytes), + freeFormatted: formatBytes(hostAvailableBytes || hostFreeBytes), hostAvailableBytes, hostAvailableFormatted: formatBytes(hostAvailableBytes), buffersCacheBytes, buffersCacheFormatted: formatBytes(buffersCacheBytes), hostUsedBytes, hostUsedFormatted: formatBytes(hostUsedBytes), + usedFormatted: formatBytes(hostUsedBytes), hostUsedPercent, + usedPercent: hostUsedPercent, processRssBytes: memUsage.rss, processRssFormatted: formatBytes(memUsage.rss), processHeapUsedBytes: memUsage.heapUsed, processHeapUsedFormatted: formatBytes(memUsage.heapUsed), pm2LimitBytes, 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: { totalBytes: swapTotalBytes,