Senior #lifecycle#shutdown#hooks#kubernetes
Lifecycle hooks trong NestJS và graceful shutdown (OnModuleInit, OnApplicationShutdown)
⚡ Trả lời nhanh
Nest có hook OnModuleInit, OnApplicationBootstrap khi khởi động và OnModuleDestroy, BeforeApplicationShutdown, OnApplicationShutdown khi tắt. Shutdown hook mặc định tắt, phải gọi app.enableShutdownHooks() để Nest lắng nghe SIGTERM và chạy dọn dẹp (đóng DB, queue, drain request).
Thứ tự:
| Giai đoạn | Hook |
|---|---|
| Khởi động | onModuleInit → onApplicationBootstrap |
| Tắt | onModuleDestroy → beforeApplicationShutdown(signal) → onApplicationShutdown(signal) |
// main.ts
const app = await NestFactory.create(AppModule);
app.enableShutdownHooks(); // lắng nghe SIGTERM/SIGINT
await app.listen(3000);
@Injectable()
export class QueueService implements OnModuleInit, OnApplicationShutdown {
async onModuleInit() { await this.client.connect(); }
async onApplicationShutdown(signal?: string) {
await this.worker.close(); // chờ job đang chạy hoàn tất
await this.client.quit();
}
}
- Kubernetes gửi SIGTERM, đợi
terminationGracePeriodSecondsrồi SIGKILL; app cần dừng nhận request mới, hoàn tất request đang chạy, rồi đóng tài nguyên. - Hook async: Nest chờ
Promisehoàn tất trước khi sang hook tiếp. onModuleInitchạy theo thứ tự phụ thuộc module; nên dùng cho khởi tạo nặng thay vì constructor (constructor khôngawaitđược).- Kết hợp readiness probe (Terminus) trả không-sẵn-sàng ngay khi bắt đầu shutdown để LB ngừng gửi traffic.