Skip to content

Commit

Permalink
Shuts down the service in a graceful way (#468)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored Jun 30, 2024
1 parent 0491073 commit e374614
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
21 changes: 16 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down
31 changes: 28 additions & 3 deletions socket-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,52 @@ 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
* @param {*} system
*/
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
});

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) => {
Expand Down

0 comments on commit e374614

Please sign in to comment.