Skip to content

Instantly share code, notes, and snippets.

@myndzi
Last active August 29, 2015 14:05
Show Gist options
  • Save myndzi/cf30c18ab5adf0863989 to your computer and use it in GitHub Desktop.
Save myndzi/cf30c18ab5adf0863989 to your computer and use it in GitHub Desktop.
'use strict';
var Promise = require('bluebird');
module.exports = function (app) {
var c = app.config, log = app.log;
return Promise.promisify(app.listen)(
c.get('port'),
c.get('ip')
).then(function (server) {
var a = server.address();
log.info('Server listening on ' + a.address + ':' + a.port + ' in ' + app.env + ' mode');
app._onShutdown$ = new Promise(function (resolve, reject) {
var shutdown$;
app.shutdown = function () {
if (shutdown$) { return shutdown$; }
var timeout = c.get('shutdown.timeout');
if (timeout) {
log.info('Shutting down (timeout ' + timeout + 'ms)');
} else {
log.info('Shutting down (no timeout)');
}
var promises = [ ];
app.emit('shutdown', function (arg) {
if (typeof arg.then === 'function') {
promises.push(arg);
} else {
promises.push(Promise.promisify(arg)());
}
});
if (timeout) {
shutdown$ = Promise.any([
Promise.delay(timeout),
Promise.all(promises)
]).then(resolve, reject);
} else {
shutdown$ = Promise.all(promises)
.then(resolve, reject);
}
return shutdown$;
};
});
app.on('shutdown', function (await) {
log.info('Closing HTTP server');
await(server.close);
});
return app;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment