Spaces:
Sleeping
Sleeping
File size: 1,273 Bytes
da819ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
const http = require('http');
function checkServerHealth() {
const options = {
hostname: 'localhost',
port: 5000,
path: '/api/health',
method: 'GET',
timeout: 5000
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const response = JSON.parse(data);
if (response.status === 'OK') {
console.log(`[${new Date().toISOString()}] Server is healthy`);
} else {
console.error(`[${new Date().toISOString()}] Server returned unexpected status:`, response);
}
} catch (error) {
console.error(`[${new Date().toISOString()}] Failed to parse health check response:`, error);
}
});
});
req.on('error', (error) => {
console.error(`[${new Date().toISOString()}] Server health check failed:`, error.message);
});
req.on('timeout', () => {
console.error(`[${new Date().toISOString()}] Server health check timed out`);
req.destroy();
});
req.end();
}
// Check server health every 30 seconds
setInterval(checkServerHealth, 30000);
// Initial check
checkServerHealth();
console.log('Server monitoring started. Health checks every 30 seconds.'); |