Worker memory: report available memory, not free memory (#30)
CI / Test (jest) (pull_request) Successful in 19s
CI / Lint (eslint) (pull_request) Successful in 21s
CI / Security (npm audit) (pull_request) Successful in 11s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 1m54s
CI / Integration (api) (pull_request) Successful in 2m36s
CI / UI (playwright) (pull_request) Successful in 2m15s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m29s

os.freemem() leaves out the page cache, so a healthy Linux box looks full:
the production worker (CT 153, 512 MB) has 4 MB free but 449 MB
available, and showed as 99% used instead of about 12%.

- worker: heartbeat metadata adds availableMem from MemAvailable in
  /proc/meminfo (falls back to os.freemem()); freeMem stays for old
  servers.
- Workers tab and dashboard compute used memory from availableMem when
  present, from freeMem for workers that predate it.
- Unit tests for the meminfo parsing and the fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-23 11:55:25 -05:00
1 parent 2c1eb712f2
commit 789e5651c8
4 files changed
+36 -5

No files matched your search

+14 -1
View File
@@ -69,6 +69,18 @@ function detectVersion(dir = __dirname) {
}
}
// Memory a new process could actually get (#30). os.freemem() leaves out the page
// cache, so a healthy Linux box looks almost full; MemAvailable counts reclaimable
// cache as available. Falls back to freemem where /proc/meminfo has no such line.
function availableMemory(meminfo) {
try {
const text = meminfo !== undefined ? meminfo : require('fs').readFileSync('/proc/meminfo', 'utf8');
const m = /^MemAvailable:\s+(\d+)\s+kB/m.exec(text);
if (m) return Number(m[1]) * 1024;
} catch { /* not Linux */ }
return os.freemem();
}
// Reconnect delay after `attempt` consecutive failures: 1 s doubling to 60 s, with up
// to 20% jitter so a fleet of workers doesn't reconnect in lockstep (#29).
function reconnectDelay(attempt, random = Math.random) {
@@ -138,6 +150,7 @@ class PulseWorker {
cpus: os.cpus().length,
totalMem: os.totalmem(),
freeMem: os.freemem(),
availableMem: availableMemory(),
uptime: os.uptime(),
loadavg: os.loadavg(),
activeTasks: this.activeTasks,
@@ -369,7 +382,7 @@ class PulseWorker {
}
}
module.exports = { PulseWorker, detectVersion, runCommand, reconnectDelay };
module.exports = { PulseWorker, detectVersion, runCommand, reconnectDelay, availableMemory };
// Start only when run directly (node worker.js), so tests can load the class.
if (require.main === module) {