(signal: string)
| 364 | ) { |
| 365 | let receivedSignal = false; |
| 366 | const cleanup = async (signal: string) => { |
| 367 | try { |
| 368 | if (receivedSignal) { |
| 369 | // If we receive another signal while we're waiting |
| 370 | // for the server to stop, just ignore it. |
| 371 | return; |
| 372 | } |
| 373 | receivedSignal = true; |
| 374 | await this.initializationPromise; |
| 375 | await this.callDestroyHook(); |
| 376 | await this.callBeforeShutdownHook(signal); |
| 377 | await this.dispose(); |
| 378 | await this.callShutdownHook(signal); |
| 379 | signals.forEach(sig => process.removeListener(sig, cleanup)); |
| 380 | |
| 381 | if (options.useProcessExit) { |
| 382 | // Use process.exit() to ensure the 'exit' event is properly triggered. |
| 383 | // This is required for async loggers (like Pino with transports) |
| 384 | // to flush their buffers before the process terminates. |
| 385 | process.exit(0); |
| 386 | } else { |
| 387 | process.kill(process.pid, signal); |
| 388 | } |
| 389 | } catch (err) { |
| 390 | Logger.error( |
| 391 | MESSAGES.ERROR_DURING_SHUTDOWN, |
| 392 | (err as Error)?.stack, |
| 393 | NestApplicationContext.name, |
| 394 | ); |
| 395 | process.exit(1); |
| 396 | } |
| 397 | }; |
| 398 | this.shutdownCleanupRef = cleanup as (...args: unknown[]) => unknown; |
| 399 | |
| 400 | signals.forEach((signal: string) => { |
nothing calls this directly
no test coverage detected