feat(health-server): /health checkt /var/cache vrije ruimte
Voeg cacheBytesFree() toe via df -PB1 en retourneer 503 met
{ status: 'unhealthy', reason: 'cache-low' } als < 100 MB vrij.
Bij voldoende ruimte wordt cache_free_bytes toegevoegd aan de
200-response.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a7933f7420
commit
482a5b900e
1 changed files with 17 additions and 0 deletions
|
|
@ -14,6 +14,7 @@
|
|||
const http = require('node:http');
|
||||
const fs = require('node:fs/promises');
|
||||
const path = require('node:path');
|
||||
const { execSync } = require('node:child_process');
|
||||
|
||||
const STATE_DIR = process.env.AGENT_STATE_DIR || '/var/run/agent';
|
||||
const PORT = Number(process.env.AGENT_HEALTH_PORT || 8080);
|
||||
|
|
@ -25,6 +26,13 @@ const STATE_FILE = path.join(STATE_DIR, 'state.json');
|
|||
const UNHEALTHY_MARKER = path.join(STATE_DIR, 'UNHEALTHY');
|
||||
const TOKEN_EXPIRED_MARKER = path.join(STATE_DIR, 'TOKEN_EXPIRED');
|
||||
|
||||
const CACHE_LOW_BYTES = 100 * 1024 * 1024; // 100 MB
|
||||
|
||||
function cacheBytesFree() {
|
||||
const out = execSync('df -PB1 /var/cache').toString().split('\n')[1];
|
||||
return parseInt(out.split(/\s+/)[3], 10);
|
||||
}
|
||||
|
||||
async function exists(p) {
|
||||
try {
|
||||
await fs.access(p);
|
||||
|
|
@ -59,6 +67,14 @@ async function buildResponse() {
|
|||
const heartbeatStale =
|
||||
heartbeatAgeS !== null && heartbeatAgeS > STALE_HEARTBEAT_SECONDS;
|
||||
|
||||
const cacheFreeBytes = cacheBytesFree();
|
||||
if (cacheFreeBytes < CACHE_LOW_BYTES) {
|
||||
return {
|
||||
httpStatus: 503,
|
||||
body: { status: 'unhealthy', reason: 'cache-low' },
|
||||
};
|
||||
}
|
||||
|
||||
let httpStatus = 200;
|
||||
let effectiveStatus = state.status || 'unknown';
|
||||
|
||||
|
|
@ -87,6 +103,7 @@ async function buildResponse() {
|
|||
tokenExpired,
|
||||
unhealthy,
|
||||
},
|
||||
cache_free_bytes: cacheFreeBytes,
|
||||
};
|
||||
|
||||
return { httpStatus, body };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue