File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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} ) ;
Original file line number Diff line number Diff line change @@ -78,10 +78,12 @@ function getserverurl() {
7878}
7979
8080var version = '0.4.0' ;
81+ var maintenance = config . maintenance || false ;
8182var cwd = path . join ( __dirname , '..' ) ;
8283
8384module . exports = {
8485 version : version ,
86+ maintenance : maintenance ,
8587 debug : debug ,
8688 urlpath : urlpath ,
8789 port : port ,
Original file line number Diff line number Diff 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
3133function 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
7679var updater = setInterval ( function ( ) {
7780 async . each ( Object . keys ( notes ) , function ( key , callback ) {
@@ -536,6 +539,7 @@ function ifMayEdit(socket, callback) {
536539}
537540
538541function connection ( socket ) {
542+ if ( config . maintenance ) return ;
539543 parseNoteIdFromSocket ( socket , function ( err , noteId ) {
540544 if ( err ) {
541545 return failConnection ( 500 , err , socket ) ;
Original file line number Diff line number Diff 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+ } ) ;
18281833socket . 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} ) ;
18371844socket . 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} ) ;
18431851socket . on ( 'connect' , function ( data ) {
18441852 personalInfo [ 'id' ] = socket . id ;
You can’t perform that action at this time.
0 commit comments