Skip to content

Commit

Permalink
enhancement: Removing a dependency + improving server shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Aug 21, 2017
1 parent 869dd37 commit d46338c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -63,7 +63,6 @@
"abe": "dist/index.js"
},
"dependencies": {
"@moebius/http-graceful-shutdown": "^1.0.1",
"ajax-request": "^1.2.1",
"bcrypt-nodejs": "0.0.3",
"bluebird": "^3.4.6",
Expand Down
21 changes: 19 additions & 2 deletions src/cli/core/manager/Manager.js
Expand Up @@ -132,14 +132,31 @@ class Manager {
console.error(
clc.red("can't start the Abe's watch server\n"),
'This watch server has tried to listen on the port ' +
lport +
' but this server is already in use by another process... '
lport +
' but this server is already in use by another process... '
)
} else {
console.error(err)
}
})

process.on('SIGINT', () => {
try {
this.lserver.close(() => {
console.log('SIGINT:Livereload server has been gracefully terminated')
process.exit(0)
})
} catch(error){}
})
process.on('SIGTERM', () => {
try {
this.lserver.close(() => {
console.log('SIGTERM:Livereload server has been gracefully terminated')
process.exit(0)
})
} catch(error){}
})

// sync assets from templates to /site
cmsTemplates.assets.copy()
}
Expand Down
47 changes: 35 additions & 12 deletions src/server/app.js
Expand Up @@ -16,7 +16,6 @@ import flash from 'connect-flash'
import cookieParser from 'cookie-parser'
import csrf from 'csurf'
import passport from 'passport'
import {GracefulShutdownManager} from '@moebius/http-graceful-shutdown'

import {
config,
Expand Down Expand Up @@ -239,17 +238,41 @@ if (coreUtils.file.exist(path.join(config.root, 'cert.pem'))) {
})
}

var shutdownManager = new GracefulShutdownManager(server)
process.on('SIGINT', () => {
shutdownManager.terminate(() => {
console.log('SIGINT:Server has been gracefully terminated')
})
})
process.on('SIGTERM', () => {
shutdownManager.terminate(() => {
console.log('SIGTERM:Server has been gracefully terminated')
})
})
server.on("error", onError)

function onError(error) {
if (error.syscall !== 'listen') throw error;

var bind = typeof port === 'string'? 'Pipe ' + port : 'Port ' + port;

switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

var cleanup = function () {
server.close(function () {
console.log("Closed out remaining connections.");
process.exit();
});

setTimeout(function () {
console.log("Could not close connections in time, forcing shut down");
process.exit(1);
}, 10 * 1000);
}

process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup)

// important : require here so config.root is defined
var routes = require('./routes')
Expand Down

0 comments on commit d46338c

Please sign in to comment.