diff --git a/src/main/routes/health.ts b/src/main/routes/health.ts index 728771a8..6ba4d470 100644 --- a/src/main/routes/health.ts +++ b/src/main/routes/health.ts @@ -1,13 +1,7 @@ -import { app as myApp } from '../app'; - import { Application } from 'express'; const healthcheck = require('@hmcts/nodejs-healthcheck'); -function shutdownCheck(): boolean { - return myApp.locals.shutdown; -} - export default function (app: Application): void { const healthCheckConfig = { checks: { @@ -15,9 +9,7 @@ export default function (app: Application): void { sampleCheck: healthcheck.raw(() => healthcheck.up()), }, readinessChecks: { - shutdownCheck: healthcheck.raw(() => { - return shutdownCheck() ? healthcheck.down() : healthcheck.up(); - }), + shutdownCheck: healthcheck.raw(() => healthcheck.up()), }, }; diff --git a/src/main/server.ts b/src/main/server.ts index f36c14e1..ab59da02 100755 --- a/src/main/server.ts +++ b/src/main/server.ts @@ -1,5 +1,6 @@ #!/usr/bin/env node import * as fs from 'fs'; +import * as http from 'http'; import * as https from 'https'; import * as path from 'path'; @@ -9,7 +10,7 @@ const { Logger } = require('@hmcts/nodejs-logging'); const logger = Logger.getLogger('server'); -let httpsServer: https.Server | null = null; +let server: https.Server | http.Server | null; // used by shutdownCheck in readinessChecks app.locals.shutdown = false; @@ -23,28 +24,30 @@ if (app.locals.ENV === 'development') { cert: fs.readFileSync(path.join(sslDirectory, 'localhost.crt')), key: fs.readFileSync(path.join(sslDirectory, 'localhost.key')), }; - httpsServer = https.createServer(sslOptions, app); - httpsServer.listen(port, () => { + server = https.createServer(sslOptions, app); + server.listen(port, () => { logger.info(`Application started: https://localhost:${port}`); }); } else { - app.listen(port, () => { + server = http.createServer(app); + server.listen(port, () => { logger.info(`Application started: http://localhost:${port}`); }); } function gracefulShutdownHandler(signal: string) { - logger.info(`⚠️ Caught ${signal}, gracefully shutting down. Setting readiness to DOWN`); - // stop the server from accepting new connections - app.locals.shutdown = true; + logger.info(`⚠️ Caught ${signal}, gracefully shutting down`); + + server?.close(() => { + logger.info('Connections closed, exiting'); + process.exit(0); + }); setTimeout(() => { - logger.info('Shutting down application'); + logger.info('Forcefully shutting down application'); // Close server if it's running - httpsServer?.close(() => { - logger.info('HTTPS server closed'); - }); - }, 4000); + process.exit(1); + }, 10000); } process.on('SIGINT', gracefulShutdownHandler);