From 0e4b3d83890b929221208959b697d81b42763181 Mon Sep 17 00:00:00 2001 From: Martii Date: Tue, 5 Apr 2016 22:06:22 -0600 Subject: [PATCH] Some dep updates * Reinstate *toobusy-js*... at least one of their timers has been fixed on shutdown. See #354, #353, #352 and base issue of #345 ... loosely related to #249 and attempt to address #944 with a work-around... VPS should be faster than our old one so perhaps the timers don't make as much of a difference. Start with our old default lag value... this may introduce too many 503's again but hopefully not * Retested delete op * Bug fixes, tests, and docs updates... please read their CHANGELOGS * Shutdown the server on SIGINT * Modify db closure to not have dependents --- README.md | 6 ++++++ app.js | 38 ++++++++++++++++++++++++++++++++++---- package.json | 13 +++++++------ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bce73079b..3f086daa0 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Repository | Reference | Recent Version [select2][select2GHUrl] | [Documentation][select2DOCUrl] | [![NPM version][select2NPMVersionImage]][select2NPMUrl] [select2-bootstrap-css][select2-bootstrap-cssGHUrl] | [Documentation][select2-bootstrap-cssDOCUrl] | [![NPM version][select2-bootstrap-cssNPMVersionImage]][select2-bootstrap-cssNPMUrl] [serve-favicon][serve-faviconGHUrl] | [Documentation][serve-faviconDOCUrl] | [![NPM version][serve-faviconNPMVersionImage]][serve-faviconNPMUrl] +[toobusy-js][toobusy-jsGHUrl] | [Documentation][toobusy-jsDOCUrl] | [![NPM version][toobusy-jsNPMVersionImage]][toobusy-jsNPMUrl] [uglifyJS2][uglifyJS2GHUrl] [¹][uglifyJS2GHHarmonyUrl] [²][uglifyJS2GHHarmony2Url] | [Documentation][uglifyJS2DOCUrl][¹][uglifyJS2DOC1Url] | [![NPM version][uglifyJS2NPMVersionImage]][uglifyJS2NPMUrl] [underscore][underscoreGHUrl] | [Documentation][underscoreDOCUrl] | [![NPM version][underscoreNPMVersionImage]][underscoreNPMUrl] @@ -403,6 +404,11 @@ Outdated dependencies list can also be achieved with `$ npm --depth 0 outdated` [serve-faviconNPMUrl]: https://www.npmjs.com/package/serve-favicon [serve-faviconNPMVersionImage]: https://img.shields.io/npm/v/serve-favicon.svg?style=flat +[toobusy-jsGHUrl]: https://github.com/STRML/node-toobusy +[toobusy-jsDOCUrl]: https://github.com/STRML/node-toobusy/blob/master/README.md +[toobusy-jsNPMUrl]: https://npmjs.com/package/toobusy-js +[toobusy-jsNPMVersionImage]: https://img.shields.io/npm/v/toobusy-js.svg?style=flat + [uglifyJS2GHUrl]: https://github.com/mishoo/UglifyJS2 [uglifyJS2GHHarmonyUrl]: https://github.com/mishoo/UglifyJS2/tree/harmony [uglifyJS2GHHarmony2Url]: https://github.com/OpenUserJs/UglifyJS2/tree/harmony-named diff --git a/app.js b/app.js index e4514ed01..3ee5fdaf0 100755 --- a/app.js +++ b/app.js @@ -17,6 +17,9 @@ if (isPro) { var path = require('path'); var express = require('express'); +var toobusy = require('toobusy-js'); +var statusCodePage = require('./libs/templateHelpers').statusCodePage; + var methodOverride = require('method-override'); var morgan = require('morgan'); var bodyParser = require('body-parser'); @@ -111,14 +114,41 @@ db.on('reconnected', function () { }); process.on('SIGINT', function () { - db.close(function () { - console.log(chalk.green('MongoDB connection disconnected gracefully with app termination')); - process.exit(0); - }); + console.log(chalk.green('Capturing app termination for an attempt at cleanup')); + + /** + * Attempt to get everything closed before process exit + */ + + // Close the db connection + db.close(); // NOTE: Current asynchronous but auth may prevent callback until completed + + // Stop serving new http connections + server.close(); // NOTE: Currently asynchronous but auth may prevent callback until completed + + // Shutdown timer in toobusy + toobusy.shutdown(); // NOTE: Currently synchronous + + // Terminate the app + process.exit(0); }); var sessionStore = new MongoStore({ mongooseConnection: db }); +// See https://hacks.mozilla.org/2013/01/building-a-node-js-server-that-wont-melt-a-node-js-holiday-season-part-5/ +toobusy.maxLag(100); +app.use(function (aReq, aRes, aNext) { + // check if we're toobusy + if (toobusy()) { + statusCodePage(aReq, aRes, aNext, { + statusCode: 503, + statusMessage: 'We\'re busy right now. Try again later.' + }); + } else { + aNext(); + } +}); + // Force HTTPS if (app.get('securePort')) { sslOptions = { diff --git a/package.json b/package.json index 94c136a85..ea816eb4a 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,12 @@ "dependencies": { "ace-builds": "git://github.com/ajaxorg/ace-builds#4c15514", "async": "1.5.2", - "aws-sdk": "2.2.47", + "aws-sdk": "2.3.1", "body-parser": "1.15.0", "bootstrap": "3.3.6", "bootstrap-markdown": "2.10.0", "chalk": "1.1.1", - "clipboard": "1.5.9", + "clipboard": "1.5.10", "compression": "1.6.1", "connect-mongo": "1.1.0", "cookie-parser": "1.4.1", @@ -22,14 +22,14 @@ "formidable": "1.0.17", "git-rev": "0.2.1", "github": "0.2.4", - "highlight.js": "9.2.0", - "jquery": "2.2.0", + "highlight.js": "9.3.0", + "jquery": "2.2.3", "jwt-simple": "0.5.0", "less-middleware": "2.1.0", "marked": "0.3.5", "method-override": "2.3.5", "moment": "2.12.0", - "mongoose": "4.4.10", + "mongoose": "4.4.11", "morgan": "1.7.0", "mu2": "0.5.20", "octicons": "3.5.0", @@ -50,12 +50,13 @@ "passport-windowslive": "1.0.2", "passport-yahoo": "0.3.0", "pegjs": "0.9.0", - "request": "2.69.0", + "request": "2.70.0", "rfc2047": "2.0.0", "sanitize-html": "1.11.4", "select2": "3.5.2-browserify", "select2-bootstrap-css": "1.4.6", "serve-favicon": "2.3.0", + "toobusy-js": "0.4.3", "uglify-js-harmony": "git://github.com/OpenUserJs/UglifyJS2#harmony-named+cdba43c", "underscore": "1.8.3" },