* Monitors the app and the server to close the HTTP server when * either one of them goes down. Sets up event listeners for graceful shutdown. * * @param nodeHttpServer - The Node.js HTTP or HTTPS server instance to monitor * @param app - The application service instance * @param logg
(
nodeHttpServer: NodeHttpsServer | NodeHttpServer,
app: ApplicationService,
logger: LoggerService
)
| 65 | * @param logger - The logger service for error reporting |
| 66 | */ |
| 67 | #monitorAppAndServer( |
| 68 | nodeHttpServer: NodeHttpsServer | NodeHttpServer, |
| 69 | app: ApplicationService, |
| 70 | logger: LoggerService |
| 71 | ) { |
| 72 | /** |
| 73 | * Close the HTTP server when the application begins to |
| 74 | * terminate |
| 75 | */ |
| 76 | app.terminating(async () => { |
| 77 | debug('terminating signal received') |
| 78 | await this.#close(nodeHttpServer) |
| 79 | }) |
| 80 | |
| 81 | /** |
| 82 | * Terminate the app when the HTTP server crashes |
| 83 | */ |
| 84 | nodeHttpServer.once('error', (error: NodeJS.ErrnoException) => { |
| 85 | debug('http server crashed with error "%O"', error) |
| 86 | logger.fatal({ err: error }, error.message) |
| 87 | process.exitCode = 1 |
| 88 | app.terminate() |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Starts the HTTP server on a given host and port using environment variables. |