Create Dashboard service health check
This commit is contained in:
52
services/dashboard/src/healthcheck.js
Normal file
52
services/dashboard/src/healthcheck.js
Normal file
@ -0,0 +1,52 @@
|
||||
import { createClient } from 'redis';
|
||||
import { Client as PgClient } from 'pg';
|
||||
import http from 'http';
|
||||
|
||||
const POSTGRES_URL = process.env.POSTGRES_URL;
|
||||
const REDIS_URL = process.env.REDIS_URL || 'redis://redis:6379';
|
||||
|
||||
async function healthCheck() {
|
||||
try {
|
||||
// Test PostgreSQL connection
|
||||
const pgClient = new PgClient({ connectionString: POSTGRES_URL });
|
||||
await pgClient.connect();
|
||||
await pgClient.query('SELECT 1');
|
||||
await pgClient.end();
|
||||
|
||||
// Test Redis connection
|
||||
const redisClient = createClient({ url: REDIS_URL });
|
||||
await redisClient.connect();
|
||||
await redisClient.ping();
|
||||
await redisClient.quit();
|
||||
|
||||
// Test web server
|
||||
const options = {
|
||||
hostname: 'localhost',
|
||||
port: 3000,
|
||||
path: '/health',
|
||||
method: 'GET',
|
||||
timeout: 5000
|
||||
};
|
||||
|
||||
const req = http.request(options, (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
console.log('Health check passed');
|
||||
process.exit(0);
|
||||
} else {
|
||||
throw new Error(`HTTP ${res.statusCode}`);
|
||||
}
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
req.end();
|
||||
|
||||
} catch (error) {
|
||||
console.error('Health check failed:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
healthCheck();
|
Reference in New Issue
Block a user