Spaces:
Running
Running
| 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.'); |