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
3 changes: 2 additions & 1 deletion index.js
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);
var 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, settings, template) {

var gracefulShutdown = function () {
settings.set('kraken:shuttingDown', true);
console.log("Received kill signal, shutting down gracefully.");
var server = app.get('kraken:server');
// switch to kraken.close once it's implemented
server.close(function () {
console.log("Closed out remaining connections.");
process.exit();
});

setTimeout(function () {
console.error("Could not close connections in time, forcefully shutting down.");
process.exit(1);
}, 30 * 1000);
};

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

return function (req, res, next) {
if (!settings.get('kraken:shuttingDown')) {
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 restarting.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with the team.
Thanks!
Just a minor nitpick on this line: Should it say "shutting down" instead of "restarting"?

}
};

};