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

Shuts down the service in a graceful way #468

Merged
merged 1 commit into from
Jun 30, 2024
Merged
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
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