Health Checks với @nestjs/terminus:
typescript
import { TerminusModule, TypeOrmHealthIndicator, HealthCheckService, MicroserviceHealthIndicator } from '@nestjs/terminus';
import { Transport, RedisOptions } from '@nestjs/microservices';
// Lưu ý: HealthController là controller do user tạo, không phải từ @nestjs/terminus.
@Module({ imports: [TerminusModule] })
export class HealthModule {}
// controller
@Get('health')
@HealthCheck()
check() {
return this.health.check([
() => this.db.pingCheck('database'),
// Redis không nói HTTP — ping bằng MicroserviceHealthIndicator, không phải HttpHealthIndicator
() => this.microservice.pingCheck<RedisOptions>('redis', {
transport: Transport.REDIS,
options: { host: 'redis', port: 6379 },
}),
() => this.memory.checkHeap('memory_heap', 300 * 1024 * 1024),
]);
}Graceful Shutdown: đảm bảo app xử lý xong in-flight requests trước khi shutdown:
typescript
// main.ts
app.enableShutdownHooks(); // Lắng nghe SIGTERM, SIGINT
// Service có thể implement OnApplicationShutdown
async onApplicationShutdown(signal?: string) {
this.logger.log(`Shutting down on signal: ${signal}`);
await this.closeConnections();
}Kubernetes gửi SIGTERM khi pod bị terminate — NestJS cần hoàn thành requests đang xử lý trước khi exit.
Set terminationGracePeriodSeconds: 30 trong K8s manifest.