Skip to content

Commit 16d5e3e

Browse files
committed
Add maintenance mode and update to gracefully exit process on signal
1 parent 27e17d7 commit 16d5e3e

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

app.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,30 @@ process.on('uncaughtException', function (err) {
501501
logger.error(err);
502502
logger.error('Process will exit now.');
503503
process.exit(1);
504+
});
505+
506+
// gracefully exit
507+
process.on('SIGINT', function () {
508+
config.maintenance = true;
509+
// disconnect all socket.io clients
510+
Object.keys(io.sockets.sockets).forEach(function (key) {
511+
var socket = io.sockets.sockets[key];
512+
// notify client server going into maintenance status
513+
socket.emit('maintenance', config.version);
514+
socket.disconnect(true);
515+
});
516+
var checkCleanTimer = setInterval(function () {
517+
var usersCount = Object.keys(realtime.users).length;
518+
var notesCount = Object.keys(realtime.notes).length;
519+
// check if all users and notes array are empty
520+
if (usersCount == 0 && notesCount == 0) {
521+
// close db connection
522+
models.sequelize.close();
523+
clearInterval(checkCleanTimer);
524+
// wait for a while before exit
525+
setTimeout(function () {
526+
process.exit(0);
527+
}, 100);
528+
}
529+
}, 100);
504530
});

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ function getserverurl() {
7878
}
7979

8080
var version = '0.4.0';
81+
var maintenance = config.maintenance || false;
8182
var cwd = path.join(__dirname, '..');
8283

8384
module.exports = {
8485
version: version,
86+
maintenance: maintenance,
8587
debug: debug,
8688
urlpath: urlpath,
8789
port: port,

lib/realtime.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ var realtime = {
2525
onAuthorizeFail: onAuthorizeFail,
2626
secure: secure,
2727
connection: connection,
28-
getStatus: getStatus
28+
getStatus: getStatus,
29+
users: users,
30+
notes: notes
2931
};
3032

3133
function onAuthorizeSuccess(data, accept) {
@@ -70,8 +72,9 @@ function emitCheck(note) {
7072
}
7173

7274
//actions
73-
var users = {};
74-
var notes = {};
75+
var users, notes;
76+
realtime.users = users = {};
77+
realtime.notes = notes = {};
7578
//update when the note is dirty
7679
var updater = setInterval(function () {
7780
async.each(Object.keys(notes), function (key, callback) {
@@ -536,6 +539,7 @@ function ifMayEdit(socket, callback) {
536539
}
537540

538541
function connection(socket) {
542+
if (config.maintenance) return;
539543
parseNoteIdFromSocket(socket, function (err, noteId) {
540544
if (err) {
541545
return failConnection(500, err, socket);

public/js/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,11 @@ socket.on('error', function (data) {
18251825
if (data.message && data.message.indexOf('AUTH failed') === 0)
18261826
location.href = "./403";
18271827
});
1828+
var retryOnDisconnect = false;
1829+
socket.on('maintenance', function (data) {
1830+
if (data == version)
1831+
retryOnDisconnect = true;
1832+
});
18281833
socket.on('disconnect', function (data) {
18291834
showStatus(statusType.offline);
18301835
if (loaded) {
@@ -1833,12 +1838,15 @@ socket.on('disconnect', function (data) {
18331838
}
18341839
if (!editor.getOption('readOnly'))
18351840
editor.setOption('readOnly', true);
1841+
if (retryOnDisconnect)
1842+
socket.connect();
18361843
});
18371844
socket.on('reconnect', function (data) {
18381845
//sync back any change in offline
18391846
emitUserStatus(true);
18401847
cursorActivity();
18411848
socket.emit('online users');
1849+
retryOnDisconnect = false;
18421850
});
18431851
socket.on('connect', function (data) {
18441852
personalInfo['id'] = socket.id;

0 commit comments

Comments
 (0)