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:
parent
28a5c814f8
commit
1632acb14c
@ -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,
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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(() => {
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user