Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix graceful shutdown #1102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/main/routes/health.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
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: {
// TODO: replace this sample check with proper checks for your application
sampleCheck: healthcheck.raw(() => healthcheck.up()),
},
readinessChecks: {
shutdownCheck: healthcheck.raw(() => {
return shutdownCheck() ? healthcheck.down() : healthcheck.up();
}),
shutdownCheck: healthcheck.raw(() => healthcheck.up()),
},
};

Expand Down
27 changes: 15 additions & 12 deletions src/main/server.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand All @@ -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);
Expand Down