bytelyst-devops-tools/dashboard/backend/src/modules/health/repository.ts
root fbaaa71a66 feat(devops): adopt trading web deployment model with docker-compose
- Add docker-compose.yml following trading web pattern
- Update web Dockerfile to use multi-stage build with metadata
- Add build metadata (commit SHA, branch, timestamp, author, message)
- Rewrite deploy.sh to use docker compose with build metadata
- Add hotcopy deployment script for quick updates
- Add comprehensive backend API with deployment orchestration
- Add health checks, service management, and monitoring endpoints
- Add CI/CD workflow configuration
- Add deployment documentation and guides

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-05-11 03:24:11 +00:00

70 lines
1.9 KiB
TypeScript

import type { ServiceHealth } from './types.js';
import type { Service } from '../services/types.js';
// Cache health check results to avoid overwhelming services
const healthCache = new Map<string, { health: ServiceHealth; timestamp: number }>();
const CACHE_TTL = 30000; // 30 seconds
export async function checkServiceHealth(service: Service): Promise<ServiceHealth> {
const startTime = Date.now();
let status: 'up' | 'down' | 'degraded' = 'down';
// Check cache first
const cached = healthCache.get(service.id);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
return cached.health;
}
try {
const response = await fetch(service.healthUrl, {
method: 'GET',
signal: AbortSignal.timeout(10000), // 10 second timeout
headers: {
'User-Agent': 'ByteLyst-DevOps-HealthCheck/1.0',
},
});
const responseTime = Date.now() - startTime;
if (response.ok) {
status = responseTime > 5000 ? 'degraded' : 'up';
} else {
status = 'down';
}
const health: ServiceHealth = {
serviceId: service.id,
status,
responseTime,
lastCheck: new Date().toISOString(),
};
// Cache the result
healthCache.set(service.id, { health, timestamp: Date.now() });
return health;
} catch (error) {
const health: ServiceHealth = {
serviceId: service.id,
status: 'down',
lastCheck: new Date().toISOString(),
};
// Cache failures for shorter time (5 seconds)
healthCache.set(service.id, { health, timestamp: Date.now() - CACHE_TTL + 5000 });
return health;
}
}
export async function checkAllServices(services: Service[]): Promise<ServiceHealth[]> {
const healthChecks = await Promise.all(
services.map(service => checkServiceHealth(service))
);
return healthChecks;
}
export function clearHealthCache(): void {
healthCache.clear();
}