mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-14 16:02:14 +00:00
chore(mongoose): set some options per #2725 to address high number of open connections, replset connection timeout, and close mongoose connection on process death
This commit is contained in:
parent
4c094ebf47
commit
6a86da316b
4 changed files with 20 additions and 34 deletions
|
|
@ -3,7 +3,6 @@ var Schema = mongoose.Schema;
|
|||
var shared = require('habitrpg-shared');
|
||||
var _ = require('lodash');
|
||||
var TaskSchemas = require('./task');
|
||||
var Group = require('./group').model;
|
||||
|
||||
var ChallengeSchema = new Schema({
|
||||
_id: {type: String, 'default': shared.uuid},
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ var Schema = mongoose.Schema;
|
|||
var shared = require('habitrpg-shared');
|
||||
var _ = require('lodash');
|
||||
var async = require('async');
|
||||
var User = require('./user').model;
|
||||
|
||||
var GroupSchema = new Schema({
|
||||
_id: {type: String, 'default': shared.uuid},
|
||||
|
|
@ -161,8 +160,6 @@ GroupSchema.methods.finishQuest = function(quest, cb) {
|
|||
})
|
||||
var members = _.keys(group.quest.members);
|
||||
group.quest = {};group.markModified('quest');
|
||||
// FIXME this is TERRIBLE practice. Looks like there are circular dependencies in the models, such that `var User` at
|
||||
// this point is undefined. So we get around that by loading from mongoose only once we get to this point
|
||||
mongoose.models.User.update({_id:{$in:members}}, updates, {multi:true}, cb);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,14 +35,18 @@ if (cluster.isMaster && (isDev || isProd)) {
|
|||
|
||||
// ------------ MongoDB Configuration ------------
|
||||
mongoose = require('mongoose');
|
||||
require('./models/user'); //load up the user schema - TODO is this necessary?
|
||||
require('./models/group');
|
||||
require('./models/challenge');
|
||||
mongoose.connect(nconf.get('NODE_DB_URI'), {auto_reconnect:true}, function(err) {
|
||||
if (err) throw err;
|
||||
logging.info('Connected with Mongoose');
|
||||
var mongooseOptions = !isProd ? {} : {
|
||||
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
|
||||
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }
|
||||
};
|
||||
mongoose.connect(nconf.get('NODE_DB_URI'), mongooseOptions, function(err) {
|
||||
if (err) throw err;
|
||||
logging.info('Connected with Mongoose');
|
||||
});
|
||||
|
||||
// load schemas & models
|
||||
require('./challenge');
|
||||
require('./group');
|
||||
require('./user');
|
||||
|
||||
// ------------ Passport Configuration ------------
|
||||
var passport = require('passport')
|
||||
|
|
@ -63,7 +67,6 @@ if (cluster.isMaster && (isDev || isProd)) {
|
|||
done(null, obj);
|
||||
});
|
||||
|
||||
|
||||
// Use the FacebookStrategy within Passport.
|
||||
// Strategies in Passport require a `verify` function, which accept
|
||||
// credentials (in this case, an accessToken, refreshToken, and Facebook
|
||||
|
|
@ -135,7 +138,7 @@ if (cluster.isMaster && (isDev || isProd)) {
|
|||
app.use('/api/v1', require('./routes/apiv1').middleware);
|
||||
app.use('/export', require('./routes/dataexport').middleware);
|
||||
|
||||
app.use(utils.crashWorker(server));
|
||||
app.use(utils.crashWorker(server,mongoose));
|
||||
app.use(utils.errorHandler);
|
||||
|
||||
require('./routes/apiv2.coffee')(swagger, v2);
|
||||
|
|
|
|||
29
src/utils.js
29
src/utils.js
|
|
@ -6,10 +6,10 @@ var cluster = require("cluster");
|
|||
|
||||
module.exports.sendEmail = function(mailData) {
|
||||
var smtpTransport = nodemailer.createTransport("SMTP",{
|
||||
service: nconf.get('SMTP_SERVICE'),
|
||||
service: nconf.get('SMTP_SERVICE'),
|
||||
auth: {
|
||||
user: nconf.get('SMTP_USER'),
|
||||
pass: nconf.get('SMTP_PASS')
|
||||
user: nconf.get('SMTP_USER'),
|
||||
pass: nconf.get('SMTP_PASS')
|
||||
}
|
||||
});
|
||||
smtpTransport.sendMail(mailData, function(error, response){
|
||||
|
|
@ -33,8 +33,6 @@ module.exports.makeSalt = function() {
|
|||
return crypto.randomBytes(Math.ceil(len / 2)).toString('hex').substring(0, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Load nconf and define default configuration values if config.json or ENV vars are not found
|
||||
*/
|
||||
|
|
@ -44,25 +42,13 @@ module.exports.setupConfig = function(){
|
|||
//.file('defaults', path.join(path.resolve(__dirname, '../config.json.example')))
|
||||
.file('user', path.join(path.resolve(__dirname, '../config.json')));
|
||||
|
||||
// var agent;
|
||||
// if (process.env.NODE_ENV === 'development') {
|
||||
// // Follow these instructions for profiling / debugging leaks
|
||||
// // * https://developers.google.com/chrome-developer-tools/docs/heap-profiling
|
||||
// // * https://developers.google.com/chrome-developer-tools/docs/memory-analysis-101
|
||||
// agent = require('webkit-devtools-agent');
|
||||
// var logging = require('./logging');
|
||||
// logging.info("To debug memory leaks:" +
|
||||
// "\n\t(1) Run `kill -SIGUSR2 " + process.pid + "`" +
|
||||
// "\n\t(2) open http://c4milo.github.com/node-webkit-agent/21.0.1180.57/inspector.html?host=localhost:1337&page=0");
|
||||
// }
|
||||
|
||||
if (nconf.get('NODE_ENV') === "development") {
|
||||
if (nconf.get('NODE_ENV') === "development")
|
||||
Error.stackTraceLimit = Infinity;
|
||||
}
|
||||
if (nconf.get('NODE_ENV') === 'production') require('newrelic');
|
||||
if (nconf.get('NODE_ENV') === 'production')
|
||||
require('newrelic');
|
||||
};
|
||||
|
||||
module.exports.crashWorker = function(server) {
|
||||
module.exports.crashWorker = function(server,mongoose) {
|
||||
return function(err, req, res, next) {
|
||||
if (!cluster.isMaster) {
|
||||
// make sure we close down within 30 seconds
|
||||
|
|
@ -73,6 +59,7 @@ module.exports.crashWorker = function(server) {
|
|||
killtimer.unref();
|
||||
// stop taking new requests.
|
||||
server.close();
|
||||
mongoose.connection.close();
|
||||
cluster.worker.disconnect();
|
||||
}
|
||||
next(err);
|
||||
|
|
|
|||
Loading…
Reference in a new issue