From 0d8e4e9aab23d10bdef91baeafffa75e102a46ea Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Sun, 30 Jun 2024 11:09:49 -0700 Subject: [PATCH] Shuts down the service in a graceful way Signed-off-by: Dan Cunningham --- app.js | 21 ++++++++++++++++----- socket-io.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index 6eee26c..f30c3fe 100644 --- a/app.js +++ b/app.js @@ -105,7 +105,7 @@ if (config.system.subDomainCookies) { if (app.get('env') === 'development') { app.use(errorHandler()); } -if (system.getLoggerMorganOption()){ +if (system.getLoggerMorganOption()) { app.use(system.getLoggerMorganOption()); } // App configuration for all environments @@ -201,12 +201,23 @@ rt.setSocketIO(socketIO); rt.setupRoutes(app); function shutdown() { - // TODO: save current request id? + // Stop accepting new requests + socketIO.shutDown().then(() => { + server.close((err) => { + if (err) { + console.error('Error while shutting down:', err); + process.exit(1); + } + process.exit(0); + }); + logger.info('will wait up to 5 seconds to shut down'); + setTimeout(() => { + process.exit(0); + }, 5000); + + }); logger.info('Stopping every5min statistics job'); every5MinStatJob.stop(); - - logger.info('Safe shutdown complete'); - process.exit(); } process.on('SIGINT', function () { diff --git a/socket-io.js b/socket-io.js index 3e758d7..2c88315 100644 --- a/socket-io.js +++ b/socket-io.js @@ -7,7 +7,7 @@ const logger = require('./logger.js'), Event = require('./models/event'), Notification = require('./models/notification'), notificationSender = require('./notificationsender'); - + /** * Socket.IO Logic for incoming openHAB servers * @param {*} server @@ -15,8 +15,9 @@ const logger = require('./logger.js'), */ function SocketIO(server, system) { const internalAddress = system.getInternalAddress(); - var requestTracker = new requesttracker(); - var io = require('socket.io')(server, { + let isShuttingDown = false; + const requestTracker = new requesttracker(); + const io = require('socket.io')(server, { maxHttpBufferSize: 1e8, //100mb, this was a previous default in engine.io before the upgrade to 3.6.0 which sets it to 1mb. May want to revisit. logger: logger }); @@ -24,10 +25,34 @@ function SocketIO(server, system) { this.io = io; this.requestTracker = requestTracker; + this.shutDown = function () { + isShuttingDown = true; + return new Promise((resolve) => { + io.sockets.clients((error, clients) => { + if (error) { + console.error('Error retrieving clients:', error); + process.exit(1); + } + clients.forEach(clientId => { + const socket = io.sockets.connected[clientId]; + if (socket) { + socket.disconnect(true); + } + }); + console.log('All socket.io connections closed.'); + resolve(); + }); + }); + } + /** Socket.io Routes **/ //Check if we have blocked this connection io.use(function (socket, next) { + if (isShuttingDown) { + next(new Error('Shutting down')); + return; + } const uuid = socket.handshake.query['uuid'] || socket.handshake.headers['uuid']; if (uuid) { redis.ttl('blocked:' + uuid, (err, result) => {