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

[Issue #7] Add support for graceful shutdowns. #21

Merged
merged 8 commits into from Dec 9, 2013
2 changes: 2 additions & 0 deletions config/webcore.json
Expand Up @@ -4,6 +4,8 @@

"host": ["OPENSHIFT_NODEJS_IP", "VCAP_APP_HOST", "HOST", "IP"],

"shutdownTimeout": 30000,

"globalAgent": {
"maxSockets": 250
},
Expand Down
5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -108,7 +108,7 @@ var kraken = {
}

function bind(app) {
var deferred, server, ssl;
var deferred, server, ssl, httpServer;

if (port === undefined) {
port = that.port;
Expand Down Expand Up @@ -138,7 +138,8 @@ var kraken = {
server = ssl ? https.createServer(ssl, app) : http.createServer(app);
server.once('listening', resolve);
server.once('error', reject);
server.listen(port, host);
httpServer = server.listen(port, host);
app.set('kraken:server', httpServer);

return deferred.promise;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/appcore.js
Expand Up @@ -163,7 +163,9 @@ var proto = {
config = this._config.get('middleware');
srcRoot = this._resolve(config.static.srcRoot);
staticRoot = this._resolve(config.static.rootPath);
errorPages = config.errorPages || {};

app.use(kraken.shutdown(app, this._config, errorPages['503']));
app.use(express.favicon());
app.use(kraken.compiler(srcRoot, staticRoot, this._config, this._i18n));
app.use(express.static(staticRoot));
Expand Down Expand Up @@ -197,7 +199,6 @@ var proto = {
delegate.requestAfterRoute(app);
}

errorPages = config.errorPages || {};
app.use(kraken.fileNotFound(errorPages['404']));
app.use(kraken.serverError(errorPages['500']));
app.use(kraken.errorHandler(config.errorHandler));
Expand Down
2 changes: 2 additions & 0 deletions lib/middleware/index.js
Expand Up @@ -33,6 +33,8 @@ exports = module.exports = {
// TODO: Add multipart support
//multipart: require('./multipart'),

shutdown: require('./shutdown'),

compiler: require('./compiler'),

fileNotFound: error.fileNotFound,
Expand Down
56 changes: 56 additions & 0 deletions lib/middleware/shutdown.js
@@ -0,0 +1,56 @@
/***@@@ BEGIN LICENSE @@@***/
/*───────────────────────────────────────────────────────────────────────────*\
│ Copyright (C) 2013 eBay Software Foundation │
│ │
│hh ,'""`. │
│ / _ _ \ Licensed under the Apache License, Version 2.0 (the "License"); │
│ |(@)(@)| you may not use this file except in compliance with the License. │
│ ) __ ( You may obtain a copy of the License at │
│ /,'))((`.\ │
│(( (( )) )) http://www.apache.org/licenses/LICENSE-2.0 │
│ `\ `)(' /' │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
/***@@@ END LICENSE @@@***/
'use strict';

module.exports = function (app, config, template) {

var gracefulShutdown = function () {
var server, timeout;
config.set('kraken:state', 'disconnecting');

server = app.get('kraken:server');
// switch to kraken.close once it's implemented
server.close(function () {
process.exit();
});

timeout = config.get('shutdownTimeout');
setTimeout(function () {
process.exit(1);
}, timeout);
};

process.on('SIGTERM', gracefulShutdown);
process.on('SIGINT', gracefulShutdown);

return function (req, res, next) {
if (config.get('kraken:state') !== 'disconnecting') {
return next();
}
res.setHeader('Connection', 'close');
if (template && !req.xhr) {
res.status(503);
res.render(template);
} else {
res.send(503, 'Server is in the process of shutting down.');
}
};

};